I found that most of my friends in black and white do not know how to program, which is not a good thing. So this time I wrote a simple programming tutorial to talk about VBScript. It is mainly aimed at novices. Friends who know how to program should not waste time. If you want to get to the following VBScript, it is also OK. But since you have a basic programming foundation, it is recommended to directly find some reference books to read, which will be faster.
What is VBScript? The full name of VBScript is: Microsoft Visual Basic Script Editon. (Microsoft Visual BASIC Script Edition). As the information revealed in literals, VBS (a further abbreviation of VBScript) is a scripting language based on Visual Basic. Let me explain further that Microsoft Visual Basic is a set of visual programming tools produced by Microsoft. The syntax is based on Basic. The scripting language is that it is not compiled into a binary file, and the source code is directly interpreted and executed by the host. To put it simply, the program you write does not need to be compiled into .exe, but sends the source program of .vbs directly to the user, and the user can execute it.
I know what newbie cares about most now is what tools to use to develop VBS programs. The answer is: Notepad. I'm not kidding. In fact, any text editor can be used to develop VBS development. It's just that Notepad is built by the system, which is easier to find. Despite this, I still recommend that you download a professional text editor because these tools can provide "grammar highlighting" and other functions, which is more convenient to develop. Which one you like is Edit Plus (2.10).
OK, let’s write a VBScript program to warm up first.
REM Enter and echo your name
'Use InputBox and Msgbox functions
Dim name,msg
msg="Please enter your name:"
name=Inputbox(msg,"name")
Msgbox(name)
Enter the above program list into Notepad, and then save it as a file with .vbs as the extension (select "All Files" in "Save Type"). Then double-click to run and observe the run results. Note: Please enter the program list yourself, do not copy->paste!
Let me explain this program. The first and second lines are the "REM" statements and " ' " respectively. The functions of these two things are the same, indicating that this line is a comment line, that is, these two lines do nothing, and are just used to explain the functions of this program, copyright information, etc. Comment lines are one of the most important parts of the program. Although it is not necessary, it is very beneficial for others to read the source code and analyze the source code by themselves. A good habit is to add clear and concise comments where necessary.
Dim is used to declare a variable. In VBS, variable types are not that important. That is to say, VBS will help you automatically identify variable types. And variables do not have to be declared first before use. The program will dynamically allocate variable space. In VBS, you don’t have to consider whether name stores an integer or a decimal (the scientific name is "floating point number"), nor do you have to consider whether it is a string (a string of characters, such as "Hello World"), VBS will automatically help you solve it. So the third line statement can be deleted, the effect will not change, but I strongly oppose doing this. The basic principle of a variable is: declare first, then use. The variable name starts with a letter, you can use underscores and numbers, but you cannot use words that have been defined by vbs, such as dim, and cannot be pure numbers.
The next line is called "assignment", "="is an assignment symbol, not an equal sign in mathematics, although it looks the same. This is an orthodox understanding, and it is nothing to understand it as equals. The left side of the assignment number is a variable, and the right side is the value to be assigned to the variable. After assignment, the variable msg is equivalent to "Please enter your name" in the program, but when msg is copied again, the original value will disappear. Not only the string, but any other variable is assigned in this way, for example: a=2, b=12.222, etc.
Further down, Inputbox and Msgbox are built-in functions in VBS. A function is equivalent to a "black box", with input (parameters) and output (return value). You don't need to understand how the function works. Just understand what this function can do. We can also define our own functions, but we have to wait until later. Now we only need to understand that a function can have a return value or no, and can have parameters or no. For example, Inputbox is a function with a return value. We use the variable on the left side of the assignment number to "snap" the return value of the InputBox - which is what you enter. In the brackets on the right side of the input box are parameter lists, each parameter is separated by "," and each parameter has different functions. , For example, the first parameter will be displayed in the prompt. We pass the msg variable as the first parameter to the Inputbox function, and msg="Please enter your name:", so we will see "Please enter your name:" in the prompt bar of the dialog box. The second parameter is the title of the dialog box. We use the direct quantity (scientific name is "constant", here is "string constant") to the function. Of course, you can also pass variables. Inputbox has many parameters, such as adding another " after the "name", and then entering any string of characters (a string, a string wrapped in double quotes" is called a string) and then run it to see the results. You will find that the text box used for input has a default value, which is the function of the third parameter.
The Msgbox function is a function used to output. There is no special output function in VBS (print in BASIC and printf in C), so we can only use a dialog box to observe the output results. There is only one necessary parameter for Msgbox, which is the content to be output. In this case, we don’t need to pay attention to the return value of msgbox. We will discuss Msgbox and Inputbox in the future. Today is just a warm-up, and that’s all.
Key points:
1) The comment (starting with REM or ') line does not work in the program, but it can make it easier for others to understand your program.
2) The variable is like a box, or a code name, which can represent what you want to represent. Variable assignment uses "="
3) Characters wrapped in "" are called "strings"
4) The function is like a "black box", with parameters and return value, and the variable on the left can catch the return value
5) The Inputbox function pops up an input dialog box, and Msgbox is used for output
Operation:
1) Test the third parameter of Inputbox
2) Write a program to output your age
3) Write a program for 3 inputs, enter your and your parents' names respectively (requires a prompt to be displayed), and output them in 3 times
This is the first section.
What is VBScript? The full name of VBScript is: Microsoft Visual Basic Script Editon. (Microsoft Visual BASIC Script Edition). As the information revealed in literals, VBS (a further abbreviation of VBScript) is a scripting language based on Visual Basic. Let me explain further that Microsoft Visual Basic is a set of visual programming tools produced by Microsoft. The syntax is based on Basic. The scripting language is that it is not compiled into a binary file, and the source code is directly interpreted and executed by the host. To put it simply, the program you write does not need to be compiled into .exe, but sends the source program of .vbs directly to the user, and the user can execute it.
I know what newbie cares about most now is what tools to use to develop VBS programs. The answer is: Notepad. I'm not kidding. In fact, any text editor can be used to develop VBS development. It's just that Notepad is built by the system, which is easier to find. Despite this, I still recommend that you download a professional text editor because these tools can provide "grammar highlighting" and other functions, which is more convenient to develop. Which one you like is Edit Plus (2.10).
OK, let’s write a VBScript program to warm up first.
REM Enter and echo your name
'Use InputBox and Msgbox functions
Dim name,msg
msg="Please enter your name:"
name=Inputbox(msg,"name")
Msgbox(name)
Enter the above program list into Notepad, and then save it as a file with .vbs as the extension (select "All Files" in "Save Type"). Then double-click to run and observe the run results. Note: Please enter the program list yourself, do not copy->paste!
Let me explain this program. The first and second lines are the "REM" statements and " ' " respectively. The functions of these two things are the same, indicating that this line is a comment line, that is, these two lines do nothing, and are just used to explain the functions of this program, copyright information, etc. Comment lines are one of the most important parts of the program. Although it is not necessary, it is very beneficial for others to read the source code and analyze the source code by themselves. A good habit is to add clear and concise comments where necessary.
Dim is used to declare a variable. In VBS, variable types are not that important. That is to say, VBS will help you automatically identify variable types. And variables do not have to be declared first before use. The program will dynamically allocate variable space. In VBS, you don’t have to consider whether name stores an integer or a decimal (the scientific name is "floating point number"), nor do you have to consider whether it is a string (a string of characters, such as "Hello World"), VBS will automatically help you solve it. So the third line statement can be deleted, the effect will not change, but I strongly oppose doing this. The basic principle of a variable is: declare first, then use. The variable name starts with a letter, you can use underscores and numbers, but you cannot use words that have been defined by vbs, such as dim, and cannot be pure numbers.
The next line is called "assignment", "="is an assignment symbol, not an equal sign in mathematics, although it looks the same. This is an orthodox understanding, and it is nothing to understand it as equals. The left side of the assignment number is a variable, and the right side is the value to be assigned to the variable. After assignment, the variable msg is equivalent to "Please enter your name" in the program, but when msg is copied again, the original value will disappear. Not only the string, but any other variable is assigned in this way, for example: a=2, b=12.222, etc.
Further down, Inputbox and Msgbox are built-in functions in VBS. A function is equivalent to a "black box", with input (parameters) and output (return value). You don't need to understand how the function works. Just understand what this function can do. We can also define our own functions, but we have to wait until later. Now we only need to understand that a function can have a return value or no, and can have parameters or no. For example, Inputbox is a function with a return value. We use the variable on the left side of the assignment number to "snap" the return value of the InputBox - which is what you enter. In the brackets on the right side of the input box are parameter lists, each parameter is separated by "," and each parameter has different functions. , For example, the first parameter will be displayed in the prompt. We pass the msg variable as the first parameter to the Inputbox function, and msg="Please enter your name:", so we will see "Please enter your name:" in the prompt bar of the dialog box. The second parameter is the title of the dialog box. We use the direct quantity (scientific name is "constant", here is "string constant") to the function. Of course, you can also pass variables. Inputbox has many parameters, such as adding another " after the "name", and then entering any string of characters (a string, a string wrapped in double quotes" is called a string) and then run it to see the results. You will find that the text box used for input has a default value, which is the function of the third parameter.
The Msgbox function is a function used to output. There is no special output function in VBS (print in BASIC and printf in C), so we can only use a dialog box to observe the output results. There is only one necessary parameter for Msgbox, which is the content to be output. In this case, we don’t need to pay attention to the return value of msgbox. We will discuss Msgbox and Inputbox in the future. Today is just a warm-up, and that’s all.
Key points:
1) The comment (starting with REM or ') line does not work in the program, but it can make it easier for others to understand your program.
2) The variable is like a box, or a code name, which can represent what you want to represent. Variable assignment uses "="
3) Characters wrapped in "" are called "strings"
4) The function is like a "black box", with parameters and return value, and the variable on the left can catch the return value
5) The Inputbox function pops up an input dialog box, and Msgbox is used for output
Operation:
1) Test the third parameter of Inputbox
2) Write a program to output your age
3) Write a program for 3 inputs, enter your and your parents' names respectively (requires a prompt to be displayed), and output them in 3 times
This is the first section.