Introduction to commonly used commands in batch processing (Echo, REM, goto, call, pause, if, for) _dos/bat

Source: Internet
Author: User
Tags echo command goto
One, echo command (Echo graphic version)

1. Echo: Displays the status of the current echo: echo on or echo off.
2. The ECHO On:echo state is set to on, and displays the command line (such as the c:\> before each row, and similar flags).
3. The ECHO Off:cho state is set to off, and the command line will not be displayed (such as c:\> before each row).
4. ECHO string: Displays the input string on the cmd screen.
5. ECHO string &echo string ... : A logical operation, similar to and, used to display multiple rows of data.
6. ECHO string > file path and filename: Writes the string to the file, if the existing data will be emptied and written.
7. ECHO string >> file path and filename: Writes a string to the file, if the data is appended to the end.
Note Each time: Echo string >> file path and file name, after the command, Echo automatically adds a line break after the string, so if you want to add it directly to the end of the file, the data already in the file cannot be entered by the Echo command (or manually delete swap line characters).
8. (ECHO string &echo string ...) > File path and file name:
Write multiple rows of data in a file (note that parentheses cannot be removed, and if parentheses are removed, only the last one will be written, and the preceding output is in the cmd window). If there is already data in the file, it will be automatically emptied before writing.
9. (ECHO string &echo string ...) >> path and filename
Write multiple rows of data in a file (note that parentheses cannot be removed, and if parentheses are removed, only the last one will be written, and the preceding output is in the cmd window). If there is already data in the file, append the string to the end of the file.
(ECHO string) >> file path and filename: Appends a string to another line in the file. As shown in the figure:
ECHO. : Output blank line, which is equivalent to entering a carriage return
Notice the "in the command line. "There must be no spaces in the middle of the echo, otherwise". "will be output to the screen as a cue message. In addition ". "Can be used,:; + any symbol substituted."
echo Reply | command file name: Automatic reply to a question in a command without human input
ECHO ^g: "^g" is a ctrl+g type of conjoined symbols, input multiple ^g can produce multiple beeps.

Second, pause command: Line This sentence will be suspended

The @ command: echo off image, but it is added to the front of the other command line, indicating that the runtime does not display the command line itself.
Four, call order

Call invokes another batch file (if another batch file is called directly, the current file successor command cannot be executed after the file is executed)
V. REM command
REM indicates that the word identifier after this command interprets the line, does not execute, but only gives its own future lookup. :: Can also play a role in the annotation of REM, and more concise and effective;

Six,% order
% represents a parameter, which is a string that is added after the file name when the batch file is run. A variable can represent the file name itself from%0 to%9,%0, and the string is represented in the order of%1 to%9.
(1) Application of%0:
DOS command line input: AA (on behalf of executing Aa.bat batch program),% 0 represents the file itself, which shows a dead loop that executes the Aa.bat command continuously

(2)%1 to%9 application


Vii. Control command
(1) If "parameter" = = "string" command to be executed

= = equal to, equ equal to, neq not equal to; LSS less than;
EQ is less than or equal to; GTR greater than; Geq greater than or equal to
(2) if exist the name of the command to be executed (eg: edit Aa1.txt file)

(3) if errorlevel number of orders to be executed
If the return code equals the specified number, the condition is set, the command is run, or the next sentence is run. If the IF errorlevel 2 goto X2,dos program runs, it will return a number to DOS, called the error code ERRORLEVEL or return code.

(4) OR [%%f] in (set) do [command]
For%%c in (*.bat *.txt) do type%%c

Eight, directory switching
(1) CD [disk] [path]: Switching the same partition

(2) cd/d [disk] [path]: switching between partitions

(3)%cd%: Display the current full path

Nine, directory switch dir (slightly, check yourself)

X. ATTRIB command, setting file properties

Xi. Common Command Commands (abbreviated)
Del: Deleting files
Copy: Copying files
Xcopy: Copying folders
Md: Creating folders
RD: Deleting folders
ren: Renaming files (clips)
Move: Moving files (clips)
Find: String Lookup
FINDSTR: String lookup enhancement for pattern matching searches
12. Common network commands and symbolic articles
The pipeline command has precedence over the redirection command, and the redirection command has precedence over the combination command: |, >, &&
(1) redirect >, >>
The > redirector, which means passing and overwriting, is the effect of passing the results of the run to a later range (either a file or the default system console, or a command prompt).
The >> redirector appends the output of the command to the device behind it. The role of this symbol and > is somewhat similar, but they are different >> are
Passed and appended at the end of the file, while > is overwritten. Use the same as >.
Echo hello> 1.txt
Echo World>>1.txt
(2) Command pipe character |
Echo y|rd/s c:\abc, through Pipe command |, passes the results of Echo y to the rd/s c:\abc command, which is the purpose of automatically entering Y.
(3) Combination Command &, &&, | |
<a>:&amp, the symbol allows simultaneous execution of multiple commands, when the first command fails and does not affect the execution of the command behind it. Here & the commands on both sides are executed sequentially, from the point of departure.
The <B>&& symbol allows you to execute multiple commands at the same time, and you will no longer execute the following commands when you encounter a command that executes the error, and all commands are executed if there is no error.
The following command is not executed after the correct command, and all commands are executed if the correct command is not present.
(4) Escape character ^
To output special characters, you need to use echo ^>, echo ^|, Echo ^|^|, echo ^^ ... To deal with.
(5) Variable guide%
① variable reference: When the percent sign appears in pairs and contains non special characters, such as%var%,%str%
② a reference to a formal parameter,%0 Represents the batch file itself, including the full path and extension;% 1 to%9 represents the first argument to the Nineth parameter.
③SET/A statement, which represents the division of two numbers for the remainder. In the Command line window, you need only a single%, and in a batch file, you need a sequential Party semicolon, which is written as%.
set/a num=4%%2, get num 0 instead of 2.
④ Escape symbol: If you want to display the% itself, you need to escape it in the front with%.
echo a percent semicolon:% percent
Echo Two percent sign:%%%%
Echo Three percent sign:%%%%%%
(6) The definition character ""
① always use "" to enclose a path in a path that represents a space or special symbol.
Example 1:cd/d CD "D:\Program Files\^%*abc"
Because there are spaces and special symbols in the path, enclose the path with "".
② indicates that the content is a complete string.
Example 2:set "Var=abc 123"
Echo%var%

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.