Problem with splitting a file

borrie

New member
Joined
Jun 19, 2007
Messages
3
Programming Experience
1-3
Helo everybody,

I have a problem with splitting a textfile, the file looks like this:

VB.NET:
H1,1,blabla
S1,2,blabla
C1,3,blablaC1
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,6,C1oneP2one
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,6,C1oneP2two
C1,3,066,WOODBRIDGE,Grundisburgh Road,Woodbridge,Suffolk,Woodbridge,GB,IP13 6HX,91234,912345,info@test.co.uk
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,6,C11twoP2one
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,6,c1twoP2two
C1,3,066,WOODBRIDGE,Grundisburgh Road,Woodbridge,Suffolk,Woodbridge,GB,IP13 6HX,91234,912345,info@test.co.uk
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,C1treeP2one
Z1,7,7,1,2,6,,89.00,250.00

The program has to split the files as followed
split when there is a new c1 line and if there are multiple p2 lines in a C1 split these also in new lines. and everytime copy the H1 S1 line before and the Z1 as last so you end up with seperate files like this:

VB.NET:
H1,1,blabla
S1,2,blabla
C1,3,blablaC1
P1,4,PO123,1,1,15/09/07,Awaiting first delivery,Pounds Sterling,066,123,15/09/07,1,124,Apron,,WYE124,2,,,12.50,25.00,25.00,50.00,Notes
P1,5,PO123,2,1,15/09/07,Awaitin first delivery,Pounds Sterling,066,123,15/09/07,1,126,Mitt,,WyE126,4,,,16.00,64.00,50.00,200.00,Notes
P2,6,C1oneP2one
Z1,7,7,1,2,6,,89.00,250.00

I've managed to split everything into different files if there is a new C1 line, but now i want to do the same with p2 lines also, does anybody how to loop this or is there a more simple way to do this? Thanks a lot, here under is the code i wrote.

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim FILE_NAME As String = "C:\readtest.txt"
        Dim H1, S1, Z1, Line As String
        Dim C1_COUNT, iLineCount, iC1Position, i, i2, iposloop, iloop As Integer
        Dim aLineCount(9999), aC1Position(99) As Integer

        Dim objReader As New StreamReader(FILE_NAME)
        Dim MyFileText As New StringCollection
        iLineCount = 1
        iC1Position = 0
        iposloop = 0
  
        Do While objReader.Peek <> -1
            Line = objReader.ReadLine()
            MyFileText.Add(Line)
            If Line.StartsWith("H1") Then
                H1 = Line
            End If
            If Line.StartsWith("S1") Then
                S1 = Line
            End If
            If Line.StartsWith("C1") Then
                C1_COUNT += 1
                aC1Position(iC1Position) = iLineCount
                iC1Position += 1
            End If
            If Line.StartsWith("Z1") Then
                Z1 = Line
            End If
            iLineCount += 1
        Loop
        aC1Position(iC1Position) = iLineCount - 1
   
        For i = 0 To C1_COUNT - 1


            Dim FS As FileStream = New FileStream("c:\MyTextFile" & iposloop + 1 & ".txt", FileMode.CreateNew)
            Dim sw As StreamWriter = New StreamWriter(FS)
            sw.WriteLine(H1)
            sw.WriteLine(S1)
            iloop = aC1Position(iposloop) - 1
            For i2 = aC1Position(iposloop) To ((aC1Position(iposloop + 1)) - 1)
                sw.WriteLine((MyFileText.Item(iloop)).ToString)
                iloop += 1
            Next
            iposloop += 1
            sw.WriteLine(Z1)

            sw.Close()
        Next
 
Last edited by a moderator:
Maybe you should try looking into regular expressions. This can be a very helpful things to learn if you are going to be parsing a lot of text files and things like that. In .NET there are built-in classes for using regex. Maybe look at these links:

http://www.regular-expressions.info/dotnet.html

http://www.codeproject.com/dotnet/RegexTutorial.asp

http://regexlib.com/CheatSheet.aspx


This should be helpful to learn and it is very simple and you could easily accomplish all of that with having to worry about all the splits, loops, startswith's, and endswith's you could enter a few simple regular expressions to chop all that up like you are supposed to.
 
Back
Top