The code of merging multiple text files with VBScript

Time:2021-2-26

Q:
Hi, scripting guy! In the command prompt, you can execute the command “copy” a.txt + b.txt ab.txt ”To extract the contents of ﹣ a.txt ﹣ and ﹣ b.txt ﹣ and merge them into a ab.txt In the new file. Can I use scripts to do the same?

— DL

A:
Hi, DL. In yesterday’s column, we talked about text files; more specifically, we discussed how to use scripts to modify. INI files. We mentioned that this solution, though not very ingenious, can achieve the goal. As far as today’s problem is concerned, the same is true. Can we use scripts to merge text files? Yes, you can. It’s just a little cumbersome, but the effect is very good.

The problem we have is that neither WSH nor VBScript can merge text files with one command. For example, objFile.AddTextFiles (“file1.log”,”file2.log”)。 It’s a bit disappointing, but we won’t stop there. We can still merge text files; it’s just a few more steps. For example, to combine ﹣ file1. Log ﹣ and ﹣ File2. Log ﹣ into one file (we name it Output.txt ), you need to read ﹣ file1.log first, and attach the contents of the file to the output.txt At the end of the file, read the ﹣ file2.log, and then attach the contents of the ﹣ file to the Output.txt At the end. In fact, we have to use a script like this:

Copy codeThe code is as follows:
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, the script is not particularly complex. First, we define a constant (forreading) to open each log file. Next, create an instance of “create file system object” (script technology for processing text files), and use the “create text file” method to create a file named Output.txt New file for.

Then, open the first file (C::? Logs? File1. Log) to read its contents. We use the “read all” method to read in the whole text file, and store the information in the variable “strText”. Then, close ﹣ file1. Log and use the ﹣ writeline ﹣ method to attach the newly read information to the new file Output.txt At the end of. Next, perform the same procedure for the next file (C:: – logs – File2. Log). After reading in the second file, Output.txt Will contain all the information in the first file as well as the second file. Ha ha, we made it!

We know what you’re thinking: Yes, although the above script can achieve the goal, the problem is that you have to know the names of all the files in the folder “C::” logs “in advance. So, wouldn’t it be better to write a script to get all the files in C:: logs and merge them together? Well, we haven’t thought about that yet. Are you talking about a script similar to the following

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

In fact, all we do here is to get a collection of all the files in the C:: logs folder. This is achieved by the following query: WMI associates of

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

After getting this collection, we can immediately use the “for each” loop to open each file and read in the text (using the “read all” method, the same as the code above). Then close the file and attach the text to the end of the output file. Next, a loop is executed to perform the same procedure for the next file in the collection. In just a moment, you can extract all the text from all the files in C: logs and merge them into a single file named output.txt New file for. The whole process is so simple.