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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending Text Files Sequentially

Status
Not open for further replies.

dbtetlow

Programmer
Mar 21, 2001
5
US
Hello,

This probably has already been answered, but I can't seem to find anything that has helped. I need to copy the contents of a number of text files into a single separate file. I really just need to mimic exactly what would happen from a command line -
{copy file1.txt + file2.txt file3.txt}.

Thanks in advance,
Bruce
 
Use the FileScriptingObject to create a new file "for append". You can then read each file into a variable and append it to your new file.

Go to and look in the VBScript/Documentation section for FileSystemObject. This has lots of examples. If you get stuck, let me know.
 
Try something like this:
Code:
Sub AppendFiles()

    Dim sPath As String
    Dim sFiles As Variant
    Dim sNewFile As String
    Dim i As Long
    Dim sTmp As String
    
    sPath = "C:\Temp\"
    sFiles = Array("file1.txt", "file2.txt", "file3.txt")
    sNewFile = "master.txt"
    
    'Open the master file for output
    Open (sPath & sNewFile) For Output As #1
    
    'Append the list of files
    For i = LBound(sFiles) To UBound(sFiles)
        Open (sPath & sFiles(i)) For Input As #2
            Do While Not EOF(2)
                Line Input #2, sTmp
                Print #1, sTmp
            Loop
        Close #2
    Next i
    
    'Close the master file
    Close #1
    
End Sub
 
You could always just do the following as well:
shell ("command /c copy /b file1 + file2 file3")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top