Windows Bat (Batch): if detailed

Source: Internet
Author: User
Tags error code goto valid

Windows Bat (Batch)--if detailed

Summary: This article describes the syntax of the IF statement in Windows bat, including the basic situation in 3 and the extended if statement

1. If & if not

If and if not all have 3 of the situation:

IF [NOT] ERRORLEVEL number do command

IF [NOT] string1==string2 do command

IF [NOT] EXIST filename do command

Not

Specifies that Windows should execute the command only if the condition is false.

ERRORLEVEL number if the last program that was run returns an exit code that is equal to or greater than the specified digit, the specified condition is true.

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

EXIST filename Specifies that the condition is true if the specified file name exists.

command if the criteria are met, specify the commands to execute. If the specified condition is FALSE, the command can be followed by the Else command, which executes after the ELSE keyword.

1.1 IF [NOT] string1==string2 do command

Note that it is a string, and you want to keep string1 and string2 in the same format

Such as:

[Python]

If "%1" = = "A" format a:

if {%1}=={} goto noparms

if {%2}=={} goto noparms

1.2 IF [NOT] EXIST filename do command

If there is a specified file, then the condition is established, run the command, otherwise run the next sentence.

Such as

[Python]

1.

If exist Config.sys edit Config.sys

2.

If exist%1.obj link%1.obj

else Pause & Edit%1.asm

3.

IF EXIST filename. (

del filename.

) ELSE (

echo filename. Missing.

)

ELSE clause must appear on the same line after the IF

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

[Python]

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 also

does not work:

[Python]

IF EXIST filename. del filename.

ELSE echo filename. Missing

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

[Python]

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

1.3 IF [NOT] ERRORLEVEL number do command

If the return code equals the specified number, the condition is set up, run the command, or run the next sentence.

if errorlevel 2 goto x2

DOS programs will return a number to DOS, called the error code ERRORLEVEL or return code, the common return code is 0, 1.

2. If extension

If the command extension is enabled, the if will change as follows:

IF [i] string1 compare-op string2 command

IF cmdextversion number Command

IF DEFINED Variable Command

(1) IF [i] string1 compare-op string2 command

The Compare-op 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

The/I switch (if specified) indicates that the string comparison to be made is not case-sensitive.

The/I switch can be used in the form of the IF string1==string2. These comparisons are common because, if both string1 and string2 are made up of numbers, the strings are converted to numbers for numeric comparisons.

(2) IF cmdextversion number Command

The cmdextversion condition is the same as ERRORLEVEL, except that it is compared to the build number associated with the command extension. The first version is 1. Each time a significant increase in the command extension occurs, the version number is incremented by one. When the command extension is deactivated, the cmdextversion condition is not true.

If an environment variable is defined, the DEFINED condition is the same as EXIST, except that it gets an environment variable, and the result is true.

If there is no environment variable named ERRORLEVEL,%errorlevel%

A string expression that expands to errolevel the current value, otherwise you get its 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 numbers above to compare:

IF%errorlevel% LEQ 1 goto okay

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

If there is no environment variable named cmdextversion,%cmdextversion% expands to a string character expression that cmdextversion the current value, otherwise you will get its value.

Related Article

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.