Brief introduction of common special symbols for batch processing _dos/bat

Source: Internet
Author: User
Tags stdin

Common special symbols for batch processing

1, @ command line echo screen character
2,% Batch variable guide
3, > redirection character
4, >> redirection character
5, <, >&, <& redirection character
6, | Command pipe character
7. ^ Escape characters
8, & Combination command
9, && Combination command
10, | | Group command
11, "" "String definition character
12., comma
13,; Semicolon
14, () bracket
15,! Exclamation number
16. Other special tags that may be seen in batch processing: (abbreviated)
CR (0D) command line terminator
Escape (1B) ANSI escape character guide
Space (20) commonly used parameter-defining characters
Tab (09); = Non-commonly used parameter-defining characters
+ Copy command file connector
* ? File wildcard characters
/Parameter Switch guide
: Batch Label Guide

Cut the crap and talk.

1, @ command line echo screen character

This character is meant to turn off the echo of the current line in batch processing. We know from the first few lessons
echo off can turn off the echo of the entire batch command, but you can't turn off this command, and now we're going to add a @ to the echo off command so that all the commands don't echo.

2,% Batch variable guide

This percent sign is strictly not a command, it is only a batch of parameters (except for the use of multiple%, in the future will be described in detail).
Reference variable with%var%, calling program external parameters with% 1 to%9 and so on
%0%1%2%3%5%6%7%8%9 the parameters passed to the batch for the command line
%0 The batch file itself, including the full path and extension
%1 first parameter
%9 nineth parameter
%* all parameters starting with the first argument
Parameter%0 has special functionality to invoke the batch itself to achieve the purpose of the batch itself loop, or to copy the file itself, and so on.
Example: The simplest way to copy a file itself
Copy%0 D:\wind.bat
Tip: Add inline comments
% Comment content% (can be used as inline annotations, redirection symbols and pipe symbols cannot appear)
Why do you do that? At this time "annotation content" is actually regarded as a variable, its value is empty, so only the annotation function, but this usage is apt to appear grammatical error, generally not.

3, > redirection character

Output redirection command
The meaning of this character is to pass and overwrite, and his role is to pass the results of the run to the following range (either a file or the default system console)
In the NT Series command line, the scope of redirection is transformed from the entire command line to a single command statement, which is &,&&,| by the command delimiter | and statement block constraints.
Like what:
Using the command: Echo Hello >1.txt will create file 1.txt with the content "Hello" (note that there is a space at the end of the line)
Using commands: Echo hello>1.txt will create file 1.txt with the content "Hello" (note that there are no spaces at the end of the line)

4, >> redirection character

Output redirection command
The role of this symbol is similar to that of >, but their difference is that >> is passed and appended at the end of the file, and > is overriding
Usage ditto
Also take 1. txt as an example
To use the command:

echo Hello > 1.txt
echo World >>1.txt

The 1.txt content is as follows:
Hello
World

5, <, >&, <& redirection character

These three commands are also pipeline commands, but they are generally not used, you just need to know the OK, of course, if you want to study carefully, you can check the data. (I have checked, the Internet also can not find the relevant information)
Enter the redirect command to read the command input from the file instead of reading it from the keyboard.

@echo off
echo 2005-05-01>temp.txt
date <temp.txt
del temp.txt

This allows you to modify the current date without waiting for input
>&, writes the output of one handle to the input of another handle.
<&, just as opposed to >&, reads input from one handle and writes it to another handle output.
Common handles: 0, 1, 2, undefined handle: 3-9
1>nul indicates that the correct information is not allowed to be exported
2>nul indicates that error messages are not allowed to be printed.
1 and 2 of them are the addresses that represent the input and output of a data stream (NT CMD is called a handle, Msdos called a device).
Handle 0: Standard input stdin, keyboard input
Handle 1: Standard output stdout, output to command Prompt window (console, code con)
Handle 2: Standard error stderr, output to command Prompt window (console, code con)
The stdin can be < redirected, stdout can be >, >> redirected.
We already know that reading the contents of the text can be used for the for command, but it is a bit cumbersome to read only the first line with the for command. The simple approach is as follows:

@echo off
set/p str=<%0
echo%str%
Pause

Run the first line that displays the batch file itself: @echo off

6, | Command pipe character

Format: First Command | The second command [| The Third Order ...]
Use the result of the first command as a parameter to the second command, and remember that this approach is common in Unix.
For example:
Dir c:\|find "txt"
The above command is: Find C:\ all, and find txt string.
Find's function Please use Find/? View yourself
This is how I automatically format a disk without automatically formatting parameters for format
Echo Y|format A:/s/q/v:system
As you can see in the format, you need to enter Y to confirm that the grid is Gepan, with Echo y in front of the command and the | character to pass the result of Echo y to the Format command
So as to achieve the purpose of automatically entering Y
(This order is dangerous, please be careful when testing)

7. ^ Escape characters

^ is the leading character of the special symbol <,>,&, in which he removes the special features of the above 3 symbols and uses them only as symbols instead of using their special meaning.
Like what

echo Test ^>1.txt

The results are: Test > 1.txt
He did not append in 1.txt, hehe. It just shows up.
In addition, this escape character can also be used as a continuation symbol.
For a simple example:

@echo off
echo Hero ^
is ^
good ^
man
Pause

Needless to say, I will try to understand.
Why is an escape character descriptors at the end of a line to act as a continuation? The reason is simple, because there is also an invisible symbol at the end of each line, that is, a carriage return, the escape character at the end of the line to let the carriage return invalid, thereby playing a continuation of the role.

8, & Combination command

Syntax: first Command & second command [& Third command ...]
&, &&, | | As a combination command, as the name suggests, you can combine multiple commands when a command is executed. This is allowed in batch scripts and is very extensive. Because the batch recognition line does not recognize the number of orders.
This symbol allows you to use more than 2 different commands on a single line, and when the first command fails, it does not affect the execution of the command behind it.
Here & the commands on both sides are executed sequentially, from the point of departure.
Like what:
Dir z:\ & dir y:\ & dir c:\
The above command will display the contents of the Z,Y,C disk continuously, regardless of whether the disk exists

9, && Combination command

Syntax: first command && second command [&& Third command ...]
In this way, you can execute multiple commands at the same time, and you will not execute the following command when you encounter a command that executes the error, and if you have never made a mistake, you are done with all the commands.
This command is similar to the one above, but the difference is that when the first command fails, the command behind it does not execute
Dir z:\ && dir y:\ && dir c:\

10, | | Group command

Syntax: first Command | | The second command [| | | The Third order ...]
In this way, you can execute multiple commands at the same time, when a command fails, the second command is executed, the following command is not executed when the correct command is encountered, and all commands are executed if the correct command is not present;

Tip: Combining commands with redirection commands must pay attention to priority
The pipeline command has precedence over the redirection command, and the redirection command has precedence over the combined command
Question: List the files and folders in the C and D disks to the A.txt file. See Example:
Dir c:\ && dir d:\ > A.txt
In this way after the A.txt only D disk information! Why? Because the precedence of a combination command has no precedence over the redirection command! So this sentence divides the line into two parts in execution: dir c:\ and dir d:\ > A.txt, not the two parts you want: dir c:\ && dir d:\ and > A.txt. To use the combination command && to meet the requirements of the topic, you must write this:
Dir c:\ > A.txt && dir d:\ >> a.txt
In this way, by priority, DOS divides the sentence into two parts: dir c:\ > A.txt and dir d:\ >> a.txt. The difference between the several sentences in example 18 is quite special, and it is worth studying and understanding.
Of course, you can also use the & command (think for yourself):
Dir c:\ > A.txt & dir d:\ >> a.txt
[This can also be accomplished with dir c:\;d: \ >>a.txt]

11, "" "String definition character

Double quotes allow spaces to be included in a string, and you can enter a special directory using the following methods
CD "Program Files"
CD progra~1
CD pro*
All of the above three methods can be entered into Program Files directory

12., comma

Commas are equivalent to spaces, and in some cases "," can be used as spaces to make
Like what
Dir,c:\

13,; Semicolon

Semicolons, when the command is the same, you can use different targets to isolate, but the execution effect is unchanged, such as an error occurred during execution, only return error reports, but the program will still execute. (Some people say that will not continue to do, in fact, test to know)
Like what:
Dir c:\;d: \;e:\;z:\
The above command is equivalent
Dir c:\
Dir d:\
Dir e:\
Dir f:\
If the z disk does not exist, run the display: The system cannot find the specified path. The execution of the command is then terminated.
Example: Dir c:\;d: \;e:\1.txt
The above command is equivalent
Dir c:\
Dir d:\
Dir e:\1.txt
The file e:\1.txt does not exist, but the E disk exists with an error prompt, but the command is still executed.

Why? Terminates execution if the target path does not exist, and continues execution if the path exists and only the file does not exist.
That's all! What's your opinion? Please go to the Bat Exchange area to post! The next section is improved!

14, () bracket

Parentheses have a special role in batch programming, where the left and right brackets must be used in pairs, and can include multiline commands, which will be considered as a whole, as a command line.
Parentheses are common in the for and if statements, nested using loops or conditional statements, but parentheses () can also be used separately, see examples.
Cases:
Command: Echo 1 & Echo 2 & Echo 3
Can be written as:
(
Echo 1
Echo 2
Echo 3
)
Both of these are considered to be a command line, as are the two written effects.
Note: This multiple command is treated as a command line, and if there are variables, the problem of variable latency is involved.

15,! Exclamation number

Nothing to say, in the variable delay problem, used to represent the variable, that is,%var% should be expressed as!var!, see the previous setlocal command introduction.

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.