Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic syntax of VBS scripts

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the basic syntax of VBS script". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the basic syntax of VBS script".

1. Edit the software related to VBS

For VBS-based programming, we can write it directly using notepad.

The way to open it is:

1. Right-click on the desktop and click New, and then click New notepad.

2, press Windows + R, so you can open cmd, and then type notepad (this is actually a new notepad file. That is, you can create a new notepad

The effect picture displayed is shown in the following figure:

When you have written a code, click the file above notepad, and then click Save as, when saving, please select the "all files" option, and then change the file extension to: .vbs, so that after the operation will generate a vbs file in the specified location, and then double-click the file can be run.

II. Definition of variables

For any programming language, the first thing we care about is how to define a variable.

In Visual Basic Script, the normal pattern for defining a variable is: dim variable name

Of course, there is no problem if you do not define variables but use them directly, just like Python, you can use them without declaration, but here we recommend that you follow the principle of declaring variables before using them.

The following is the final running effect and the source code.

Msgbox here is a keyword in vbs that pops up a pop-up box to display the information you want to display.

Msgbox can display various types of variables.

A little explanation.

In vbs, the types of variables are basically the same as those of other languages, such as strings, integers, floating-point numbers, and Boolean numbers (True and False). However, we do not need to specify which type of variable a variable is, the compiler will directly identify the type of variable itself.

Input and output 1. Input

In vbs, if you want to enter, you need to first pop up a pop-up box, and then type.

For example, the following code:

Dim strstr=inputbox ("please input your name here", "Title", "hhhhhhhhh") msgbox str

Here, a str variable is defined to receive the value of a variable obtained by inputbox, and then displayed using the msgbox we mentioned earlier.

In inputbox, the first parameter is the prompt for the pop-up box, the second parameter is the title of the pop-up box, and the third parameter is a default value for the input box.

The effect picture is as follows:

Figure 1.

Figure 2.

Figure 3.

Figure 4.

2. Output

In fact, the output mode here has been used many times in the previous article, that is, the msgbox keyword mentioned above, its function is to pop up a pop-up box and then display the information you want to display, such as the number 30 we output in the first example and the string we displayed in the second example: "Jiangyue-". In short, the msgbox box is used to display the information. Equivalent to cout in C++, equivalent to print in Python, etc., is an output, but in a different way from other languages.

IV. Judgment sentence

Next, let's first introduce the judgment statement, and then look at the loop statement to display the judgment statement:

If you have come into contact with other programming languages, you must be no stranger to judgment statements, and the same is true in vbs, syntax is if,else,else if, and so on. The specific writing is slightly different, such as the following code:

Dim strstr=msgbox ("Do you love me?", vbYesNo, "LOVE") if str=vbYes then msgbox "I love you too, I think we will be happy!" else msgbox "I will close your computer!" Set sh=wscript.createobject ("wscript.shell") sh.run "shutdown-s-t 300" end if

VbYesNo this means to show two option boxes to choose from!

The above code is obviously a confession code, can also be said to be a whole person code, if others do not agree, then directly shut down.

Here from the above code is obviously relatively easy to see the specific syntax of the judgment statement, through the above example can be found to judge the basic syntax of the statement, here should be no need to say more, just emphasize, try to indent the code, and then to end if. Although there is no problem with code indentation, we highly recommend syntax indentation for the convenience of Yue Su.

The effect of the above code is as follows:

Figure 1.

Figure 2.

Figure 3.

Figure 4.

Here are two different results directly shown.

The shutdown code is:

Set sh=wscript.createobject ("wscript.shell") sh.run "shutdown-s-t 300"

This means that the computer will be shut down five minutes later.

Of course, in a whisper, there is a way to stop this automatic shutdown:

First, Windows+R opens cmd, and then, type:

Shutdown-a

This will stop the automatic shutdown.

You can try this yourself, of course, you can also close the process in the Task Manager.

This method is used in the following example. (in a loop statement)

Loop statement 1. Do-loop loop

There are two loops in vbs, and this is one of them. The specific syntax is:

Do msgbox "Dead loop!" loop

Of course, we found that the above code is an endless loop, there is no end condition, if you open the software, you will find that you can not turn it off.

(if you want to force the shutdown, there is still a way. At this time, you need to open the task manager and manually close the process directly. The specific action is: shortcut key: Ctrl + Alt + Del, then click the task manager to find the corresponding process, as shown in the following figure:

Here I have opened three. In this interface, we click on the corresponding process, and then click to end the task, that is, to close the task. )

The above code can be regarded as the second simplest whole person code.

Of course, we still want a loop to end, not all the time, so here are three ways to end a do-loop loop

1) until

Here, we use examples directly to demonstrate, which may be easier to understand.

Example:

Dim strdo until str= "yes" msgbox "do you love me?" Str=inputbox ("yes or no", "ANSWER", "yes") loop

The effect of the run is as follows:

Figure 1.

Figure 2.

Figure 3.

Figure 4.

Of course, if you do not type yes, then the program will always be executed.

2) while

Here, we also show it in the form of an example:

Dim strdo msgbox "do you love me?" Str=inputbox ("yes or no", "ANSWER", "yes") loop while str "yes"

In this code, it means that the program will always be executed when the input is not equal to yes, and when the input is equal to yes, then the loop will be terminated.

Of course, the execution result of this code is exactly the same as that of the previous one, so we won't show much here.

3) exit do

When using exit do, we need to make use of the conditional judgment of if to implement:

Dim strdo msgbox "Do you love me?" Str=inputbox ("yes or no?", "ANSWERING", "yes") if str= "yes" then msgbox "I love you toodies!" Exit do else msgbox "Please say that you love metropolis!" Msgbox "Please answer kids!" End ifloop

The effect display image is as follows:

Figure 1.

Figure 2.

Figure 3.

Figure 4.

Figure 5.

Figure 6.

Figure 7.

Figure 8.

This is to use the exit do method to achieve the cut off of the loop.

2. For-next cycle

Next, let's introduce the syntax of the for-next loop.

First of all, explain the role of this loop, in fact, this loop is like for in C++ or for in Python, traversing an array starting with 0 (of course, you can also set it from somewhere else), and then add one step at a time (the step in C++ is written directly to add yourself, while in Python you can use the range function to set the step, but here The vbs language cannot directly set the step size, it can only be added one at a time, that is, the step size can only be one):

Specific examples are as follows

Msgbox "show some numbers" for iTunes 0 to 3 msgbox "the number now is:" & inext

Here, the way we output different types of variables at the same time is to use a symbol: & to connect.

The result of the run is as follows:

Figure 1.

Figure 2.

Figure 3.

Figure 4.

Figure 5.

Figure 6.

Thank you for your reading, these are the contents of "what is the basic syntax of VBS scripts?" after the study of this article, I believe you have a deeper understanding of what the basic syntax of VBS scripts is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report