December 1st, 2004

Can I Combine Multiple Text Files Using a Script?

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.

Author

0 comments

Discussion are closed.