vbs basic tutorial (1)

vbs introduction

vbs is a scripting language based on Visual Basic. The full name of VBS is: Microsoft Visual Basic Script Edition. (Microsoft Visual BASIC script version). VBS is an abstract subset of Visual Basic. It is built-in in the system. The script code written with it cannot be compiled into a binary file and is directly executed by the Windows system (actually it is an interpreted source code called the host and executed, which may be wscript .exe or cscript.exe)
Its language is similar to Visual Basic (VB). Vbs is generally compiled with Notepad, because it comes with windows, it is more convenient to write. But it is best to get a compiler yourself.

Why learn vbs?

Some students may say: "Why learn vbs? I would rather learn Python, Java, or basic C, which is very popular recently, why should I learn a little vbs?"
1. In Windows, learning computer operations may be very Simple, but many computer tasks are repetitive tasks. For example, you may need to copy, paste, rename, and delete some computer files every week. Maybe the first thing you start the computer every day is to open WORD and switch to your favorite input method. Perform text editing and play beautiful music to create a comfortable environment for your work. Of course, you may often need to sort out some data in the text and arrange all kinds of data according to certain rules... . These things are repetitive and trivial, making people tired easily.
vbs is easy to learn, easy to use, code can be shared, write as you go, small and exquisite, can automate various tasks, can free you from repeated trivial tasks, and greatly improve work efficiency. Moreover, the most convenient thing is that it doesn't even need a special development environment. In your computer, as long as you have a notepad, you can write Vbs scripts. what? Let you learn vbs for overkill? Is this language the best language? There is no best language in the world. The best language is for you.
Suitable crowd: any age group is interested in vbscript or wants to get started in other languages

How to save vbs files?

Note: This section is for those who have no computer knowledge, the following is a demonstration of the notepad operation

Some friends may not know how to save the vbs file, so let’s use Notepad to demonstrate.
1. Write the code Insert picture description here
2. Click "File (F)" in the upper left corner, and then click Save As, to the following interface
Insert picture description here
3. Click File to run
Insert picture description here

hello,world!

After listening to me how to write vbs code, some students may not wait to code hello world.

Let me tell you this. To write a hello world, you must first count the number of characters, calculate the output size, find the output device, determine the output format, and...
Insert picture description here
wait, people c language, Python only a few characters It can be achieved, why is your vbs so troublesome?
Haha, don’t tease you, in fact, the language developer has found a way and encapsulated it in a function------------

msgbox

MsgBox is a function in VBS, the function is to pop up a dialog box, wait for the user to click the button, and return an integer value (can be understood as an integer), indicating which button the user clicked.
The msgbox has multiple parameters. The first parameter is the content displayed in the text box, and the second parameter is the title of the text box. We will not consider the rest for the time being. A formula can be summarized:

msgbox"文本框显示的内容",,"文本框标题"

In other words, hello, world! It's msgbox "hello, world"!
Insert picture description here

ok, now let's write a program, the title requires the text box to write where you are, the title bar is "the city I am in"

'这里以北京举例
 msgbox"北京",,"我在的城市"'第一个vbs程序

operation result
You may be surprised, the output here is all graphical interface (C language is to print data in the dark console. VBS is such a convenient and beautiful language, we will explain more in the future)

inputbox

We can now output data, that is, the program -> computer -> human mode, so is there a way from the keyboard -> computer -> program?
(Emmmm, the technical content that makes your expressions solidify is 100,000 words omitted here)
Insert picture description here
Some students may think that msgbox is a function, so there must be a function for input operations.
The answer is: inputbox! It is a standard input function, and an input box will pop up when running. 'He is responsible for putting the entered value back in a variable' and calling it when appropriate.
The formula is: (At present, as long as you master these two parameters)
inputbox语法:inputbox(“对话框内容”,“对话框标题”)
/* Note: the inputbox should be bracketed with a comma in the middle. It is a
string type */ in the small box (variable)

Product program: parsing code

Let's look at a program:

dim name
name = inputbox("请输入你的名字:")
msgbox"你好,"&name,,"打招呼"

Interpretation: You
may be a little confused when you see this code. Next, let me change the code to a different look:
If I enter'Xiao Ming': The
Input
final result: You

should be able to guess a bit, the name here is'Xiao Ming', here The'&' is an operation of combining "Hello," and "Xiaoming" into "Hello, Xiaoming". That's right,'&' is just to join two characters together. This code is simplified to:

msgbox"你好,小明",,"打招呼"

Product program: knowledge points in the code

1.‘=’

The'=' sign in'name = inputbox("Please enter your name:")' means assignment, which is to put the thing on the right (here is the user input) into the variable on the left. For details, please see Baidu Encyclopedia-Assignment .
2. Variables
What are variables? I think the best way to explain variables is a "box". A variable is like a box. It can only contain one thing. When you want to put other things in, you must take out the original things. We don’t have to worry about this small If the box is too small or too large, it will select the corresponding box according to our value. This is also a smart feature of vbs.

Unlike some languages, such as C, the box it defines can only hold one type. Whether it is a string in VBS: such as "hello world", or a floating point number (commonly known as a decimal): such as 3.3, it can be packed into a'box', we don't need to care about what is packed in, VBS will automatically adjust the box Size. Type.

This "box" has a name with more than one name. When you use a variable in a program, the system will open the box to take out the contents and let these things participate in the processing. In other words, the name is just a label affixed to it, and the content is obtained when used. For details, see Variables (Computer Terms)

VBS variable naming rules :
In VBS, the naming of variables must follow the following rules:
(1) The variable name must start with a letter or an underscore, and the middle of the name can only consist of letters, numbers and an underscore "_";
(2) Variable names The length must not exceed 255 characters;
(3) The variable name must be unique within the valid range. The content of the scope of reference variables will be introduced later.
(4) Variable names cannot be reserved words (keywords) in VBS.
For example: strName1, intMax_Length, intLesson, strNo3, etc. are legal variable names, while A&B, all right, 3M, _Number, 7a, etc. are illegal variable names.

Declare variables: dim
dim means you want to create variables, it can declare multiple variables at the same time (but note that in vbs, undeclared variables will be recreated when you use them. If you don’t want to create them, add them at the beginning of the program Option Explicit, we are in the vbs advanced tutorial: error handling will say)

公式:dim 变量名
或
公式:dim 变量1,变量2

Examples of the above knowledge points

such as:

name="Jerry"
a1=12334
a3=867880
msgbox name

Operation result:
operation result
You can see that it is not the output name, but the Jerry in the output.

Let's take another example:

name="Jerry"
a1=12334
a3=867880
name="Tom"
msgbox name

Operation result: As

you can see, the variable is changeable. If you give it the value of'Tom' again, the previous "Jerry" is lost.

Example of variable naming:
Xiaoming wrote a program:
Insert picture description here
Oh, how did you report an error?

Do you know why?
------------------------------------answer------------- -------------------------

It uses keywords to name variables.

The variable name cannot be a reserved word (keyword) in VBS. It is understood that it has special functions. For example, the inputbox function is to pop up a dialog box, in which a prompt is displayed, waiting for the user to input text, of course it cannot be used.

Summary question

After learning so much, let's summarize it with a small program:

dim a,b,c
a=inputbox("请输入:你的年龄")
b=inputbox("请输入:你父亲的年龄")
c=inputbox("请输入:你母亲的年龄")
msgbox(a)
msgbox(b)
msgbox(c)
'当然,可以简化,这样更直观

Main formulas & knowledge points of this lesson

Msgbox syntax: msgbox "dialog box content",, "dialog box title"
inputbox syntax: inputbox ("dialog box content", "dialog box title")
declare variable format: dim variable 1, variable 2...
"=" is assignment Is not equivalent to the equal sign in mathematics

Alright, this lesson is over, the following is the homework:

operation

1. Try to use it directly without defining variables, and add Option Explicit at the beginning of the
program. 2. Write a program, enter your age, name and age, and output
3. Be proficient in input and output
4. Experiment with easter eggs Reconciliation

Easter eggs

do
msgbox"你想要显示的内容",48
msgbox"你想要显示的内容",48
msgbox"你想要显示的内容",48
msgbox"你想要显示的内容",48
loop

or

do
msgbox"你想要显示的内容",16
msgbox"你想要显示的内容",16
msgbox"你想要显示的内容",16
msgbox"你想要显示的内容",16
loop

(You can try the two separately.)
After running, you will find that the content you want to display will always be dotted. This is because do...loop, which will keep the content looping. Achieve the effect that the dots always appear. We are in vbs basic tutorial: loop will explain in detail

Antidote

1. Open the task manager and end the wscript.exe or cscript.exe process
2. Copy the following code, save it as a .bat file and run:

@echo off
:to
taskkill /im wscript.exe /f
taskkill /im cscript.exe /f
goto to

Guess you like

Origin blog.csdn.net/CSDN_C2/article/details/105641669
Recommended