Detailed explanation of common symbols for cmd Batch Processing

Source: Internet
Author: User
Tags echo b

1 ,@
Generally, a command or statement is followed by a command. The command or statement itself is not displayed on the screen. Save the following code as the test. cmd file and run it to compare the output differences between the two echo statements on the screen:
Echo
@ Pause
@ Echo B
@ Pause
The execution result is as follows:
C: \ Documents and Settings \ JM \ Desktop> echo
A
Press any key to continue...

Press any key to continue...

2. %, %
Percent signs are used in different occasions and have different meanings:
① When the percent sign is paired and contains non-special characters, it is generally used for variable reference processing, such as % var % and % str %. Save the following code as a batch processing file and observe the displayed result after running the Code:
@ Echo off
Set str = abc
Echo variable str value: % str %
Pause
The following result is displayed on the screen:
The value of the str variable is abc.
Press any key to continue...
In addition, the percent sign is also used as a variable reference, that is, the reference to the form parameter. At this time, a single percent sign is followed by 0 ~ 9 These 10 numbers, such as % 0 and % 1, where % 0 is the Script Name and % 1 to % 9 is the second to nine parameters... supports up to % 0 ~ % 9, % 10 is referenced by the variable, that is, the value of % 15 is % 1 connected to 5.
See the Demo code:
@ Echo off
If defined str goto next
Set str =
Set/p str = pull the file to this window and press Enter:
Call "% ~ 0 "% str %
Pause
Exit
: Next
Cls
Echo full path of this batch of Files: "% ~ 0"
The complete path of the echo file dragged to this window is: "% ~ 1"
Goto: eof
② When it appears in the set/a statement, it indicates that the two numbers are separated by the remainder, that is, the so-called modulo operation. It is slightly different in the syntax of the command line window and the batch file: in the command line window, only a single % is required. In the batch file, two percentage signs must be written as %.
For example, if set/a num = 4% 2 is run in the command line window, 0 is displayed, because the remainder of 4 divided by 2 is 0. If it is saved as a batch file, this statement will slightly change:
@ Echo off
Set/a num = 4% % 2
The remainder of echo 4 divided by 2 is % num %
Pause
③ Escape Symbol: If % itself is to be displayed, % must be used for escape before. For example:
@ Echo off
Echo a percent sign: %
Echo Two percentage signs: %
Echo Three percentage signs: %
Pause 3 ,:,::
① A single header: indicates that the row is a tag, and the content after it is a tag segment. For example, test indicates that the content under test is a tag segment, test is the name of the tag segment. You can use goto test and goto: test to jump to the tag segment or call the sub-process with call: test; two colons in a row indicate that the content of the row is the comment content. In fact,: is an invalid label name. Adding spaces can also be used as a comment. At this time ,:: the function is the same as the annotation command rem; however, some command symbols in the rem annotation statement, such as the redirection symbol and the pipeline symbol, will still be executed. If you use: to annotate, and :: all commands or symbols in the same line are directly ignored by the command interpreter, which virtually improves the compatibility of comments and the efficiency of executing the entire program, and is more eye-catching among the numerous command statements, therefore, the format of: is recommended for comment statements.
② In the set statement: And ~ At the same time, it is used to intercept strings. If set str = abcde, set var = % str :~ 0, 1% indicates that the first character of the string abcde is truncated; and = indicates that the string is replaced. Suppose: set str = abc: de, then, set var = % str: a = 1% means to replace a in the string abc: de with 1, set var = % str :: = 2% represents replacing: In the string abc: de with 2; 4 ,~
① Used in the set statement, and: used at the same time, to intercept the string function, please refer to the previous explanation;
② When used in the set/a statement, it is the unary operator number, indicating that the Operation number is reversed by bit, for example, set/a num = ~ The execution result of 1 is-2, set/a num = ~ The result of 0 is-1.
③ Used in the for statement to enhance the for function and extract more information. For example, in the for statement of the batch processing file: % ~ I indicates removing the first pair of outer quotation marks, % ~ Zi indicates the file size (in bytes), % ~ Ni indicates obtaining the file name, % ~ Xi indicates getting the extension (with a dot number )...... They can be used in combination, for example, % ~ Nxi indicates obtaining the file name and suffix. 5. >,>>
In general,> indicates to overwrite the content of the original file with the new content,> indicates to append the content to the original file. At this time, they appear as redirection symbols. If they are used in the set/a statement, then> indicates the group,> indicates the logical shift; 6. |
Generally, it appears as a pipeline symbol, indicating that the execution result of the command or statement before it is used as the processing object of the command or statement after it, in short, the previous output is used as the input after it, for example, echo abcd | findstr "B", which indicates that the execution result of echo abcd is used as the execution object of findstr "B". If test.txt contains an abcd string, the statement has the same effect as findstr "B" test.txt; 7. ^
In general, ^ appears as an escape character. In the cmd environment, some characters have special functions, such as>,>, which indicate redirection, | which indicates pipeline, &, &, and | which indicates statement connection ...... They all have specific functions. If you need to output them as characters, echo>, echo | ...... There will be errors in writing such as -- the cmd interpreter treats them as characters with special functions rather than normal characters. At this time, escape the special characters: add the Escape Character ^ before each special character. Therefore, output these special characters, echo ^>, echo ^ |, echo ^ |, echo ^ ...... Format; 8 ,&
In general, & indicates the simultaneous execution of two commands or statements. For example, echo a & echo B will display both a and B characters on the screen. When several statements have similar meanings or have the same functions but have no sequential order, enabling & Symbol connection will increase the readability of the program; 9. &, |
This is a pair of command operators with the opposite meaning. & indicates that if the previous statement is successfully executed, the subsequent statement will be executed, and | indicates that if the previous statement fails to be executed, the statement after it is executed; in some cases, they can replace if ...... Else ...... Statement, for example:
@ Echo off
Md test & echo succeeded in creating the folder test | echo failed to create the folder test
Pause
The effect is equivalent to the following code:
@ Echo off
Md test
If "% errorlevel %" = "0" (echo successfully created the folder test) else echo failed to create the folder test
Pause 10 ,()
Parentheses are often used in for statements and if statements, but they also have some special situations. for and if statements, they must be in the statement format. for example:
① For % I in (statement 1) do (Statement 2): in this statement, Statement 1 must be surrounded by parentheses, the parentheses in statement 2 can be discarded or retained as needed: If Statement 2 is a single statement or multiple statements connected with the &, &, | and other connection symbols, parentheses can be discarded. If Statement 2 is a set of multiple statements with a logical order, the parentheses must be retained, and multiple statements must be written in a broken line. For example:
@ Echo off
For % I in (a B c) do echo % I & echo --------
Pause
You can also rewrite it:
@ Echo off
For % I in (a B c) do (
Echo % I
& Echo --------
)
Pause
② If condition (statement 1) else (Statement 2): if there is no else part, the parentheses in statement 1 are dispensable. if there is an else part, the parentheses in statement 1 must be retained, at this time, the parentheses in statement 2 are retained or not, which is similar to the above. For example:
@ Echo off
If exist test.txt echo is included in the current directory test.txt
Pause
@ Echo off
If exist test.txt (echo has test.txt under the current directory) else echo does not have test.txt under the current directory
Pause
@ Echo off
If exist test.txt (echo current directory contains test.txt) else (
Echo does not have test.txt in the current directory.
Pause
Cls
Echo will create the test.txt File
Cd.> test.txt & echo successfully created test.txt
)
Pause
③ Using parentheses in a specific scenario can not only make the code logic clear, enhance readability, but also reduce the amount of code. For example, when the echo statement is used to construct multi-line text content:
@ Echo off
(
Echo first line
Echo Line 2
Echo Row 3
)> Test.txt
Start test.txt
If you do not use parentheses, use the following code:
@ Echo off
Echo line 1> test.txt
Echo line 2> test.txt
Echo Row 3> test.txt
Start test.txt 11, + ,-,*,/
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 operators follow the priority order in mathematical operations: First multiplication, division, addition, and subtraction, parentheses are included, and the decimal point is directly ignored, the result of the formula is 1 instead of 0 or 0.6.
In addition, you may see the following code: set/a num + = 1, set/a num-= 1, set/a num * = 1 and set/a num/= 1, these indicate accumulation, subtraction, multiplication, division, step size is 1, and the complete method after expansion is as follows: set/a num = num + 1, set/a num = num-1, set/a num = num * 1 and set/a num = num/1 (set/a statement, variable reference can ignore percent or exclamation point pairs. set/a num = % num % + 1 is equivalent to set/a num = num + 1) 12. equ, neq, lss, leq, gtr, and geq
These operators are commonly used numerical comparison symbols in the if statement. They are taken from key English letters. The specific meanings are as follows:
Meaning of command symbols
EQU equals equal
NEQ is not equal to not equal
Less LSS than less
LEQ is less than or equal to less than or equal
GTR greater
GEQ is greater than or equal to greater than or equal

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.