Results 1 to 10 of 10

Thread: code to read two text files, concatenate one after the other, output to new text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    8

    code to read two text files, concatenate one after the other, output to new text file

    hello,
    what would be the code that would allow me to read in two text files, concatenate the second one to the end of the first one, then output the file as a new third text file?
    thanks in advance,
    david

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: code to read two text files, concatenate one after the other, output to new text file

    it's pretty simple, you could just search the form. however, here is what would work for you:
    vb Code:
    1. Private Sub Form_Load()
    2.   Dim File1 As String, File2 As String
    3.   File1 = StoreFile("F:\file1.txt")
    4.   File2 = StoreFile("F:\file2.txt")
    5.   SaveFile "F:\file3.txt", File1 & File2
    6. End Sub
    7. Private Function StoreFile(FilePath As String) As String
    8.   Dim strTemp As String, strReturn As String
    9.   Open FilePath For Input As #1
    10.     Do Until EOF(1)
    11.       Input #1, strTemp
    12.       strReturn = strReturn & strTemp & vbCrLf
    13.     Loop
    14.   Close #1
    15.   StoreFile = strReturn
    16. End Function
    17. Private Sub SaveFile(FilePath As String, Contents As String)
    18.   Open FilePath For Output As #1
    19.     Print #1, Contents
    20.   Close #1
    21. End Sub
    if "F:\fileX.txt" (where X is 1 or 2) don't exist, you'll get a run-time error, so you might want to change the path/add error checking.

  3. #3
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Re: code to read two text files, concatenate one after the other, output to new text file

    try this.

    create a form with a textbox and a button and put this code.

    Private fso As New FileSystemObject
    Private strName As String

    Private Sub Command1_Click()
    readfile1
    readfile2
    End Sub

    Private Sub readfile1()
    strName = "c:\file1.txt"
    With fso
    Set strm = .OpenTextFile(strName, ForReading)
    With strm
    Do Until .AtEndOfStream
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Loop
    End With
    End With
    Exit Sub
    End Sub
    Private Sub readfile2()
    strName = "c:\file2.txt"
    With fso
    Set strm = .OpenTextFile(strName, ForReading)
    With strm
    Do Until .AtEndOfStream
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Text1.Text = Text1.Text & .ReadLine
    Loop
    End With
    End With
    Exit Sub
    End Sub

    Private Sub createnewfile()
    Open App.Path & "\log_erros\log_erros.txt" For Output As #1
    Print #1, Text1.TabIndex
    Close #1
    End Sub

    it helped???

  4. #4
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: code to read two text files, concatenate one after the other, output to new text file

    This works too. It reads the contents of two files (1.txt and 2.txt) upon a button click. It then creates a new file Results.txt and writes the appended content from both files. All files in code below aer located in the same directory as the application.

    Hope it helps!

    Code:
    Private Sub Command1_Click()
        Dim FilePath1 As String
        Dim FilePath2 As String
        Dim OverallContent As String
            FilePath1 = App.Path & "\1.txt"
            FilePath2 = App.Path & "\2.txt"
            OverallContent = ReadFileContents(FilePath1)
            OverallContent = OverallContent & ReadFileContents(FilePath2)        
            OutputResults (OverallContent)       
    End Sub
    
    'Return file contents
    Private Function ReadFileContents(filepath As String) As String
        Dim content As String
        Open filepath For Input As #1
            On Error GoTo errorrk
            Input #1, content
    errorrk:
        Close #1
        
        ReadFileContents = content
    
    End Function
    Private Sub OutputResults(OverallContent As String)
        On Error GoTo EndSub
        Open App.Path & "\Result.txt" For Output As #1
            On Error GoTo errorrk
                Write #1, OverallContent
    errorrk:
        Close #1
    EndSub:
    End Sub

  5. #5
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: code to read two text files, concatenate one after the other, output to new text

    For smaller text files this will work without being slow. For real large ones you might want to use a different method because this will load the entire 2 files into memory then save to a new one. Test it out and see how it performs.

    I wrote it kinda quickly so I hope there aren't any bugs.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub JoinFiles(ByVal File1 As String, ByVal File2 As String, ByVal SaveAs As String)
    4.     Dim intFF As Integer, strBuffer As String
    5.     Dim lonL1 As Long, lonL2 As Long
    6.    
    7.     lonL1 = FileLen(File1)
    8.     lonL2 = FileLen(File2)
    9.    
    10.     'Allocate space for the 2 files.
    11.     strBuffer = Space$(lonL1 + lonL2)
    12.    
    13.     intFF = FreeFile
    14.    
    15.     Open File1 For Input As #intFF
    16.         Mid$(strBuffer, 1, lonL1) = Input(LOF(intFF), intFF)
    17.     Close #intFF
    18.    
    19.     intFF = FreeFile
    20.    
    21.     Open File2 For Input As #intFF
    22.         Mid$(strBuffer, lonL1 + 1) = Input(LOF(intFF), intFF)
    23.     Close #intFF
    24.    
    25.     intFF = FreeFile
    26.    
    27.     'Write new file.
    28.     Open SaveAs For Output As #intFF
    29.         Print #intFF, strBuffer
    30.     Close #intFF
    31.    
    32.     strBuffer = ""
    33. End Sub
    34.  
    35. 'Using the sub.
    36. Private Sub Form_Load()
    37.     'Joins 1.txt and 2.txt and saves it as NewFile.txt
    38.     JoinFiles "C:\1.txt", "C:\2.txt", "C:\NewFile.txt"
    39. End Sub

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: code to read two text files, concatenate one after the other, output to new text file

    or how about the lazy version of DigiRev's code:
    Code:
    Private Sub JoinFiles(ByRef File1 As String, ByRef File2 As String, ByRef SaveAs As String)
        Dim intFF1 As Integer, intFF2 As Integer, intFF3 As Integer
        
        intFF1 = FreeFile
        Open File1 For Input As #intFF1
        
        intFF2 = FreeFile
        Open File2 For Input As #intFF2
        
        intFF3 = FreeFile
        Open SaveAs For Output As #intFF3
            Print #intFF3, Input(LOF(intFF1), #intFF1) & vbCrLf & Input(LOF(intFF2), #intFF2)
        
        Close #intFF1, #intFF2, #intFF3
    End Sub

  7. #7
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: code to read two text files, concatenate one after the other, output to new text

    Quote Originally Posted by bushmobile
    or how about the lazy version of DigiRev's code:
    Code:
    Private Sub JoinFiles(ByRef File1 As String, ByRef File2 As String, ByRef SaveAs As String)
        Dim intFF1 As Integer, intFF2 As Integer, intFF3 As Integer
        
        intFF1 = FreeFile
        Open File1 For Input As #intFF1
        
        intFF2 = FreeFile
        Open File2 For Input As #intFF2
        
        intFF3 = FreeFile
        Open SaveAs For Output As #intFF3
            Print #intFF3, Input(LOF(intFF1), #intFF1) & vbCrLf & Input(LOF(intFF2), #intFF2)
        
        Close #intFF1, #intFF2, #intFF3
    End Sub


    Didn't think of doing it like that. That's actually probably a better way to do it.

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: code to read two text files, concatenate one after the other, output to new text file

    Another option, the good ol' DOS Copy command.

    Code:
    Private Sub Command1_Click()
        Dim strCmd As String
        
        strCmd = "cmd /C copy c:\test1.txt + c:\test2.txt c:\test3.txt /B"
        
        Shell strCmd
    
    End Sub

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: code to read two text files, concatenate one after the other, output to new text file

    Here's how I would do it:
    Code:
    Dim TextFile1 As String, TextFile2 As String
    Private Sub Form_Load()
    Open "FirstFile" For Binary As #1
    TextFile1 = Space(LOF(1))
    Get 1, , TextFile1
    Open "SecondFile" For Binary As #2
    TextFile2 = Space(LOF(2))
    Get 2, , TextFile2
    Open "TargetFile" For Binary As #3
    Put 3, , TextFile1 & TextFile2
    Close
    End Sub
    DOS might be able to outrun this but not by much.
    Doctor Ed

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    8

    Re: code to read two text files, concatenate one after the other, output to new text file

    I think digirev's solution is going to work for me, at least it did well in a test run with the largest text files I'll probably be doing with.
    thanks,
    david

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