Code for combining multiple text files with vbscript

Source: Internet
Author: User

Q:
Hi, Scripting Guy! In the command prompt, you can run the "copy a.txt0000b.txt AB .txt" command 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 perform the same operation?

-- DL

A:
Hi, DL. In yesterday's column, we discussed the issues related to text files. More specifically, we discussed how to use scripts to modify the. INI file. We mentioned that this solution is not very clever, but can achieve the goal. This is also the case for today's problems. Can we use scripts to merge text files? Yes, yes. It is a little tedious, but the effect is good.

The problem we encounter is that WSH and VBScript cannot use a command to merge text files, for example, objFile. AddTextFiles ("file1.log", "file2.log "). This is a bit disappointing, but we won't stop it. We can still merge text files, but we only need to perform several more steps. For example, to merge File1.log and File2.log into a file (we name it Output.txt), read File1.log, append the content of the file to the end of output.txt, and then read File2.log, then, append the content of the file to Output.txt. In fact, we must use a script similar to the following:

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, this script is not very complex. First, we define a constant (ForReading) to open each log file. Next, create a FileSystemObject (script technology used to process text files) instance, and use the CreateTextFile method to create a new file named Output.txt.

Open the first file (C: \ Logs \ File1.log) to read the content. We use the ReadAll method to read the entire text file and store the information in the variable strText. Disable File1.log and use the WriteLine method to append the newly read information to the end of the new file Output.txt. Next, execute the same process for the next file (C: \ Logs \ File2.log. After reading the second file, output.txt will contain all the information in the first file and the second file. Haha, we have succeeded!

We know what you are thinking: Good, although the above script can achieve the purpose, but the problem is that you must "Know the name of all the files in the C: \ Logs folder in advance. So it is better to write a script to get all the files in C: \ Logs and merge them together? Well, we never thought so. Is the script similar to the following:

Const ForReading = 1

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

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

Set FileList = obw.miservice. 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 the operations we perform here are to obtain the collection of all the files in the C: \ Logs folder; this is achieved through the following WMI Associators query:

Set FileList = obw.miservice. ExecQuery _
("Associators of {Win32_Directory.Name = 'C: \ logs'} Where "_
& "ResultClass = CIM_DataFile ")

After obtaining this set, we can immediately use the For-Each loop to open Each file and read the text (using the ReadAll method, the same as the code above ). Close the file and append the text to the end of the output file. Next, execute a loop to execute the same process for the next file in the set. Just a moment, you can extract all the text of all the files in C: \ Logs and combine 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.