Windows bat (batch processing) -- IF

Source: Internet
Author: User

Windows bat (batch processing) -- IF explanation Abstract: This article introduces the IF statement syntax in windows bat, including 3 Basic Situation and extended IF Statement 1. IF & IF notif and if not both have three situations: IF [NOT] ERRORLEVEL number do command IF [NOT] string1 = string2 do command IF [NOT] EXIST filename do command NOT specifies that only the condition is false, in Windows. ERRORLEVEL number if the last running program returns 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 the ELSE command, which will be executed after the ELSE keyword. 1.1 IF [NOT] string1 = string2 do command note that it is a string and the format of string1 and string2 must be the same, for example: [python] if "% 1" = "a" format: if {% 1 }={} goto noparms if {% 2 }={} goto noparms 1.2 IF [NOT] EXIST filename do command if a specified file exists, if the condition is true, run the command. Otherwise, run the next sentence. For example, [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 .) the ELSE clause must appear after the IF statement on the same line. Because the del Command needs to end with a new line, the following clause is not valid: [python] if exist filename. del filename. ELSE echo filename. missing because the ELSE command and the IF command must end on the same line, the following clause is not valid: [python] if exist filename. del filename. ELSE echo filename. IF missing is placed on the same row, the following clause is valid: [python] if exist filename. (del filename .) ELSE echo filename. missing 1.3 IF [NOT] ERRORLEVEL number do command IF the return code is equal to the specified number, the condition is true. Run the command; otherwise, run the next sentence. For example, if errorlevel 2 goto x2 DOS returns a number to DOS when running the program, which is called the error code errorlevel or return code. The common return codes are 0 and 1. 2. IF extension is enabled, IF [/I] string1 compare-op string2 command IF CMDEXTVERSION number command IF DEFINED variable command (1) IF [/I] string1 compare-op string2 commandcompare-op can be: EQU-equal to NEQ-not equal to LSS-less than LEQ-less than or equal to GTR-greater than GEQ-greater than or equal to/I switch (if specified) indicates that the strings to be performed are case-insensitive. The/I switch can be used in the form of IF string1 = string2. These comparisons are common because, if both string1 and string2 are composed of numbers, strings are converted into numbers for comparison. (2) if cmdextversion number commandCMDEXTVERSION has the same effect as ERRORLEVEL, except that it is compared with the internal version number associated with the command extension. The first version is 1. Each time the command extension is greatly enhanced, a version number is added. When the command extension is stopped, the CMDEXTVERSION condition is not true. If an environment variable has been DEFINED, the DEFINED condition serves the same purpose as EXIST. Besides obtaining an environment variable, the returned result is true. If no environment variable named ERRORLEVEL exists, % ERRORLEVEL % is expanded to the string expression of the current ERROLEVEL value. Otherwise, you will get the value. After running the Program, the following statements describe the usage 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 numbers to compare: IF % ERRORLEVEL % LEQ 1 goto okay IF there is no environment variable named too many line, % too many line % will be in CMD. EXE is extended to passed to CMD before any processing. the original command line of EXE; otherwise, you will get its value. If no environment variable named CMDEXTVERSION exists, % CMDEXTVERSION % is expanded to the string expression of the current value of CMDEXTVERSION; otherwise, you will get the 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.