Special symbols for Batch Processing

Source: Internet
Author: User

1. @ command line echo Shield
2.% batch variable pilot
3.> Redirection
4.> Redirection
5. <,> &, <& redirection
6. | command pipeline operator
7. ^ escape characters
8. & combined commands
9. & combined commands
10. | combined command
11. "string delimiter
12. comma
13.; semicolon
14. Brackets
15 ,! Exclamation point
16. Other special tags that may be seen during batch processing: (omitted)
Cr (0d) command line terminator
Escape (1B) ANSI Escape Character Guide
Space (20) Common parameter delimiters
Tab (09); = uncommon parameter delimiters
+ Copy command file Connector
*? File wildcard
/Parameter switch Guide
: Batch tag pilot

Speak nonsense.
1. @ command line echo Shield
In batch processing, this character indicates that the echo of the current row is disabled. The lessons we used to know
Echo off can shut down the echo of the entire batch processing command, but it cannot turn off the echo off command. Now we add @ before the echo off command @, to achieve the requirement that all commands do not display back.
2.% batch variable pilot
This percent number is strictly not a command, and it is only a parameter in batch processing (except when multiple % are used together, it will be detailed later ).
Use % var % to reference the variable. Program External parameters include % 1 to % 9.
% 0% 1% 2% 3% 4% 5% 6% 7% 8% * parameters passed to the batch processing through the command line
% 0 batch processing file itself, including the complete path and extension
% 1 first Parameter
% 9 ninth Parameter
% * All parameters starting with the first parameter
The parameter % 0 has a special function. You can call the batch processing itself to achieve the batch processing itself, or copy the file itself.
Example: the simplest method for copying files
Copy % 0 D: \ wind. bat

3.> Redirection
Output redirection command
This character is passed and overwritten. It is used to pass the running result to the following range (the following can be a file or the default system console)
In the NT command lines, redirection ranges from the entire command line to a single command statement, which is restricted by the command separator &, &, | and statement block.
For example:
Run echo Hello> 1.txt to create the 1.txt file with the content "hello" (note that there is a space at the end of the line)
Run echo Hello> 1.txt to create the 1.txt file with the content "hello" (note that there is no space at the end of the line)

4.> Redirection
Output redirection command
The role of this symbol is similar to>, but the difference between them is> is passed and appended at the end of the file, while> is overwritten
Same as above
Example 1. txt
Run the following command:
Echo Hello> 1.txt
Echo world> 1.txt
In this case, the content of the 1.txt file is as follows:
Hello
World

5. <,> &, <& redirection
These three commands are also pipeline commands, but they are generally not commonly used. You just need to know it and it will be OK. Of course, if you want to study them carefully, you can check the information yourself. (I have already checked, and no relevant information can be found online)
<, Enter the redirection command, and read the command input from the file instead of the keyboard.
@ Echo off
Echo 2005-05-01> temp.txt
Date <temp.txt
Del temp.txt
In this way, you can directly modify the current date without waiting for the input.
> & Writes the output of one handle to the input of another handle.
<&, Exactly the same as> & on the contrary, read the input from one handle and write it into another handle output.
Common handle: 0, 1, 2, undefined handle: 3-9
1> NUL indicates that the output of correct information is forbidden.
2> NUL indicates that error messages cannot be output.
Both 1 and 2 represent the input and output addresses of a data stream (nt cmd is called a handle and msdos is called a device ).
Handle 0: standard input stdin, keyboard input
Handle 1: stdout is output to the Command Prompt window (console, Code Is con)
Handle 2: stderr standard error, output to the Command Prompt window (console, code is con)
Stdin can be <redirected, stdout can be>,> redirected, and stderr cannot be directly redirected in DOS, you can only use ctty or other commands to transmit control of the system to other devices.

6. | command pipeline operator
Format: The First Command | the second command [| the third command...]
Use the result of the first command as a parameter of the second command. Remember that this method is common in UNIX.
For example:
Dir c: \ | find "TXT"
The above command is: Find all c: \ and find the TXT string.
Use Find /? View by yourself
When you do not enable automatic formatting of the format parameter, I used this method to automatically format disk.
Echo y | format A:/S/Q/V: System
All those who have used the format know that, when re-partitioning the disk, enter y to confirm whether the disk is a disk. Add echo y before this command and use the | character to pass the echo y result to the format command.
To automatically input y
(This command is harmful. Be careful when testing)

7. ^ escape characters
^ Is the leading character of special symbols <,>, &. In the command, he removes the special functions of the above three symbols, only use them as symbols instead of their special meanings.
For example
Echo test ^> 1.txt
The result is: Test> 1.txt.
He didn't try to add it to 1.txt. Only shown
In addition, this escape character can also be used as a continuation symbol.
A simple example:
@ Echo off
Echo hero ^
Yes ^
Good ^
Man
Pause
You can give it a try.
8. & combined commands
Syntax: The First Command and the second command [& the third command...]
&, &, | Is a combination of commands. As the name suggests, multiple commands can be combined and executed as one command. This is allowed in batch processing scripts and is widely used. Because batch processing does not recognize the number of commands.
This symbol allows more than two different commands to be used in a line. If the First Command fails to be executed, the subsequent command execution is not affected.
Here & the commands on both sides are executed sequentially, and previously and subsequently.
For example:
Dir Z: \ & dir Y: \ & dir c :\
The preceding command displays the content of disk Z, disk y, and disk C consecutively, regardless of whether the disk exists.

9. & combined commands
Syntax: The first command & the second command [& the third command...]
This method can be used 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 the commands will be executed.
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.
Dir Z: \ & dir Y: \ & dir c :\
10. | combined command
Syntax: The First Command | the second command [| the third command...]
This method can be used to 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;

Tip: when using combined commands and redirection commands, you must pay attention to the priority.
Pipeline commands have a higher priority than redirection commands, and redirect commands have a higher priority than combined commands.
Problem: folder the C and D files to the.txt file. How will you solve this problem? Some friends said, isn't that easy? Execute two directories at the same time, and then convert the obtained result to a.txt. For example:
Dir c: \ & dir D: \> a.txt

Take a closer look at the results of this sentence to see if it meets the requirements of the question! Wrong! In this example, a.txt only contains information about disk D! Why? This is because & commands
And> commands cannot appear in a sentence at the same time (batch processing treats a row as a sentence )!! The combined command & has a higher priority than the pipeline command> (summarized by yourself)
Yes. Please correct the incorrect information )! Therefore, this statement divides the line into the following two parts: Dir c: \ and Dir D: \>
A.txt, rather than the two parts you want: Dir c: \ & dir D: \ and>
A.txt. To use the combined command & to meet the requirements of the question, you must write the following:
Dir c: \> a.txt & dir D: \> a.txt
In this way, based on the priority, DOS will divide this sentence into the following two parts: Dir c: \> a.txt and Dir D: \> a.txt. For example, the differences between the sentences in the 18 th sentence are special, so it is worth a good study.
Of course, you can also use the & command here (think the truth yourself ):
Dir c: \> a.txt & dir D: \> a.txt

11. "string delimiter
Double quotation marks can contain spaces in strings. To enter a special directory, use the following method:
Cd "Program Files"
CD progra ~ 1
CD Pro *
The preceding three methods can be used to access the directory of program files.

12. comma
A comma is equivalent to a space. In some cases, "," can be used as a space
For example
Dir, c :\
13.; semicolon
Semicolons (;). When commands are the same, different targets can be used for isolation, but the execution effect remains unchanged. If an error occurs during execution, only the error report is returned, but the program will still be executed. (Some people say they won't continue to execute it. In fact, they will know it after testing)
For example:
Dir c: \; D: \; E: \; Z :\
The preceding command is equivalent
Dir c :\
Dir D :\
Dir E :\
Dir F :\
If the Z disk does not exist, run the command: the system cannot find the specified path. Then terminate the command execution.
Example: Dir c: \; D: \; E: \ 1.txt
The preceding command is equivalent
Dir c :\
Dir D :\
Dir E: \ 1.txt
The file E: \ 1.txt does not exist, but the edisk exists. An error message is displayed, but the command will still be executed.

Why? If the target path does not exist, the execution is terminated. If the path exists and the file does not exist, the execution continues.
That's all! Please reply to your comments! If you have any questions, please post them in the bat Communication zone! Next improvement!

14. Brackets
Parentheses play a special role in batch 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 single 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. See the example.
Example:
Command: Echo 1 & Echo 2 & Echo 3
Can be written:
(
Echo 1
Echo 2
Echo 3
)
The above two writing methods have the same effect. Both of them are considered as a command line.
Note: such multiple commands are considered as a command line. If there is a variable in it, the variable delay problem is involved.
15 ,! Exclamation point
Nothing to mention. In the variable delay problem, it is used to represent the variable, that is, % var % should be expressed! VaR !, See the preceding 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.