Code for merging multiple text files in VBScript _vbs

Source: Internet
Author: User
Ask:
Hey, scripting guy!. At 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 then merge them into a new file named Ab.txt. Can I use a script to do the same thing?

--DL

For:
Hi, DL. In yesterday's column, we explored questions about text files; More specifically, we discussed how to use script modifications. INI file. We mentioned that this solution, though not very ingenious, can achieve its purpose. As far as today's problem is concerned, this is also the case. Can we use scripts to merge text files? Yes, I can. It's just a little cumbersome, but it works fine.

The problem we have is that neither WSH nor VBScript can combine text files with a single command, such as Objfile.addtextfiles ("File1.log", "File2.log"). It's a bit disappointing, but we're not going to let it go, we can still combine text files, but we need to do a few more steps. For example, to merge File1.log and File2.log into a single file (which we name as Output.txt), we need to read the File1.log, append the contents of the file to the end of the output.txt, then read the File2.log, and then The contents of the file are appended to the end of the Output.txt. In fact, we must use a script similar to the following:

Copy Code code 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 FileSystemObject (scripting technology for working with text files) and use the CreateTextFile method to create a new file named Output.txt.

Then, open the first file (C:\Logs\File1.log) to read the contents. We use the ReadAll method to read the entire text file and store the information in the variable StrText. Then, close File1.log and use the WriteLine method to append the information you just read to the end of the new file Output.txt. Next, perform the same procedure for the next file (C:\Logs\File2.log). After the second file is read, Output.txt will contain all the information in the first file and the second file. Haha, we have succeeded!

We know what you're thinking: Yes, although the above script can accomplish the purpose, the problem is that you must "advance" the name of all the files in the folder C:\Logs. Wouldn't it be better to write a script to get all the files in the C:\Logs and merge them together? Well, we haven't thought about it that way. What you are saying is not like the following script:

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 get a collection of all the files in the C:\Logs folder, which is implemented through the following WMI associators query:

Set FileList = objWMIService.ExecQuery _
("Associators of {win32_directory.name= ' C:\Logs '} Where" _
& "ResultClass = Cim_datafile")

After we get this collection, we can immediately open each file with For-each loop and read the text in it (using the ReadAll method, as in the previous code). Then close the file and attach the text to the end of the output file. The next loop executes 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 the C:\Logs and merge them into a new file named Output.txt. The whole process is so simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.