Use the VBS to sort the contents of a text file _vbs

Source: Internet
Author: User
Tags readline
Ask:
Hello, Scripting Guy! I have a text file that contains a list of computer names. How do I sort the file alphabetically?
--LR
For:
Hello, LR. If you want to be lazy, we'll tell you: "Sorry, you can't do that." "We can get away with that, because there's not a single method in Microsoft scripting that can sort files after they've opened a text file. But, hey, when did the Scripting Guys steal laziness?
Oh, yes, we've stolen a few times, but I hope everyone has forgotten. However, this time we will provide you with a solution.
Although there is no way to sort text files directly, we can end up with the same effect by doing the following: 1 reads the file into memory using FileSystemObject, and 2 sorts the files alphabetically in memory; 3) Replaces the existing contents of a file with sorted data in memory. These operations are a bit Shang, but you'll end up with a text file that has been sorted alphabetically, which is exactly the result you want.
To complete this task, we chose to use a disconnected recordset. There are other ways to do this (for example, bubble sort), but disconnected recordsets provide more flexibility and are easier to explain, especially for those who have experience in database programming. Here we will brief you on the disconnected recordset, and for detailed instructions on how it works, you may need to visit the Scripting Week 2 webcast things the Scripting Guys Never told you (what the Scripting Guys never told you )。 (as additional supplemental information, this webcast will also show you how to sort individual file data using bubble sort.) )
Before we begin, we assume that you have a text file that is similar to the following, and that each row of the file has a computer name:
red-ws-02
Atl-ws-01
sf-ws-02
atl-ws-02
atl-ws-03
red-ws-02
Sf-ws-01

How do you read these computer names, sort them, and then write the sorted list back to the text file? We can use a script that is similar to the following:
Const adVarChar = 200
Const maxcharacters = 255
Const ForReading = 1
Const ForWriting = 2

Set DataList = CreateObject ("Ador. Recordset ")
DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters
Datalist.open

Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\Scripts\Computers.txt", ForReading)

Do Until Objfile.atendofstream
StrLine = Objfile.readline
Datalist.addnew
DataList ("ComputerName") = StrLine
Datalist.update
Loop

Objfile.close

Datalist.sort = "ComputerName"

Datalist.movefirst
Do Until datalist.eof
StrText = StrText & DataList.Fields.Item ("ComputerName") & VbCrLf
Datalist.movenext
Loop

Set objfile = objFSO.OpenTextFile ("C:\Scripts\Computers.txt", ForWriting)

objFile.WriteLine StrText
Objfile.close

First, we define a series of constants that are required to create a disconnected recordset. (The disconnected recordset should be treated as a database that exists only in memory and is not related to a physical database that is stored on a disk drive.) We then use the following set of code to create a disconnected recordset consisting of a single field "ComputerName":
Set DataList = CreateObject ("Ador. Recordset ")
DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters
Datalist.open

Next, we use FileSystemObject to open the text file C:\Scripts\Computers.txt. At this point, we are ready to start populating the recordset that we created. What we're going to do is read the text file line-by-row. Each read row, we will use the "AddNew" method to add a new record to the recordset. We set the value of the ComputerName field to the line we just read in the text file (remember that each row of the text file represents a computer name), and then use the Update method to save the record to the recordset. Continue this operation until we have read each line of the text file, and then we will close the file.
Yes, it sounds like a lot of work, but as you can see, all the actions require only the following lines of code:
Do Until Objfile.atendofstream
StrLine = Objfile.readline
Datalist.addnew
DataList ("ComputerName") = StrLine
Datalist.update
Loop

Objfile.close

Next, we need to sort the recordset. This is one of the main benefits of using disconnected recordsets without using bubble sorting or other manual sorting algorithms. Sorting a recordset requires only one line of code to be executed:
Datalist.sort = "ComputerName"

Now that we have a sorted recordset, we need to use that recordset to write the data back to the text file. The easiest way to do this is to traverse the Recordset, crawl each record, and save the entire recordset in a variable. This is the following action:
Datalist.movefirst
Do Until datalist.eof
StrText = StrText & DataList.Fields.Item ("ComputerName") & VbCrLf
Datalist.movenext
Loop

We just take a look at each line record and then store the value of the ComputerName field in the variable strText. Notice how we do this: we set the value of StrText to any value in StrText plus the value of the current ComputerName field, plus a carriage return line break (which is exactly what the VBCRLF constant represents). We'll end this operation with a variable named StrText in memory that contains the following data:
Atl-ws-01
atl-ws-02
atl-ws-03
red-ws-02
red-ws-02
Sf-ws-01
sf-ws-02

Finally, reopen the text file (this time for writing) and use the "WriteLine" method to replace the existing content with the StrText value. Because the value of StrText is the sorted list of computer names, we complete the alphabetical order of the contents of the C:\Scripts\Computers.txt.

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.