Vbs/js Script Programming Teaching (1) _vbs

Source: Internet
Author: User
Tags error code error handling
Script programming Teaching (1)


A lot of friends on the Forum asked questions about the script, just recently to the script more interested, write something. First of all, all my code is
Vbscript,jscript I haven't studied it, but I think it's pretty much the same.

On the basis of the syntax such as variable declarations, branches, loops, function calls, and so on, and so I do not speak, do not know how to look at themselves.

1, our first VBS program: or the old to lose teeth in winter.

hello.vbs**************************

Dim Hello

hello= "Hello world!"

WScript.Echo Hello

WScript echo "This are my"

It can be seen that there are two uses of WScript.Echo, this is not difficult.

You can double-click directly to run, which can be entered at the command line in the current directory:

cscript hello.vbs



2, in the script to invoke other programs:

Using the Run () method, you must first establish an instance of the shell before you use it

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 the error number is:"

WScript.Echo CStr (ret)

End If

***************************************************************************

Run here has three parameters, the first parameter is the path of the program you want to execute

The second program is the form of the window, 0 is running in the background;

1 indicates normal operation

2 indicates that the program is activated and is displayed as minimized

3 indicates that the program is activated and displayed as maximized

A total of 10 such parameters I have listed only 4 of the most commonly used.

The third parameter indicates whether the script is waiting or continuing, and if set to true, the script waits for the calling program to quit and then execute backwards.

Notice that no, I have a variable that accepts the return value in front of run, and generally if you return to 0, which means successful execution, if not 0, the
The return value is the error code, which can be used to find the appropriate error.



3, InputBox and MsgBox

VB people to two things should be very familiar with the use of no difference

Input=inputbox ("Please enter your password", "passwd")

If input<> "1234"

Then

MsgBox "You enter a wrong passwd"

End If

Of course, you can also add a button to the MsgBox, with a variable to accept the user's choice

For example: Ret=msgbox "Continue?", vbYesNoCancel

The return value and constants are compared as follows:

vbOK 1

Vbcancel 2

Vbabort 3

Vbretry 4

Vbignore 5

Vbyes 6

Vbno 7



4. Error handling

What VB like with On Error Resume Next

There's nothing to talk about, if you get a mistake, skip the next sentence.

Of course this method is very retarded and there is a need for a method that VBScript provides an object Err object

He's got two ways to Clear,raise.

5 properties: Description,helpcontext, Helpfile,number,source

We can use Err.Number to get 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 throw errors manually.

Like we're going to produce a path not found error telling the user that he filled in the wrong path

On Error Resume Next

Err.Raise 76

MsgBox "Error:" & Err.Description

Err.Clear





Above all is the foundation, today writes here, is very tired oh, ah hehe if has the reprint to indicate the origin. Let's talk about the file system tomorrow.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.