To perform conditional processing in batch BAT program _dos/bat

Source: Internet
Author: User
Tags goto
The following code uses the IF/? >> C:\help.txt generated.
-----------------------------------------------------------------

Perform conditional processing in a batch program.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

Not to specify that only if the condition is false, Windows XP
The command should be executed.

ERRORLEVEL number if the last program to run returns an equal or greater
Specifies the exit encoding for the number, specifying the condition to be true.

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

EXIST filename If the specified filename exists, the specified condition is true.

command specifies the commands to be executed if the condition is met. If the specified
The condition is FALSE, and the command can be executed with an ELSE
The ELSE command for the command after the keyword.

The ELSE clause must appear on the same line after the IF. For example:

IF EXIST filename. (
del filename.
) ELSE (
echo filename. Missing.
)

The following clause is not valid because the DEL command needs to be terminated with a new line:

IF EXIST filename. del filename. ELSE echo filename. Missing

Because the ELSE command must be on the same line as the end of the IF command, the following clause is also
Will not work:

IF EXIST filename. del filename.
ELSE echo filename. Missing

If all are placed on the same line, the following clause is valid:

IF EXIST filename. (del filename.) ELSE echo filename. Missing

If the command extension is enabled, if the following changes are:

IF [/i] string1 compare-op string2 command
IF cmdextversion number Command
IF DEFINED Variable Command

Where the comparison operator can be:

EQU-equals
NEQ-Not equal to
LSS-Less than
Leq-less than or equal to
GTR-Greater than
GEQ-greater than or equal to

and/I switches; If the switch is specified, the string to be performed is not divided
Uppercase and lowercase. The/I switch can be used in the form of an IF string1==string2. These
Comparisons are universal; The reason is that if string1 and string2 are all made up of numbers
, the strings are converted into numbers, and the numbers are compared.

Cmdextversion conditions have the same effect as ERRORLEVEL, except that
is compared to the build number associated with the command extension. First version
is 1. The version number is incremented each time the command extension is significantly enhanced.
The cmdextversion condition is not true if the command extension is disabled.

If you have defined an environment variable, the DEFINED condition is the same as the EXISTS,
Except that it gets an environment variable, the result returned is true.

If there is no environment variable named ERRORLEVEL,%errorlevel%
A string expression that expands to errolevel the current value;
Its numeric value. After running the program, the following statement illustrates the use of errorlevel:

Goto answer%errorlevel%
: Answer0
Echo program had return code 0
: Answer1
Echo Program had return code 1

You can also use the above numerical comparisons:

IF%errorlevel% Leq 1 goto okay

If there is no environment variable named Cmdcmdline,%cmdcmdline%
will be in CMD. EXE for any processing to be extended before being passed to CMD. EXE's original
command line; otherwise, you get the numeric value.

If there is no environment variable named cmdextversion,
%cmdextversion% will be expanded to cmdextversion current values.
string character expression, otherwise, you get its numeric value.

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.