Scripting programming (1)

zhaozj2021-02-12  165

Scripting programming (1)

By SSSA2000

7/4/2004

Many friends on the forum ask questions about scripts, just recently more interested in scripts, just write something. First explain, all of my code is VBScript, Jscript I have not studied, but I think it is almost.

About the most basic grammar, such as variables, branches, cycles, functions calls, etc. These I will not talk, don't know how to look at them.

1, our first VBS program: or the old winter winter that is old.

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

Dim Hello

Hello = "Hello World!"

WScript.echo Hello

WScript echo "this is my first VBS"

It can be seen that there are two kinds of usage of WScript.echo, which is not difficult.

You can double-click to run directly, you can enter: the command line of the current directory:

CScript Hello.vbs

2, call other programs in the script:

Using the run () method, you must build an instance of the shell before use.

******************* surround.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 to the program you want to execute

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

1 means normal operation

2 demonstrate the activation program and display it to minimize

3 demonstrate the activation program and display it to maximize

I have 10 such parameters I only list 4 most commonly used.

The third parameter is that this script is waiting or continues, if set to true, the script will wait for the program to exit after being exit.

Notice that there is no, I have a variable that accepts the return value in front of Run. Generally speaking, if it returns to 0, it means successfully executed, if not 0, then this return value is the error code, can find the corresponding corresponding to this code mistake.

3, Inputbox and MSGBox

People who will vb should be familiar with two things, and there is no difference in usage.

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 the MSGBOX, accept the user's choice with a variable

For example: RET = MSGBOX "Continue?", Vbyesnocancel

Return value and constant controls are as follows:

VBOK 1

Vbcancel 2

Vbabort 3

Vbretry 4

VBIGNORE 5

Vbyes 6

VBNO 7

4, error handling

What VB is used in Error ResMe next

This is nothing to say, if you encounter an error, skip continuing to do the next sentence. Ok, this method is very mentally, you need to have a method, VBScript provides an object ERR object.

He has two methods clear, raise

5 properties: Description, HelpContext, HelpFile, Number, Source

We can use Err.Number to get the wrong number, for example

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

ON Error ResMe 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 handle errors

For example, we have to generate a Path Not Found error tells the user that the path he filled in is not

ON Error ResMe next

Err.raise 76

MsgBox "error:" & Err.Description

Err.clear

All the above are the foundation, I will write here today, so tired, huh, huh, if you have any reprints. Tell you the document system tomorrow.

转载请注明原文地址:https://www.9cbs.com/read-6278.html

New Post(0)