Results 1 to 2 of 2

Thread: Merging .TXT files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Merging .TXT files

    Hey Guys,

    I have been racking the brains trying to think of the best way to programmatically do this, i'm trying to merge 2 .txt files into 1, but the problem is the way i do it a typical file looks like:

    #furniture_field|furniture|etc|etc
    FURNITURE
    #!chairs|chairs[]|etc|etc
    CHAIRS
    #doors_fields|door|etc|etc
    DOORS
    #price|price_tag|new_prices
    PRICE
    etc
    etc

    The macros i.e. (the capitalised fields are pretty much all the same) only the fields above change like:

    #prices|prices_tag|new_price
    PRICE
    #furniture_field|furniture|etc|etc
    FURNITURE
    #doors_field|doors|etc|etc
    DOORS
    #!chair|chair[]|etc|etc
    CHAIRS
    etc
    etc

    The macros aren't always in the same order so i can't even go line by line, ultimately i'm trying to merge them into one file like:

    #prices|prices_tag|new_price|price|prices_tag
    PRICE
    #furniture_field|furniture|etc|etc
    FURNITURE
    #doors_field|doors|etc|etc
    DOORS
    #!chair|chair[]|door|etc|etc
    CHAIRS
    etc
    etc

    The fields are seperated with a "|" so i could split them up and check for duplicates before adding to the master.txt file, so far i have:

    Code:
            Try
    
                '// READ FILE #1
                Dim File1 As New IO.StreamReader(txtBoxXas1.Text)
                Dim FileData1 As String = File1.ReadToEnd()
                File1.Close()
    
                '// READ FILE #2
                Dim File2 As New IO.StreamReader(txtBoxXas2.Text)
                Dim FileData2 As String = File2.ReadToEnd()
                File2.Close()
    
                '// DECLARE VARIABLES
                Dim File1Data As String() = FileData1.Split()
                Dim File2Data As String() = FileData2.Split()
    
                For x = 0 To UBound(File1Data)
    
                    If File1Data(x) <> File2Data(x) Then
    
    
                    Else
    
                    End If
    
                Next
    
            Catch ex As Exception
    
                MessageBox.Show(ex.ToString)
    
            End Try
    This just read one line at a time, i can't think of the best way to do this lol

    any tips or help would be appreciated

    cheers guys

    Graham

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Merging .TXT files

    You code for reading files is buggy, because FileData1.Split() is split by space while you must split by vbNewLine

    try this one
    Code:
            Try
    
                Dim File1Data As String() = IO.File.ReadAllLines(txtBoxXas1.Text) ' READ FILE #1
                Dim File2Data As String() = IO.File.ReadAllLines(txtBoxXas2.Text) ' READ FILE #2
    
                ' get the smallest upper bound to prevent 'out of range' exception inside the for...next 
                Dim intUpperBound As Integer
                If File1Data.Length < File2Data.Length Then
                    intUpperBound = File1Data.Length
                Else
                    intUpperBound = File2Data.Length
                End If
    
                For x = 0 To intUpperBound - 1
                    If File1Data(x) <> File2Data(x) Then
    
                    Else
    
                    End If
    
                Next
    
            Catch ex As Exception
    
                MessageBox.Show(ex.ToString)
    
            End Try



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width