[Original] windows Batch Processing Command learning 2

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

Continue to learn about Windows batch processing commands.

 

Batch File parameters:

People who have some basic programming knowledge know that functions have parameters. batch files also have parameters. The batch file can also use parameters (equivalent to the command line parameters of the doscommand) like C functions, which requires a parameter identifier "% ".

% [1-9] indicates a parameter. A parameter is a string separated by spaces (or tabs) after the file name when a batch file is run. Variables can be changed from % 0 to % 9.% 0 indicates the Batch Processing Command itself. Other parameter strings are represented in the order of % 1 to % 9.

Practice:

Create a new batch file a. BAT and enter the following command:

@ Echo off

Ping % 1

Ping % 2

 

Then, in the command console, enter the command: C: \ Users \ Zhu> C: \ A. Bat www.163.com www.sina.com

Let's take a look at the effect and understand how to use multiple batch file parameters.

 

If command

If statements have three modes:

 

If [not] string1 = string2 command

If [not] exist filename command

If [not] errorlevel number command

Not indicates that Windows XP should execute this command only when the condition is false.

Errorlevel number if the last runningProgramReturns an exit code equal to or greater than the specified number. The specified condition is true.

String1 = string2 if the specified text string matches, the specified condition is true.

Exist filename: if the specified file name exists, set the condition to true.

If the command meets the conditions, specify the command to be executed. If the specified condition is false, the command can be followed by an else command that executes the else keyword.

First, let's explain the first one:

If [not] string1 = string2 command

If string1 = string2, Execute Command

Next we will give you an if statement that can be applied in practice.

Natural statement: If the input parameter is 3, "A = 3" is displayed"

Computer statement:

@ Echo off

If "% 1" = "3" Echo "A = 3"

Or write it

@ Echo off

If % 1 = 3 Echo "A = 3"

Note: When testing, enter 1.bat 3 under cmd, because the parameter is used here. For details, seeArticleThe first part is "batch file Parameters ".

Second:

If [not] exist filename command

This command is used to check whether a file exists. If yes, Run Command. If no, nothing is displayed.

For example, we want to check that the eroot directory contains a file named 2.txt. If yes, exist is displayed. If no, nothing is displayed.

The batch processing command is as follows:

@ Echo off

If existe: 2.txt echo "exist 2.txt"

 

Third:

Compared to the first two types, the third type is hard to understand, and I can't understand it. I checked some information and understood it roughly. The key to understanding if [not] errorlevel number command is errorlevel. What does this mean?

Originally, every line of the DOS command has a return value after running (you can compare it to the return value of the function in the program ). Generally, the execution result of the previous commandCodeThere are only two results. "success" indicates "failure" using 0 .. Errorlevel is used to save the returned value. If errorlevel is greater than or equal to number (number is the constant or batch file parameter you have given), Execute Command.

 

Let's try and understand it.

Practice:

Create a new batch file a. BAT and enter the following command:

@ Echo off

Ping % 1

Ping % 2

 

Then, in the command console, enter the command: C: \ Users \ Zhu> C: \ A. Bat www.163.com www.sina.com

Let's take a look at the effect and understand how to use multiple batch file parameters.

@ Echo off

Net user

If errorlevel 1 echo net user execution failed!

 

If you want to prompt the command execution information, you can write it as follows:

@ Echo off

Net user

If not errorlevel 1 echo net user execution successful!

 

You may think this method is awkward. At the same time, you think it is a prompt that the execution is successful, and vice versa. Well, let's change it to the following:

@ Echo off

Net user

If % errorlevel % = 0 (

Echo netuser execution successful!

) Else (

Echo netuser execution failed!

)

 

This is a combination of IF and Else, where if % errorlevel % = 0 is assumed that errorlevel is equal to 0. It is worth noting that if And else are used in a strict format and must be:

If % errorlevel % = 0 (

Command // execute the command

) Else (

Command // execute the command

)

Goto command

To be honest, after reading this command, I immediately remembered the GOTO statement in C language. In fact, this command is very similar to the GOTO statement in C language and is also very simple. In practice, you will know.

Create a new batch file a. BAT and enter the following command:

@ Echo off

Net user

If % errorlevel % = 0 goto successed

If % errorlevel % = 1 goto failed

: Successed

Echo netuser execution successful!

Gotoreturn

: Failed

Echo netuser execution failed!

: Return

 

SET command

 

The SET command is commonly used to set environment variables. It is easy to use, for example:

Set hosts = C:/Windows/system32/Drivers/etc

Note: The SET command can only set environment variables that are valid under the current cmd.

 

References:

 

1. The batch processing file tutorial written by a man (Unknown name) on the Internet

 

Author: clever101 posted on 23:06:08 Original article link Read: 339 comments: 0 views comments

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.