Write a VBS script that can open a text file and disrupt the order of words found in the file _vbs

Source: Internet
Author: User
Tags readline
Ask:
Hello, Scripting Guy! My daughter has a flash-cassette program that can get a list of words in a text file. How can I write a script that opens the file and automatically disrupts the order of the word list?
--SN
For:
Hello, SN. You know, in most cases, we try to keep this column focused on the actual system administration tasks: We tell you how to set a default printer, how to disable a service, and how to map a network drive. However, sometimes we are willing to deal with a question that sounds interesting, and we don't think about how useful the end result is. Do system administrators usually need to disrupt the order of word lists in a text file? may not be needed. But, if only work, do not play, presumably the smart child will become stupid, right?
In fact, some children are still a little silly even if they play. But that's another thing.
With that in mind, let's take a look at the script that disrupts the order of words in the text file. As we said, this kind of work may not be useful in practice, but it is a bit challenging, and it does require us to use a little interesting scripting tips. And if that's not the case, you might never know when the trick will come in handy.
First, suppose you have a text file similar to the following, in which all the words in the file are sorted alphabetically:
Apple
Banana
Carrot
Dog
Fish
Elephant
Giraffe
Horse
How can you disrupt the order of these words? Use a script such as the following:
Copy Code code as follows:

Const ForReading = 1
Const ForWriting = 2
Set objdictionary = CreateObject ("Scripting.Dictionary")
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\scripts\words.txt", ForReading)
i =-1
Do Until Objfile.atendofstream
StrLine = Objfile.readline
Objdictionary.add StrLine, StrLine
i = i + 1
Loop
Objfile.close
Dim ArrWords ()
Redim ArrWords (i)
Intwordsleft = i
z = 0
Do While Intwordsleft >= 0
Randomize
Rndword = Int ((intWordsLeft-0 + 1) * Rnd + 0)
Intwordsleft = intWordsLeft-1
Colitems = Objdictionary.items
StrText = Colitems (Rndword)
ArrWords (z) = StrText
Z = z + 1
Objdictionary.remove (StrText)
Loop
Set objfile = objFSO.OpenTextFile ("C:\scripts\words.txt", ForWriting)
For each stritem in ArrWords
objFile.WriteLine stritem
Next
Objfile.close
Oh, of course, this looks a little weird; it's a really incredible errand (at least for the system administrator). However, whether you believe it or not, there is logic in it, and we will gradually let you understand this logic.
The first part of the script is really quite simple. We define a pair of constants-ForReading and ForWriting, and we'll use these two constants when working with text files. Then create two objects: Scripting.Dictionary and Scripting.FileSystemObject. We will use the Dictionary object as a temporary repository for the words read from the text file, and use FileSystemObject to actually interact with the text file.
Next, use the OpenTextFile method to open the file C:\Scripts\Words.txt for reading. (Note the use of constant ForReading.) Next, create a counter variable I and set its value to-1; We will use this variable to track the number of words in the file. Why should I start with-1 instead of 0? Because we're going to use I to build an array, and since the first item in the array is always 0 (instead of 1), we need to start with-1. When we read the first word, I will be set to 0, although it may sound strange, but an array of size 0 means that the array contains a data item.
Hi, we just report truthfully, but we don't know why.
Next is the following code:
Copy Code code as follows:

Do Until Objfile.atendofstream
StrLine = Objfile.readline
Objdictionary.add StrLine, StrLine
i = i + 1
Loop

What we do here is read the file line by row. For each line in the file (that is, each word), we assign the value to a variable named StrLine, and then use the Add method to add the value to the Dictionary object, thereby adding the I value by 1. After the file is read, all the words are stored in the Dictionary object, I will be 7, which happens to be the number of words in the file minus 1. Why Because the size of the array containing 8 items of data is 7. )
Don't worry; In the end you'll understand why.
We hope so.
After the file is closed, initialize the array arrWords and set its size to I (which means that the number of words in the text file is minus 1). At the same time assign the I value to the variable intwordsleft, through which we will know how many words have to be disturbed in order. Finally, the value of the variable z is set to 0; we will use Z to populate the array with words that have been scrambled. In fact, all we have to do is take a random word out of the Dictionary and add it to the array. Because words are taken out of the Dictionary in a random order, their order in the array is "scrambled" (stored in different order).
Now, here's the interesting part. Create a Do loop that runs until we run out of all the words in the Dictionary object. Next, use the following two lines of code to randomly select a number from the 0 and Dictionary objects (or at least the actual number of data items by 1, because the first item in the Dictionary object is Item No. 0):
Ranndomize
Rndword = Int ((intWordsLeft-0 + 1) * Rnd + 0)
Then we subtract the Intwordsleft value by 1; This is to keep in mind that the words we're dealing with now are less than the one we handled earlier.
So what do we need this random number for? All we have to do now is use this value to randomly extract a word from the Dictionary. To do this, we can create a collection of Dictionary items and then store the values of this randomly selected data item number in the variable strText:
Colitems = Objdictionary.items
StrText = Colitems (Rndword)
In other words, Banana is currently the 1th item in Dictionary. Suppose you get 1 when you generate random numbers. This means that we will remove the value of item 1th from Dictionary, and then it means that the word Banana is stored in the variable strText.
Do you understand? After a word is randomly removed from the Dictionary, we need to store that value somewhere. For this we use array arrWords to make StrText the first item in the array:
Words (z) = StrText
How do we know that the value should be the first item? Because we assign the value to the z item, and the Z item equals 0. After that, we are going to 1,z Z Plus will be equal to 1. This also means that by looping the next time we will assign the retrieved value to the second item in the array.
After the word Banana is used, the next step is to remove the word from the Dictionary, otherwise we may use the word again. To remove this word, simply call the Remove method of the Dictionary object and pass the variable StrText as the item to be deleted:
Objdictionary.remove (StrText)
After everything is finished, the array arrWords will contain a list of the scrambled words that are taken out of the text file:
Banana
Elephant
Giraffe
Apple
Fish
Carrot
Horse
Dog
That's good, isn't it? We then simply open the file Words.txt (this time for writing) and then replace the existing content with the scrambled list arrWords:
Copy Code code as follows:

Set objfile = objFSO.OpenTextFile ("C:\scripts\words.txt", ForWriting)
For each stritem in ArrWords
objFile.WriteLine stritem
Next
Objfile.close
The next time your daughter (who has the same daughter) runs the educational program, what she sees will be a random display of words.
By the way, we didn't test the script for the text of this column as it was rumored to be. At hello, Scripting Guys! The words in the column are not randomly chosen; every word is what we spend countless hours doing hard work and exploring.
In fact, just after we have done all the earnest and meticulous work. Our editors randomly disrupt the order of all the words. If you could see the way the column was edited, you'd be amazed at its beauty! (Edit note: What you see is what happens before this edit: it surprises you.) Use not but the word this I "wonderful". )

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.