SoFunction
Updated on 2025-04-08

Key Wizard Script - A good tutorial for learning VBS

Today I will introduce VBS in general, hoping to give you an intuitive impression. In addition, the explanation is combined with the key elves tutorial (made by Li Yue) on the key official website to help everyone understand this thing.
1. Overview
To let a computer do things, people must give it instructions. The original instructions were very complicated, all of them were 0011001, and later they were gradually simplified, and the instructions were closer to human language. VBS (Microsoft(R) Visual Basic(R) Scripting) is also a high-level language. Compared with other high-level languages, it is simple and easy to learn and is a good entrance for computer beginners to get involved in the field of programming.
If the evolution from low-level languages ​​to high-level languages ​​is a strategic issue for humans to control computers, then the specific setting of variables and loops is a specific tactical issue for controlling computers. What we are learning now is the tactic of using VBS to make computers work for us.
2. Start with the simplest things.
Online hawking script:
vbs i
vbs i=10
while i>0
SayString sells XXX, the price is negotiable.
vbs i=i-1
delay 10000
endfor
Many people sneer when they see this script. There are many ways to achieve constant hawking, some of which are several times easier than this script. But as the explanation progresses, we will find that what VBS provides us with is a powerful tool. Let’s first look at what these lines mean.
vbs i //Tell the computer that there is a variable called i.
vbs i=10 //This variable is 10
while i>0 //When this variable is greater than 0, execute it until you see the first endfor.
SayString sells XXX, the price is negotiable. //Key statement: output text
vbs i=i-1 //This variable is subtracted by 1, and then the number after decrementation by 1 is used as the value of i.
delay 10000 //Key statement: Pause for 10 seconds (10000 milliseconds)
endfor //Back to while
Effect: It ends after 10 times.
Now I want to take some effort to explain why I have to make so many troublesome things.
The use and importance of variables
Actually, this program can be written as
SayString sells XXX, the price is negotiable.
delay 10000
SayString sells XXX, the price is negotiable.
delay 10000
...Repeat N times
SayString sells XXX, the price is negotiable.
delay 10000
In fact, no matter how complex the program segment is, it can be restored to sequential programs. Using many complex things is not to show programming capabilities, but to give yourself convenience.
When we keep selling XXX, it may not feel anything, but when we sell YYY? Do you want to correct them all? Or we have to repeat 1,000 times, and then only repeat 500 times. Do we have to count 500 carefully and then delete the following one? Of course, it is quite comfortable to modify this mini program. But to improve development, we have to make a bigger program, and we must also rely on our variable friends.
Variables are actually a character code, just like your and my names, in order to distinguish each variable and let them work separately. When naming variables, you can use numbers, characters, and underscores, but you cannot use Chinese characters, spaces and other strange symbols. In addition, numbers cannot be used for the first character.
Before using variables, you must first write a statement like vbs i, which means that I now assign a variable, named i, and participate in the running of this program. In fact, you don’t have to perform such an registration program and use i directly somewhere in the program, but practice has proved that this is a bad habit, making it difficult for you to move forward when writing large programs.
Then vbs i=10 tells the program that the current value of this variable i is 10. In the future, wherever i is used, it is equivalent to 10 there. For example, vbs j=i+1 is actually equivalent to j=10+1, so the value of j is 11.
The charm of a variable is not only that it can replace numbers, but also characters, but compared to numbers, characters have their own special features.
for example:
vbs i="character"
vbs j="surface"
vbs a=i & j
At this time a is a string concatenation. Notice the two new symbols that appear here: "sign and & symbol. Someone may have guessed their role." The content between the signs is a character. The computer has a completely incomprehensible understanding of vbs i=10 and vbs i="10". The & number is a string concatenation number. If a=j & i, then a is the concatenated character.
After knowing the usage of variables, we can change the hawking procedure to be more interesting. For example, if we want the program to say different things, we will first say sell XXX 10 times, and then say sell YYY 10 times. Then we can write it as:
vbs i
vbs i=10
while i>0
ifexpression i<=5
SayString sells XXX, the price is negotiable.
endif
ifexpression i>5
SayString sells YYY, the price is negotiable.
endif
vbs i=i-1
delay 10000
endfor
In fact, we used a small programming technique, which is to use the size of the variable i to control the direction of the program. It can be seen that the first 5 times sold YYY and the last 5 times sold XXX.
Or we call XXX once, then YYY once, and then repeat the previous action:
vbs i
vbs i=10
while i>0
ifexpression i mod 2 = 0
SayString sells XXX, the price is negotiable.
endif
ifexpression i mod 2 = 1
SayString sells YYY, the price is negotiable.
endif
vbs i=i-1
delay 10000
endfor
Here is also a method of using i to control the running of the program. mod is the same operator symbol as +, -, and *, and the function is to take the remainder. For example, 10 mod 2 = 0 means 10 divided by 2, and the rest is 0. Similarly, 9 mod 2 = 1, 8 mod 2 = 0, 7 mod 2 = 1, 6 mod 2 = 0, 5 mod 2 = 1…
Oh, that's all for now.

In fact, this script is a script under the key elves. I'm sorry, if you want to read the vbs tutorial, I suggest you download onevbscript manual, and then read the articles posted in my vbs column in the early stage. At the beginning, I gave more basics.
https:///books/
Microsoft's official Vbscript reference manual