Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

dgr7

IS-IT--Management
Dec 29, 2006
43
US
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
 
Code:
[blue]With CreateObject("scripting.filesystemobject")
        .CreateTextFile("c:\text3.txt").Write .OpenTextFile("c:\text1.txt").ReadAll & .OpenTextFile("c:\text2.txt").ReadAll
    End With[/blue]
 
Look at Open and Write.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Although strongm answer is quicker and shorter. [smile]

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
this code I was given by a user on another site works well for me, at least it did well in a test run with the largest text files I'll probably be doing with.
thanks,
david
Code:
Sub JoinFiles(ByVal File1 As String, ByVal File2 As String, ByVal SaveAs As String)
    Dim intFF As Integer, strBuffer As String
    Dim lonL1 As Long, lonL2 As Long
    
    lonL1 = FileLen(File1)
    lonL2 = FileLen(File2)
    
    'Allocate space for the 2 files.
    strBuffer = Space$(lonL1 + lonL2)
    
    intFF = FreeFile
    
    Open File1 For Input As #intFF
        Mid$(strBuffer, 1, lonL1) = Input(LOF(intFF), intFF)
    Close #intFF
    
    intFF = FreeFile
    
    Open File2 For Input As #intFF
        Mid$(strBuffer, lonL1 + 1) = Input(LOF(intFF), intFF)
    Close #intFF
    
    intFF = FreeFile
    
    'Write new file.
    Open SaveAs For Output As #intFF
        Print #intFF, strBuffer
    Close #intFF
    
    strBuffer = ""
End Sub
 
You didn't like the filesystemobject solution given here, then?
 
I haven't tested that solution yet, had to get the code in production ASAP and the JoinFiles one was appealing to me at the time.
thanks,
david
 
And the prize for shortest answer of all goes to
Code:
Shell "cmd /c copy c:\text1.txt + c:\text2.txt c:\text3.txt", vbHide
Although it may be abysmally slow...but it sure is cute. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top