Detailed explanation of common symbols in CMD batch processing

Generally, it is followed by a command or statement, and the command or statement itself will not be displayed on the screen when it is executed. Please save the following code as a test.cmd file and run it to compare the output differences between the two echo statements on the screen:
    echo a
    @echo b
The results are as follows:
C: \ documents and settings \ JM \ desktop > echo a
Please press any key to continue

Please press any key to continue

The percent sign is used in different occasions and has different meanings:
① When the percentage sign appears in pairs and contains non special characters, variable reference processing is generally performed, such as:% var%,% STR%. Save the following code as a batch file, and observe the screen display results after running:
    @echo off
    set str=abc
The value of echo variable STR is:% STR%
The result is displayed on the screen:
The value of variable STR is: ABC
Press any key to continue
In addition, as a variable reference, the percentage sign has a special form, that is, the reference to the formal parameter. At this time, a single percentage sign is followed by 10 numbers, such as% 0 and% 1, where% 0 is the name of the script itself, and% 1 to% 9 are the second to nine parameters. At most, it supports% 0 to% 9. After 10, it is the variable reference, that is,% 15 is the value of% 1 followed by 5.
See demo code:
    @echo off
    if defined str goto next
    set str=
Set / P STR = please pull the file to this window and enter:
    call “%~0” %str%
Echo the full path of this batch file is: ‘% ~ 0’
The full path of the file that echo dragged to this window is: ‘% ~ 1’
    goto :eof
② When it appears in the set / a statement, it means to divide two numbers and take the remainder, that is, the so-called modular operation. Its writing method in the command line window and batch file is slightly different: in the command line window, only a single% is needed, and in the batch file, two consecutive percentage signs are needed, written as%%.
For example, if you run set / a num = 4% 2 in the command line window, the result will show 0 because the remainder of 4 divided by 2 is 0; if you save as a batch file, the statement will change slightly:
    @echo off
    set /a num=4%%2
The remainder of echo 4 divided by 2 is% num%
③ Escape symbol: if you want to display% itself, you need to use% to escape. For example:
    @echo off
Echo a percent sign:%%
Echo two percent signs:%%%%
Echo three percent signs:%%%%%%
① Starting with: indicates that the line is a label, and the content after it is a label segment. For example, test indicates that the content under test is a label segment, and test is the name of the label segment. You can use goto test, goto: test to jump to the label segment or call the subprocess with call: test. Two consecutive colons indicate that the content of the line is comment content. In fact, the content of the line is comment content On,:: is an invalid label name,: plus a space can also be used as a comment. In this case, the function of: is the same as that of the comment command rem; however, some command symbols in the REM comment statement, such as redirection symbols and pipeline symbols, will still be executed. If you use:: to comment, all commands or symbols on the same line with:: will be directly ignored by the command interpreter, none Form improves the compatibility of comments and the execution efficiency of the whole program, and it is more eye-catching in many command statements. Therefore, the comment statement is recommended to use:: format.
② In the set statement: and ~ are used at the same time,: has the function of intercepting string. If set STR = ABCDE, then set var =% STR: ~ 0,1% represents the first character of the intercepted string ABCDE; when used together with = it can replace the string. Suppose: set STR = ABC: De, then set var =% STR: a = 1% means to replace a in string ABC: de with 1, and set var =% STR:: = 2% means to replace: in string ABC: de with 2;
① Used in SET statement, and: when used at the same time, it has the function of intercepting string. Please refer to the previous explanation;
② When used in a set / a statement, it is a unary operation symbol, indicating that the operation number is reversed bit by bit. For example, the execution result of set / a num = ~ 1 is – 2, and the result of set / a num = ~ 0 is – 1
③ In the for statement, it means to enhance the function of for and extract more information. For example, in the for statement of batch file:%% ~ I means to remove the first pair of outer quotation marks,%% ~ Zi means to get the size of the file (in bytes),%% ~ Ni means to get the file name,% ~ Xi means to get the extension (with dot) They can be used in combination, such as%% ~ nxi to get the file name and suffix.
In general, > means to overwrite the content of the original file with new content, > means to append content to the original file, at this time, they appear as redirection symbols; if they are used in set / a statements, > means grouping, > means logical shift;
Generally speaking, it appears as a pipe symbol, which means that the execution result of the command or statement before it is regarded as the processing object of the command or statement after it. In short, it means that the output before it is regarded as the input after it, for example, echo abcd| findstr “B”, which means that the execution result of echo ABCD is regarded as the execution object of findstr “B”, that is, the execution result of the word If there is an ABCD string in test.txt, the statement has the same effect as findstr “B” test.txt;
In general, ^ appears as an escape character. Because in the CMD environment, some characters have special functions, such as >, > > for redirection, | for pipeline, & &, & &, |, for statement connection They all have specific functions. If you need to output them as characters, echo >, echo | There will be errors in writing methods like this — the CMD interpreter will treat them as characters with special functions instead of ordinary characters. At this time, it needs to escape these special characters: add escape characters ^ before each special character, so to output these special characters, you need to use echo ^ >, echo ^|, echo ^|, echo ^ ^ And so on;
In general, & means that two commands or statements are executed at the same time. For example, echo a & echo B will display both a and B characters on the screen. When the meanings of several statements are similar or the functions are the same and there is no different order, enabling symbol connection will increase the readability of the program;
This is a pair of command symbols with opposite meanings, & & indicates that if the statement before it is executed successfully, the statement after it will be executed, and 𞓜, indicates that if the statement before it fails to execute, the statement after it will be executed; in some cases, they can replace if Else… Statement; for example:
    @echo off
MD test & & echo successfully create folder test | | echo failed to create folder test
The effect is equivalent to the following code:
    @echo off
    md test
If “% errorlevel%” = = “0” (echo successfully creates folder test), else echo fails to create folder test
Parentheses are often used in for statements and if statements, and there are also some specific occasions. In for and if statements, they belong to statement format requirements, such as:
① For%% I in (statement 1) do (Statement 2): in this statement, statement 1 must be surrounded by bracket pairs, while statement 2’s bracket pairs can be discarded or reserved as appropriate: if statement 2 is a single statement or multiple statements connected by &, & &, |, and other connecting symbols, bracket pairs can be discarded; if statement 2 is a collection of multiple statements with logical precedence, bracket pairs must be preserved Yes, and multiple statements must be broken; for example:
    @echo off
    for %%i in (a b c) do echo %%i&echo ——–
It can also be rewritten as:
    @echo off
    for %%i in (a b c) do (
        echo %%i
        &echo ——–
② If condition (statement 1) else (Statement 2): if there is no else part, the bracket pair of statement 1 can be optional; if there is else part, the bracket pair of statement 1 must be reserved. At this time, whether the bracket pair of statement 2 is reserved or not is similar to the previous point. For example:
    @echo off
If exist test.txt echo there is test.txt in the current directory
    @echo off
If exist test.txt (there is test.txt in echo current directory) else echo there is no test.txt in current directory
    @echo off
If exist test.txt else(
Echo there is no test.txt in the current directory
Echo is about to create the test.txt file
CD. > test.txt & & echo successfully created test.txt
③ The use of bracket pairs in specific situations can not only make the code logic clear, enhance readability, but also reduce the amount of code. For example, when using echo statements to construct multiple lines of text content:
    @echo off
Echo first line
Echo line 2
Echo line 3
    start test.txt
If you do not use bracket pairs, you need to use the following code:
    @echo off
Echo first line > test.txt
Echo line 2 > > test.txt
Echo line 3 > > test.txt
    start test.txt
In the set / a statement, the meanings of these symbols are: add, subtract, multiply and divide. For example: set / a num = 1 + 2-3 * 4 / 5. It should be noted that these operation symbols follow the priority order in the mathematical operation: multiplication and division followed by addition and subtraction, parentheses were calculated first, and the decimal point was directly ignored. Therefore, the result of the formula just now is 1 instead of 0 or 0.6.
In addition, it is possible to see the following writing methods in the code: set / a num + = 1, set / a num – = 1, set / a num * = 1 and set / a num / = 1. These represent accumulation, subtraction, cumulative multiplication and cumulative division. The steps are all 1. After expansion, the complete writing methods are: set / a num = num + 1, set / a num = num-1, set / a num = num * 1 and set / a num = num / 1 Or exclamation mark pair, set / a num =% num% + 1 is the same as set / a num = num + 1)
These command symbols are common numerical comparison symbols in if statements. They are taken from the key letters of English. Their specific meanings are:
Meaning of command symbols
Equ equals equal
NEQ is not equal to not equal
LSS less than
Less than or equal to Leq
GTR greater than greater than
GEQ is greater than or equal to greater than or equal