Flexible Practical VBS Introduction Tutorial application Article _vbs

Source: Internet
Author: User
Tags case statement
In the last article, we learned some basic knowledge of VBS programming, to learn more deeply and to choose the structure and cycle structure.

Select structure

Select the structure, that is, to judge the condition, and then according to the results of the judgement, choose to perform different branches of the structure. Common if conditional statements and select Case statements.

If conditional statement
The IF conditional statement has several specific forms, such as if ... Then, If ... Then ... Else, If ... Then ... ElseIf. The following is a separate use case description. Write a VBS script with a notebook that reads:

Dim a,b
A=inputbox ("Enter first number", "input")
B=inputbox ("Enter second number", "input")
If A>b Then
MsgBox ("first number greater than second number")
End If

After running, requires the user to enter two digits, the value of the first number is assigned to a, the value of the second number is assigned to B, and then the program automatically determines a and B, and if the value of a is greater than the value of B, a pop-up message box appears, showing "first number is greater than second Note: The statement after the then is not the same line as the then, the judgment statement should be followed by a row end if to end, or the program will end. If it is the same line, the end if is not added, as the above statement can be changed to read:
Dim a,b
A=inputbox ("Enter first number", "input")
B=inputbox ("Enter second number", "input")
If A>b then MsgBox ("first number greater than second number")
Another program can use more than one if statement, we can improve the above procedure is as follows:
Dim a,b
A=inputbox ("Enter first number", "input")
B=inputbox ("Enter second number", "input")
If A>b Then
MsgBox ("first number greater than second number")
End If
If A=b Then
MsgBox ("First number equals second number")
End If
If A<b Then
MsgBox ("first number less than second number")
End If
We can also change the other statement if ... Then ... Else and if ... Then....elseif to achieve the same effect as follows:
Dim a,b
A=inputbox ("Enter first number", "input")
B=inputbox ("Enter second number", "input")
If A>b Then
MsgBox ("first number greater than second number")
Else
IF A=b Then
MsgBox ("First number equals second number")
Else
MsgBox ("first number less than second number")
End If
End If
――――――――――――――――――――――――――――――――――――――
Dim a,b
A=inputbox ("Enter first number", "input")
B=inputbox ("Enter second number", "input")
If A>b Then
MsgBox ("first number greater than second number")
Elseif A=b Then
MsgBox ("First number equals second number")
Else
MsgBox ("first number less than second number")
End If
  
Select Case Statement
The Select case statement is another representation of the multiple branching structure, which has an intuitive feature, simple structure, and is not easy to cause confusion, and its grammatical form 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 number into the English program, to experience the merits of the Select Case, let ' s go!!!

Dim a
A=inputbox ("Enter a number", "input")
Select Case A
Case 1
MsgBox ("You enter the number of English is one")
Case 2
MsgBox ("You enter the number of English is two")
Case 3
MsgBox ("You enter the number of English is Tree")
Case 4
MsgBox ("You enter the number of English is four")
End Select

From the above, is not that the Select Case statement is very intuitive, if you use the IF statement, it will be very confusing, and even to see people are dizzy. Also note that no matter how many branches the conditional statement has, as long as one branch executes, the rest of the branch is no longer executing.
  
Loop structure

For Loop statement
The For statement is used to control the looping structure known for the number of cycles, as follows:
For loop variable = initial to end value [step size]
Statement
[Exit for] ' Exit Loop statement
Next
Now we write a program that calculates integers from 1 to 100 and goes deep into the use of for, and the program reads as follows:

Dim s
S=0
For I=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 give s an initial value 0,for i=1 to 100 statement let I with each cycle plus 1, the circulation body of the statement s=s+i also with the increase of I, add to the i=100, the end of the loop. The next statement lets the loop body go to the next loop.
  
Do...loop Loop statement
The FOR Loop statement is used to control loops known to cycle times, while Do...loop is used to control cycles that are unknown and to control loops according to set conditions. It has two forms, one is to judge before execution, the other is to execute before judgment.
First judge after the execution of the grammar form:
Do [while/until conditions]
Statement
[Exit do] ' Exit loop
Loop
  
After execution, the grammar form is judged first:
Todo
Statement
[Exit do]
Loop [While/until condition]
Here we use do ... Loop statement to overwrite the above calculation 1 to 100 integer and the program, as follows:

First execute the post-judgment
Dim s
S=0
I=0
Todo
I=i+1
S=s+i
Loop until i>=100
MsgBox (s)

First to judge after the executive type:
Dim s
S=0
I=0
Do While s<100
I=i+1
S=s+i
Loop
MsgBox (s)

These two procedures are very simple, easy to understand, this is no longer to repeat.
At this point, VBS important grammatical structure and the sentence has been a rough study completed, to more in-depth and meticulous systematic study, it is best to find relevant books to see. The following will be officially a VBS programming Internet Café combat phase.

Actual Combat article

Before "Combat" we first understand what a Shell object and WSH are. The Shell object is Microsoft's encapsulation of some common shell manipulation functions. such as opening or browsing a folder, locating files, shutting down a computer, and so on, but also browsing through the entire file system to obtain various information about a file or folder. Because these packages are built on 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 scripting instruction for Windows platform, it is powerful and uses the JS and VBS scripting languages to achieve its superior functionality, and it can access Excel files in addition to modifying the registry. can also communicate with the network, of course, its biggest advantage is that it can communicate with the operating system, and modify the registry is only its communication with the operating system tip of the iceberg. It has so many advantages and practicality, is much favored by many Windows users.

It says WSH use VBS to modify the system registry, but you must first create an object that communicates with the operating system, and then manipulate the registry using various methods of the object, creating the method and format for this object:

Dim Operatereg
Set operatereg=wscript.createobject ("Wscript.Shell")
The first line above declares a variable, and the second line creates a Shell object Operatereg that communicates with the operating system using a declared variable.

The following is to use the Shell object to shut down some of the internet cafes to bring security risks to the service bar.

Turn off default sharing
Set Ds=createobject ("Wscript.Shell")
Ds.run "net stop server/y"

Turn off the WMI service
Set Swmi=createobject ("Wscript.Shell")
Swmi.run "net stop wmi/y"

Disable Guest Users
Set Sg=createobject ("Wscript.Shell")
Sg.run "NET user Guest/active:no"

Turn off Telnet Service
Set St=createobject ("Wscript.Shell")
St.run "net stop telnet"
  
Using VBS to realize boot bat delay and no black screen
If we turn on the boot to execute a batch file named Bar.bat in the remote server \\server\new\, then build a batch in the client's C-packing directory and name him Run.bat, as follows:
Ping 127.0.0.1-n 10 (10 means the number of seconds you want to delay to set yourself as needed)
Call \\server\new\bar.bat
  
Save and then build a text in the same directory
Set Nb=createobject ("Wscript.Shell")
Nb.run "Run.bat/start", 0
Save as a VBS file and then add this file to the startup item, restart the bat delay and no black screen is implemented.

All of these are the simple programs of VBS, if you can learn the VBS, you will find that it is very powerful, many complex functions can be achieved through it. Now many network management are very happy to use VBS programming to the Internet Café machine management. This series of three articles is only a simple understanding of the VBS and examples of applications, we want to learn more in-depth, but also rely on peacetime efforts and accumulation, and finally, I wish you can learn the VBS at an early stage.

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.