In the previous article, we learned some basic knowledge about VBS programming. To learn more deeply, we also need to learn the selection structure and loop structure.
Select Structure
Select the structure, that is, to determine the conditions, and then select the structure of different branches based on the judgment results. The IF and Select Case statements are common.
● IF Condition Statement
IF condition statements have multiple forms, such as If... Then, If... Then... Else, If... Then... ElseIf. The following describes how to use routines. Write a VBS script in a notebook:
Dim a, B
A = inputbox ("Enter the first number", "enter ")
B = inputbox ("enter the second number", "enter ")
If a> B then
MsgBox ("the first number is greater than the second number ")
End if
After running, the user is required to enter two numbers. The value of the first number is assigned to a, and the value of the second number is assigned to B. Then the program automatically judges a and B, if the value of a is greater than the value of B, a prompt box is displayed, showing "the first number is greater than the second number ". Note: if the statement after then is not in the same row as then, add an End if line to End after the statement ends. Otherwise, the program ends. If the row is the same, you do not need to add End if. The preceding statement can be changed:
Dim a, B
A = inputbox ("Enter the first number", "enter ")
B = inputbox ("enter the second number", "enter ")
If a> B then MsgBox ("the first number is greater than the second number ")
In another program, multiple If statements can be used. We can improve the above program as follows:
Dim a, B
A = inputbox ("Enter the first number", "enter ")
B = inputbox ("enter the second number", "enter ")
If a> B then
MsgBox ("the first number is greater than the second number ")
End if
If a = B then
MsgBox ("the first number equals the second number ")
End if
If a <B then
MsgBox ("the first number is smaller than the second number ")
End if
We can also use other IF statements... Then... Else and If... Then .... Elseif to achieve the same effect, as follows:
Dim a, B
A = inputbox ("Enter the first number", "enter ")
B = inputbox ("enter the second number", "enter ")
If a> B then
MsgBox ("the first number is greater than the second number ")
Else
IF a = B then
MsgBox ("the first number equals the second number ")
Else
MsgBox ("the first number is smaller than the second number ")
End if
End if
――――――――――――――――――――――――――――――――――――――
Dim a, B
A = inputbox ("Enter the first number", "enter ")
B = inputbox ("enter the second number", "enter ")
If a> B then
MsgBox ("the first number is greater than the second number ")
Elseif a = B then
MsgBox ("the first number equals the second number ")
Else
MsgBox ("the first number is smaller than the second number ")
End if
● Select Case statement
The Select Case statement is another form of multi-branch structure. It has the characteristics of intuitive representation, simple structure, and is not easy to cause confusion. Its Syntax format is as follows:
Select Case variable or expression
Case expression 1
Statement 1
Case expression 2
Statement 2
End Select
Now we use the Select Case statement to write a program that converts numbers to English. Let's go !!!
Dim
A = inputbox ("enter a number", "enter ")
Select Case
Case 1
MsgBox ("the number you entered is One ")
Case 2
MsgBox ("the number you entered is Two ")
Case 3
MsgBox ("the number you entered is a Tree ")
Case 4
MsgBox ("the number you entered is Four ")
End Select
From the above, do you think the Select Case statement is very intuitive? If you use the If statement, it will be very confusing and even confuse people. Note that no matter how many branches the Condition Statement has, as long as one branch is executed, the remaining branches will not be executed.
Loop Structure
● For Loop statements
The For statement is used to control the structure of a known loop. The statement is as follows:
For Loop Variable = initial value To end value [step size]
Statement
[Exit for] 'Exit loop statement
Next
Now we write a program to calculate the integers and integers from 1 to 100, to gain an in-depth understanding of the usage of For. The program content is as follows:
Dim s
S = 0
For 1 to 100
S = s + I
Next
Msgbox (s)
This program is also one of the classic programs. It first declares a variable S, and then assigns an initial value 0 to S, for I = 1 to 100, so that I can add 1 to each loop, the statement s = s + I in the loop body also accumulates with the increase of I, and ends the loop when it is added to I = 100. The Next statement puts the loop body into the Next loop.
● Do... Loop statement
For Loop statements are used to control loops with known cycles, while Do... Loop is a loop used to control the unknown number of times and control the loop according to the set conditions. It has two forms: first judgment and then execution, and second judgment.
First judge and then execute the syntax format:
Do [while/until condition]
Statement
[Exit Do] 'Exit the loop
Loop
Execute the command first and then judge the syntax format:
Do
Statement
[Exit Do]
Loop [while/until condition]
Here we use Do... Loop statement to rewrite the above program to calculate the integer sum of 1 to 100, as follows:
Executed first and then executed
Dim s
S = 0
I = 0
Do
I = I + 1
S = s + I
Loop until I> = 100
Msgbox (s)
First judge and then execute:
Dim s
S = 0
I = 0
Do while s <100
I = I + 1
S = s + I
Loop
Msgbox (s)
These two procedures are simple and easy to understand. I will not go into detail here.
At this point, the important Syntax structures and statements of VBS have been roughly learned. to study them in a more in-depth and systematic manner, it is best to look for relevant books. Next we will officially start the practical phase of VBS programming Internet cafes.
Practice
Before "practice", let's first understand what Shell objects and WSH are. Shell objects are Microsoft's encapsulation of some common Shell operation functions. Such as opening or browsing a folder, searching for a file, and shutting down a computer. In addition, you can browse the entire file system and obtain various information about a file or folder. Because these packages are built on the foundation of OLE automation services, we can easily use them in VB, Web, and VBS. WSH is an abbreviated form of Windows Script Host. WSH is a Script command on the Windows platform. It has powerful functions and uses JS and VBS scripting languages to achieve its outstanding functions, in addition to modifying the registry, it can also access Excel files and communicate with the Internet. Of course, its biggest advantage is that it can communicate with the operating system, modifying the registry is only the tip of the iceberg that communicates with the operating system. It has many advantages and practicability and is favored by many Windows users.
As mentioned above, WSH can modify the system registry using VBS, but you must first create an object that can communicate with the operating system, and then use various methods of the object to operate the registry, the method and format for creating this object are as follows:
Dim OperateReg
Set OperateReg = WScript. CreateObject ("WScript. Shell ")
The first line declares a variable, while the second line uses the declared variable to create a Shell object OperateReg that can communicate with the operating system.
Next we will use Shell objects to close some services that will bring security risks to Internet cafes.
Disable default share
Set ds = createobject ("wscript. shell ")
Ds. run "net stop server/y"
Disable the WMI Service
Set swmi = createobject ("wscript. shell ")
Swmi. run "net stop wmI/y"
Disable a GUEST user
Set sg = createobject ("wscript. shell ")
Sg. run "net user guest/active: NO"
Disable telnet Service
Set st = createobject ("wscript. shell ")
St. run "net stop telnet"
Use VBS to achieve BAT latency and no black screen
If we want to run the command bar after starting the instance. if the bat batch file is in the remote server \ new \, create a batch under the C root directory of the client and name it run. bat, the content is as follows:
Ping 127.0.0.1-n 10 (10 indicates the number of seconds you want to delay and set it as needed)
Call \ server \ new \ bar. bat
Save and write a text in the same directory.
Set nb = createobject ("wscript. shell ")
Nb. run "run. bat/start", 0
Save the file as a VBS file and add the file to the startup Item. Restart the BAT delay and black screen.
The above are some simple VBS programming. If you can master VBS, you will find that it is very powerful, and many complex functions can be implemented through it. Nowadays, many network administrators are happy to use VBS programming to manage machines in Internet cafes. The three articles in this series only provide a simple understanding of VBS and examples. If you want to learn more deeply, you need to work hard and accumulate. Finally, I hope you can learn VBS as soon as possible.