Common symbols for Windows batch processing 1. @ echo off, a command line ECHO shield, can shut down the ECHO of the entire batch processing command, but cannot shut down the echo off command itself, now we add @ before the echo off command to achieve the requirement that all commands do not display back. The purpose of this symbol in batch processing is to disable the echo of the current line command, that is, to show only the result of the command without displaying what command is being executed! 2.> the redirection returns the result of the previous command execution to the specified device or file later. The specified file or standard output (stdout -- default is the system console ), if the file contains data, it will overwrite. > The redirection code www.2cto.com needs to append the data to the specified output file without overwriting the original content. <Input redirection redirects the source of input information to a specified device or file. By default, the system receives keyboard input. > & Write the output of one handle to the input of another handle <& exactly> & on the contrary, read the input from one handle and write it to another handle output.
Common handles 0, 1, 2, undefined handles 3-9 among which 1 and 2 represent the input and output addresses of a data stream (nt cmd calls it a handle, and MSDOS calls it a device ). Handle 0: standard input stdin, keyboard input handle 1: Standard output stdout, output to Command Prompt window (console, code is CON) handle 2: Standard Error stderr, stdin can be <redirected, stdout can be>,> redirected, while stderr cannot be directly redirected in DOS, you can only use ctty or other commands to transmit control of the system to other devices. Note: if it is used in the set/a statement,> indicates the group,> indicates the logical shift.
Dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- redirection) rem =================== redirect character ================================ rem outputs the content of the current file to the specified file tb.txt instead of the console type d: \ test \ bak \ tt.txt> d: \ test \ bak \ tb.txt rem: place the specified string "Hello China" (note the last space) write to the specified file echo Hello China> d: \ test \ bak \ tc.txt www.2cto.com rem ================== redirect character >============= ====== = Echo hello> d: \ test \ bak \ td.txt echo world> d: \ test \ bak \ td.txt rem =================== redirect character <============ ====== rem reset the current time echo 2010-01-12> d: \ test \ bak \ te.txt date <d: \ test \ bak \ te.txt del d: \ test \ bak \ te.txt rem =================== redirect character <============ ====== rem if an e-disk exists, the following file information is output to te.txt, and the error information is output to err.txt dir e: \ 1> d: \ test \ bak \ te.txt 2> d: \ test \ bak \ err.txt @ pause 3. | command pipeline command, which uses the output of | previous commands as | the input of subsequent commands. Format: The First Command | the second command [| the third command...] dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- pipeline command)
Rem ======================= pipeline command |============================ rem will display tom001 tom002 rm d: \ test \ bak \ t001.txt echo tom001> d: \ test \ bak \ t001.txt echo neil001> d: \ test \ bak \ t001.txt echo tom002> d: \ test \ bak \ t001.txt echo neil002> d: \ test \ bak \ t001.txt echo jerry> d: \ test \ bak \ t001.txt type d: \ test \ bak \ t001.txt | find "tom" www.2cto.com @ pause 4. ^ escape character to the special character "<", ">", "&". In the command, remove the special functions of the preceding three symbols, use them as symbols instead of their special meanings. In addition, this escape character can also be used as a continuation symbol. Dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- escape characters) rem ======================= Escape Character ================================= rem escape output hello china> d: \ test \ bat \ t.txt echo hello china ^> d: \ test \ bat \ t.txt rem is used to resume the row and output the result as hellochinaluchunli echo hello ^ china ^ luchunli www.2cto.com @ pause 5. & the combined command allows more than two different commands to be used in one line. If the First Command fails to be executed, the subsequent command will not be affected.. Syntax: The First Command and the second command [& the third command...] & combined commands use this method to execute multiple commands at the same time. When an error occurs, the subsequent commands will not be executed. If there is no error, all command syntaxes will be executed: the first command & the second command [& the third command...] this command is similar to the preceding one, but the difference is that when the first command fails, the subsequent command will not be executed.
| The combined command can execute multiple commands at the same time. If a command fails, the second command will be executed. If a correct command is executed, the subsequent commands will not be executed, if no correct command is displayed, all commands are executed. Syntax: The First Command | the second command [| the third command...] tip: when using the combined command and redirection command, you must note that the priority of the pipeline command is higher than that of the redirection command. The priority of the redirection command is higher than that of the combined command. Dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- Combined Command) rem ========================= combined commands ================================= dir d: \ test \ aa & dir d: \ test \ all rem k disk does not exist does not affect Command Execution dir k: \ & dir d: \ test \ all www.2cto.com rem ====================== combined commands === the rem k disk does not exist and the program can no longer execute dir k: \ & dir d: \ test \ all rem ==================== combined command ||====== ============== The dir after the execution error does not exist on the rem k disk. the dir k: \ | dir d: \ test \ all rem ======== combined use of combined commands and redirection commands ====== dir c :\& dir d :\> d: \ test \ bak \ t002.txt the command execution result on rem only the directory under the D Drive is recorded in t002.txt. The directory under the rem drive is not output as expected, but is printed on the console and changed to rem and output dir c respectively: \> d: \ test \ bak \ t003.txt & dir d :\>> d: \ test \ bak \ t003.txt @ pause 6. "" Double quotation marks can contain spaces in strings. Cd "program files" cd progra ~ 1 cd pro * the above three methods can be used to enter the program files directory 7. The comma is equivalent to a space. In some cases, "," can be used as a space. For example, dir, c: \ 8 .; when commands are the same, you can use ";" to isolate different targets, but the execution effect remains unchanged. If an error occurs during execution, an error report is returned. For example: dir c: \; d: \; e: \; f: \ Dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- semicolon) rem ================== semicolon; ======================== rem c d e f disk exists, and the correct execution is performed, showing the directory dir c under each disk: \; d :\; e :\; f: \ echo ------------------- If the rem z disk does not exist, the program returns an error. If d e f does not execute dir z: \; d :\; e: \; f: \ www.2cto.com @ pause
9.% the batch variable pilot character represents a parameter, which is a string added after the file name when a batch file is run. Reference variables with % var %, call program external parameters with % 1 to % 9 and so on. % 0% 1% 2% 3% 4% 5% 6% 7% 8% * is the parameter % 0 passed to the batch processing file by the command line, including the complete path and extension % 1 the first parameter % 9 the ninth parameter % * All parameter % 0 starting from the first parameter has special features, you can call the batch processing itself, in order to achieve the purpose of batch processing itself, you can also copy the file itself and so on. % Batch variable pilot. Set str = the value of the abc echo variable str is % str %. You can also use the Escape Character echo % to output a % echo % to output two %
Dos code www.2cto.com @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- variable pilot) rem ================ variable guide character % ===================================== rem % 0 indicates the output path name of the current file and the file name echo % 0 rem can be passed through 0laibeibeibeiself (copy the content of the current file to tt.txt) copy % 0 d: \ test \ bak \ tt.txt rem can accept input variables by setting parameters. However, if there is no space rem such as: set param001 = % 1, no result is obtained, only parameters accepted by set param001 = % 1 rem batch processing, such as test. ba T tom is boy set param001 = % 1 set param002 = % 2 set param003 = % 3 rem output tom is boy echo % param001 % param002 % param003 % rem % only accept % 1 to % 9 nine parameters, if the parameter is set to 9, it is unacceptable. You need to use the shift command, see the shift command to explain the usage of rem % and %. rem = There cannot be spaces at both ends. set str = abc echo str. The value of str is % str % rem. Output one % Two % three % respectively. echo % www.2cto.com echo hello> d: \ test \ bak \ t004.txt echo china> d: \ test \ bak \ t004.txt echo how> d: \ test \ bak \ t004.txt echo are> D: \ test \ bak \ t004.txt echo you> d: \ test \ bak \ t004.txt rem output hello china how are you for/f % I in (d: \ test \ bak \ t004.txt) do echo % I rem Output a B c for % I in (a B c) do echo % I @ pause 10 in sequence. () parentheses have special functions in batch processing programming. The left and right parentheses must be used in pairs. The parentheses can contain multiple-line commands. These commands are considered as a whole and considered as a command line. Parentheses are common in for statements and if statements. They are used to nest loop or condition statements. In fact, Parentheses () can also be used independently. Note: such multiple commands are considered as a command line. If there is a variable in it, the variable delay problem is involved. In the for and if statements, the requirements for the statement format are: 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 row. 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, whether the parentheses in statement 2 are retained or not is similar to the above.
Dos code @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- parentheses) rem ============================= parentheses () ============================= rem output 1 2 3 each occupies a single echo 1 & echo 2 & echo 3 echo --------------( www.2cto.com echo 1 echo 2 echo 3) @ pause 11 .! The exclamation point is used to represent the variable in the variable delay problem, that is, % var % should be expressed! Var !, See setlocal introduction. In the set/a unary operator, any line of character whose logic is not 12.: Colon starting with Colon: is regarded as a label in batch processing, and all subsequent content is ignored directly. Valid label: the colon is followed by a string starting with a letter or number. A goto statement can recognize it, indicating that the row is a tag and the subsequent content is a tag segment. For example, test indicates that the content under test is the tag segment, and test is the name of the tag segment. You can use goto test and goto: test jumps to this label segment or calls this sub-process with call: test. 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, which is invalid: A colon followed by a special non-alphanumeric symbol. A goto unidentifiable label can be used as a comment. Therefore, it is often used as a comment symbol. In fact: + annotations can also be used.
Dos code www.2cto.com @ echo off @ rem echo close command display, @ disable echo off display rem ==================== set the title = rem-use the file name as the title to test the bat file (test. bat -- colon) rem =============================== Colon: ============================ rem as the label Segment set num = 3 set count = 4 set/a flag = num + 1:: about determining equality if no goto end is added, if % count % = % flag % goto begin goto end: begin echo This is begin: end echo This is end rem truncation/replacement Character set str = luchunli set Var001 = % str :~ 2,7% rem output chunli echo % var001 % set var002 = % str: u = r % rem output lrchrnli echo % var002 % @ pause 13.: The annotation symbol indicates that the content of this row is the comment content.: It is an invalid tag name. Adding spaces can also serve as a comment.: The function is the same as the comment command rem. But there are several differences (1): The character lines after the echo are not displayed during execution, whether or not the echo on command line echo status is enabled. (2) Some command symbols in the rem comment statement, such as the redirection symbol and pipeline symbol, will still be executed. If you use: to comment, 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. Www.2cto.com (3) rem can be used in the config. sys file. Dos code rem ================== comment the command symbol :: ====================== neither of the following rem statements will execute rem tt> d: \ test \ bak \ t.txt :: ttb> d: \ test \ bak \ t.txt 14. ~ Used with the colon (:) to intercept strings. In the set/a statement, it is a unary operator, indicating that the operand 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, which is 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. 15. +-*/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) 16.equ neq lss leq gtr geq Ter than or javasdos code rem =========== equ neq lss leq gtr geq ============= set str001 = tom www.2cto.com set str002 = tom rem equ output equal if % str001 % equ % str002 % echo equal 17. point (.) and double point (..) one vertex represents the current directory; two vertices represent the upper-level directory. 18. Question mark (?) Matching any character in the path indicates that any unified character is followed by/, which indicates obtaining help, such as if /? And set /? Author: tyou-JAVA