Can I Combine Multiple Text Files Using a Script?

Can I Combine Multiple Text Files Using a Script?

  • Comments 5
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! From the command prompt the command copy a.txt+b.txt ab.txt will take the contents of a.txt and the contents of b.txt and combine them into a new file named ab.txt. Can I do the same thing with a script?

-- DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. In yesterday’s column we dealt with text files; more specifically, we talked about how you can use a script to modify .INI files. We told you that the solution wasn’t very elegant, but that it would do the job. Well, we have the same situation here. Can we use a script to combine text files? Yes we can. We have to do it in a slightly cumbersome way, but it’ll work just fine.

The problem we face is that neither WSH nor VBScript has a way to combine text files with a single command (e.g., objFile.AddTextFiles(���file1.log”,”file2.log”). That’s a shame, but it won’t stop us from combining text files; we’ll just have to go through a few extra steps in order to do this. For example, to combine File1.log and File2.log into a single file (which we’ll call Output.txt) we’ll have to first read File1.log and append the contents of that file to output.txt, then we’ll have to read File2.log and append the contents of that file to Output.txt. In fact, we’ll have to use a script that looks a lot like this:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")

Set objTextFile = objFSO.OpenTextFile("c:\logs\file1.log", ForReading)

strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText

Set objTextFile = objFSO.OpenTextFile("c:\logs\file2.log ", ForReading)

strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText

objOutputFile.Close

As you can see, this isn’t exactly rocket science. We start out by defining a constant (ForReading) that we’ll use to open each log file. We then create an instance of the FileSystemObject (the scripting technology used to manipulate text files) and use the CreateTextFile method to create a new file named Output.txt.

After that, we open our first file (C:\Logs\File1.log) for reading. We use the ReadAll method to read in the entire text file and store that information in the variable strText. We close File1.log, then use the WriteLine method to append the information we just read in to our new file, Output.txt. We then repeat the process for the next file (C:\Logs\File2.log). After we’ve read in this second file, Output.txt will consist of all the information found in the first file plus all the information found in the second file. Hey, we did it!

Ok, ok, we know what you’re thinking: sure, the preceding script works, but it also requires you to know - in advance - the names of all the files in the folder C:\Logs. Wouldn’t it be better to have a script that could grab all the files in C:\Logs and then combine them all for us? Hmmm, we never thought about that. You mean something like this:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile("output.txt")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Logs'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In FileList
    Set objTextFile = objFSO.OpenTextFile(objFile.Name, ForReading) 
    strText = objTextFile.ReadAll
    objTextFile.Close
    objOutputFile.WriteLine strText
Next

objOutputFile.Close

All we’re really doing here is getting a collection of all the files in the C:\Logs folder; that’s what this WMI Associators of query does:

Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Logs'} Where " _
        & "ResultClass = CIM_DataFile")

As soon as we have this collection, we use a For-Each loop to open each file and then read in the text (using the ReadAll method, just like we did before). We close the file, and append the text to our output file. We then loop around and repeat this process for the next file in the collection. In no time at all, we’ll have taken all the text from all the files in C:\Logs and combined into a new file named output.txt. It’s that easy.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Here is an alternative solution that worked for me using the command line:

    type *.txt >> output.txt

    Be sure your output.txt file is not created in the same directory that contains all of the other text files or you'll end up with duplicate entries.

  • I'm gonna try this, if it works i will be very grateful!

  • I used this in my MS ACCESS VB code and it worked ok but the combined file had a blank record between each appended file. Any way round this?

  • This is a great script, but what if I want to separate them once I am done. That is, I want to join them, in this case a bunch of HTML files, translate them (separate process), and then split them back up into their separate pieces.

  • How to send the mail with HTML template using vbscript.The body of the message should be the HTML template.

    can you pls send me the answer,how to do..?