Flexible and practical introduction to vbs


We often hear that VBS is very powerful and simple. Do you also want to understand VBS? Now let’s learn VBS together.

VBS is just an English abbreviation. Its full name is visual basic script edition, that is, VBS is the script edition of Visual Basic. Many people say VBS is a subset of VB. Because VBS is basically consistent with Visual Basic in programming syntax and functions. The general functions that visual basic can realize can also be realized by VBS. The implementation process is also easier than visual basic, because VBS programming can be completed as long as a text editor, and the Notepad provided by windows can be competent.

Let’s write a classic routine: Hello world. What we want to achieve is the effect shown in Figure 1. In fact, we only need one statement to achieve this effect. The specific methods are as follows: open Notepad, enter “msgbox (” Hello world “)” in the editing area (as shown in Figure 2), then save the file with VBS as the extension (“all files” in “save type”, enter the file name including the extension, such as “hello world. VBS” in “file name”), and double-click the saved file directly after saving, The effect comes out. Friends who have studied VB know that msgbox function is one of the most commonly used functions in VB programming. It can realize pop-up window prompt information.

Flexible and practical introduction to vbs
Figure 1

Flexible and practical introduction to vbs
Figure 2

If there is output, there will be input. Corresponding to msgbox is the input function InputBox, which is used to receive the user’s input information and return it to the specified variable. Let’s take another example. Enter the following in the notebook:

  Dim name
Name = InputBox (“please enter your Internet cafe name”, “name”)

After saving, double-click run to open the window shown in Figure 3.

Flexible and practical introduction to vbs
Figure 3

Enter the content you want to enter in the window (for example, enter “Xiaocao network” in this example). Click OK, and a prompt window will pop up to display the content you entered (as shown in Figure 4).

Flexible and practical introduction to vbs

Figure 4

Now let’s have an in-depth understanding of the significance of the above content. Dim is a keyword for declaring variables in visual basic, which is also applicable in VBS. The statement form is as follows:

Dim variable name [as type]

The contents in brackets can be omitted (brackets mentioned in later articles refer to this meaning). If omitted, the variable you declare will be a variant, and the data type of the variable will depend on the value you assign to the variable. If the value you assign to the variable is an integer, it will be an integer variable. The second statement “name = InputBox (” please enter your Internet cafe name “,” name “)” means that when InputBox function is executed, the content entered by the user is assigned to the variable name, “=” is the assignment symbol, which is different from “=” in the number.

The variables mentioned above may be that readers who have not studied programming may not know what variables are at all. In fact, in high-level language programming, in addition to variables, there are constants. To put it bluntly, constants and variables are named memory units. When reading and modifying data, the data in them is accessed through the memory unit name. Specifically, a variable is an amount whose value can change during the operation of a program. Great flexibility. Constants are quantities that remain unchanged during program operation. Constants are generally divided into three types: direct constants and user declared symbolic constants. The direct constant can be used without declaration. For example, 123 is a direct constant. You can use it directly, but you can’t change it. If you try to change it, such as 123 = 321, it will make an error when the program runs. The constant declared by the user is to facilitate the reading or modification of the program. If the constant declared by the user is modified, the values related to the declared constant will change accordingly without modifying one by one. The keyword of constant declaration is const, for example: const pi = 3.14159. Let’s write a program to calculate the area of a circle. Enter the following in Notepad:

  const PI=3.14
  dim r,S
R = InputBox (“please enter the radius of the circle you want to calculate”, “radius”)
Msgbox (“area of circle” & S)

Save as VBS file, a program to calculate the circular area is completed.

So far, we must also have a preliminary understanding of VBS programming. To learn more deeply, we should also learn the selection structure and loop structure, which will be involved in the later articles.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]