Script Programming teaching (1)
Many of my friends on the Forum asked about the script. I was interested in the script recently. Just write something. First of all, all my code is
I have not studied vbscript and jscript, but I think it's similar.
I will not talk about the basic syntax, such as variable declaration, branch, loop, and function call. I don't know how to read it myself.
1. Our first vbs program: The old winter and winter.
* ************************ Hello. vbs **************************
Dim hello
Hello = "hello world !"
Wscript. echo hello
Wscript echo "this is my first vbs"
We can see that wscript. echo has two usage methods, which is not difficult.
You can double-click to run the command. You can enter the following in the command line of the current directory:
Cscript hello. vbs
2. Call other programs in the script:
Before using the run () method, you must create a shell instance.
* ******************** Shell. vbs *************************************** ***
Set ws = wscript. createobject ("wscript. shell ")
Ret = ws. run ("notepad", 3, true)
If ret = 0 then
Wscript. echo "succeed !"
Else
Wscript. echo "there is a error, the error number is :"
Wscript. echo cstr (ret)
End if
**************************************** ***********************************
Here, run has three parameters. The first parameter is the path of the program to be executed.
The second program is in the form of a window, and 0 is run in the background;
1 indicates normal operation
2 indicates that the program is activated and displayed as minimized.
3 indicates activating the program and displaying it as maximized
A total of 10 such parameters are listed. I only list the four most commonly used parameters.
The third parameter indicates whether the script is waiting or continues to be executed. If it is set to true, the script will wait until the called program exits and then run it backwards.
Note: No. I have a variable before run that accepts the returned value. Generally, if the returned value is 0, the execution is successful. If the returned value is not 0, this
The returned value is the error code, which can be used to identify the corresponding error.
3. inputbox and msgbox
People who know vb should be familiar with the two things, and their usage is no different.
Input = inputbox ("please enter you password", "passwd ")
If input <> "1234"
Then
Msgbox "you enter a wrong passwd"
End if
Of course, you can also add a button to msgbox and use a variable to accept the user's choice.
For example: ret = msgbox "continue ?", Vbyesnocancel
The return value is as follows:
Vbok 1
Vbcancel 2
Vbabort 3
Vbretry 4
Vbignore 5
Vbyes 6
Vbno 7
4. handle errors
He used on error resume next in the same way as vb.
There is nothing to say. If an error occurs, skip the next sentence.
Of course, this method is very mentally retarded and requires a method. vbscript provides an object err Object.
He has two methods: clear, raise
Five attributes: description, helpcontext, helpfile, number, source
We can use err. number to obtain the error number, for example
* *********************** Err. vbs *****************************
On error resume next
A = 11
B = 0
C = a/B
If err. number <> 0 then
Wscript. echo err. number & err. description & err. source
End if
We can use err. raisel to manually throw an error.
For example, if we want to generate a path not found error, tell the user that the path he entered is incorrect.
On error resume next
Err. raise 76
Msgbox "error:" & err. description
Err. clear
All of the above are the basis. Let's write it here today. It's so tired. If there is a reprinted article in hehah, it will indicate the source. Let's talk about the file system tomorrow.