CMD batch processing commonly used symbol detailed _dos/bat

Source: Internet
Author: User
Tags echo b goto
1, @
After it is generally followed by a command or statement, the command or statement itself is not displayed on the screen when executed. Please save the following code as a test.cmd file and run it to compare the output differences of the two ECHO statements on the screen:
echo A
@pause
@echo b
@pause
The results of the implementation are as follows:
C:\Documents and settings\jm\ Desktop >echo a
A
Please press any key to continue ...

Please press any key to continue ...
2,%,%%
Percent semicolons are used on different occasions and have different meanings:
① when the percent sign appears in pairs and contains non special characters, it usually does variable reference processing, such as:%var%,%str%. Save the following code as a batch file, and watch the screen display results after running:
@echo off
Set STR=ABC
The value of the echo variable str is:%str%
Pause
The result is displayed on the screen:
The value of the variable str is: ABC
Press any key to continue ...
In addition, the percent semicolon as a variable reference also has a special form, that is, a reference to the formal parameter, at which point, after a single percent of the 0~9 10 digits, such as%0,%1, where%0 is the name of the script itself,% 1 to%9 is the second to nineth parameter ... The maximum number of support%0~%9,%10 is the variable reference, that is, the value%15%1 is connected to 5.
Take a look at the demo code:
@echo off
If defined str goto next
Set str=
set/p str= Please pull the file into this window and return to:
Call "%~0"%str%
Pause
Exit
: Next
Cls
echo This batch file full path is: "%~0"
echo the full path of the file dragged to this window is: "%~1"
Goto:eof
When ② appears in the SET/A statement, represents a division of two numbers, the so-called modulo operation, which is slightly different in the command Line window and in the batch file: in the command-line window, only a single% is required, and in the batch file, a sequential Party semicolon is required, written as%.
For example, when you run set/a num=4%2 in a command-line window, the result displays 0 because the remainder of 4 divided by 2 is 0; If you save it as a batch file, the statement changes slightly:
@echo off
set/a num=4%%2
Echo 4 divided by the remainder of 2 is%num%
Pause
③ Escape symbol: If you want to display the% itself, you need to escape it in the front with%. For example:
@echo off
echo a percent semicolon:% percent
Echo Two percent sign:%%%%
Echo Three percent sign:%%%%%%
Pause
3,:,::
① to: The beginning of a single: Indicates that the row is a label, after which the content is a label segment, such as: test, it means: the content under test is a label segment, and test is the name of this label segment, you can use goto test, goto:test jump to the label paragraph or use call: Test calls the child procedure, and two consecutive colons begin to indicate that the contents of the row are commented, in effect:: An invalid label name: the addition of a space can also serve as an annotation, at which point:: The function and Annotation command rem; however, REM Some command symbols in the comment statement, such as redirection symbols and pipe symbols, are still executed. And if used:: To Annotate, with:: The same line of all the commands or symbols are directly ignored by the command interpreter, virtually improve the compatibility of annotations and the implementation of the entire program efficiency, and in many command statements more eye-catching, Therefore, note statements recommend the use of:: Format.
② in the SET statement: and ~ when used concurrently: function to intercept a string. Assuming set STR=ABCDE, then the set var=%str:~0,1% represents the first character of the Intercept string abcde, and = The function of the replacement string when used concurrently. Assuming: Set Str=abc:de, then the set var=%str:a=1% means that replacing a A in string Abc:de with 1,set var=%str::=2% means replacing the string abc:de with 2;
4, ~
① used in the SET statement, and: When used at the same time, play the function of intercepting the string, please refer to the explanation of the previous article;
When ② is used in a set/a statement, it is a unary operation symbol that represents the bitwise negation of the operand, for example, the result of the set/a num=~1 execution is -2,set/a num=~0 1
③ is used in a for statement, which means that the enhanced for function can extract more information. For example: In a For statement in a batch file:%%~i represents the removal of the first external quotation mark,%%~zi indicates the size of the file (in bytes),%%~ni to get the file name,%%~xi to get the extension (with dot) ... They can be used in combination, such as%%~nxi to get the filename and suffix name.
5, >, >>
In general,> means to overwrite the contents of the original file with the new content,>> to append content to the original file, at which point they appear as redirected symbols, and if used in set/a statements, the > represents a grouping,>> represents a logical shift;
6, |
In general, it appears as a pipe symbol, representing the execution of the command or statement before it as the processing object of the command or statement after it, in short, by taking its previous output as the input after it, for example: Echo abcd|findstr "B", which means that the Echo The result of ABCD, as the executing object of findstr "B", is to look for B characters in the string abcd, and if there is an ABCD string in the Test.txt, the statement will have the same effect as the findstr "B" test.txt;
7. ^
Generally, ^ appears as an escape character. Because in the CMD environment, some characters have special functions, such as >, >> for redirection, | For pipes,&, &&, | | Represents a statement connection ... They all have specific functions, and if you need to output them as characters, echo >, echo | ...... And so on, it's going to go wrong.--cmd interpreters treat them as characters with special functions, instead of being treated as ordinary characters, this time, you need to escape processing these special characters: Add the escape characters before each special character ^, so to output these special characters, you need to use the Echo ^> , Echo ^|, Echo ^|^|, echo ^^ ... To deal with the format;
8, &
Generally speaking,,& represents the meaning of two commands or statements executed concurrently. such as Echo A&echo B, will display both A and B characters on the screen. When several statements have the same meaning or function and do not have sequential order, enabling & symbolic connection will increase the readability of the program;
9, &&, | |
This is a pair of diametrically opposed command characters,&& means that if the previous statement succeeds, it executes the statement after it, and | | means that if the previous statement fails, the statement after it is executed, and in some cases they can replace if......else ... Statement, for example:
@echo off
MD Test&&echo successfully created folder Test| | Echo Create folder test failed
Pause
The effect is equivalent to the following code:
@echo off
MD Test
If "%errorlevel%" = = "0" (Echo creates folder test successfully) Else echo creates folder test failed
Pause
10, ()
Parenthesis pairs often appear in the for and if statements, and there are specific occasions in which the statement format is required in the for and if statements, for example:
①for%%i in (statement 1) Do (statement 2): In this statement, Statement 1 must be surrounded by parentheses, and the parentheses of statement 2 are discarded or reserved if the statement 2 is a single statement or with &, & &, | | The multiple statements connected by such a connection symbol, which can be discarded, and if statement 2 is a logical sequence of multiple statements, you must preserve the pair of parentheses, and multiple statements must be written with a line break, for example:
@echo off
For%%i in (a b c) do echo%%i&echo--------
Pause
can also be rewritten as:
@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 of statement 1 are dispensable; if there is an else part, the parentheses in statement 1 must be preserved, and the parentheses in statement 2 are similar to the previous one. For example:
@echo off
If exist test.txt echo current directory has test.txt
Pause
@echo off
If exist Test.txt (with Test.txt in the current directory of ECHO) else echo is not in the current directory Test.txt
Pause
@echo off
If exist Test.txt (there is test.txt in the current directory of Echo) Else (
Echo does not have test.txt under current directory
Pause
Cls
Echo is about to create a test.txt file
Cd.>test.txt&&echo successfully created Test.txt
)
Pause
③ the use of parentheses in specific situations not only makes the code logically clear, enhances readability, but also reduces the amount of code that can be used. For example, when using the Echo statement to construct multiple lines of text:
@echo off
(
Echo First line
echo Second Line
Echo Third Line
) >test.txt
Start Test.txt
If you do not use parentheses, you need to use the following code:
@echo off
Echo First line >test.txt
echo Second line >>test.txt
echo Third line >>test.txt
Start Test.txt
11, + 、-、 *,/
In set/a statements, the meanings of these symbols are: add, subtract, multiply, divide. For example: set/a NUM=1+2-3*4/5. It should be noted that these operators follow the order of precedence in mathematical operations: multiplication and subtraction, preceded by parentheses, and, directly ignoring the decimal point, so the result of that formula is 1 instead of 0 or 0.6.
In addition, it is possible to see the wording in the code: set/a num+=1, set/a num-=1, set/a num*=1 and set/a num/=1, these expressions are cumulative, tired, tired, tired, step is 1, after the expansion of the complete writing as follows: set/a Num=nu M+1, set/a num=num-1, set/a num=num*1, and set/a NUM=NUM/1 (set/a statements, variable references can ignore percent pairs or exclamation pairs, set/a num=%num%+1 and set/a num=num+ 1 equivalent)
12, Equ, NEQ, LSS, Leq, GTR, GEQ
These are the numeric comparison symbols commonly used in the IF statements, which are derived from the English key letters, meaning:
Command symbolic meaning in English
EQU equals equal
NEQ not equal to not equal
LSS less than less than
Leq is less than or equal to less than or equal
GTR is larger than greater than
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.