Home>

Batch processing commonly used special symbols

[email protected] line echo mask

2.%batch variable leader

3,>redirection

4,>>redirection

5.<&&&&redirector

6, | command pipe character

7, ^ escape character

8,&combination command

9,&&combination command

10, || Combined commands

11, "" string delimiter

12, comma

13 .;semicolon

14, () brackets

15 ,! Exclamation mark

16. Other special tags you may see during batch processing:(omitted)

cr (0d) command line terminator

escape (1b) ansi escape character guide

space (20) commonly used parameter delimiters

tab (09);= uncommon parameter delimiter

+ copy command file connector

*?file wildcard

/Parameter switch leader

:Batch tag leader

Without further ado,Talk

[email protected] line echo mask

This character means in batch processing to turn off echoing of the current line.We know from the previous lessons

echo off can turn off the echo of the entire batch command,But you cannot turn off the echo off command.Now if we [email protected] the echo off command, we can meet the requirement that all commands do not echo

2.%batch variable leader

This percent sign is not strictly an order,It's just a parameter in the batch (except when multiple%s are used together,More details will be introduced later).

Use%var%for reference variables,%1 to%9 for external parameters of the calling program, etc.

%0%1%2%3%4%5%6%7%8%9%* Arguments passed to the batch from the command line

%0 the batch file itself,Include full path and extension

%1 first parameter

%9 ninth parameter

%* All arguments from the first argument

Parameter%0 has special functions,Can call the batch itself,In order to achieve the purpose of batch processing itself,You can also copy the file itself and so on.

Example:The easiest way to copy the file itself

copy%0 d:\ wind.bat

Tip:Add inline comments

%Comment content%(can be used as an inline comment,Redirection and pipe symbols cannot appear)

Why is this so?At this point, the "note content" is actually treated as a variable.Its value is empty,Therefore, it only serves as a comment.However, this usage is prone to syntax errors,Generally not used.

3,>redirection

Output redirection command

This character means pass and cover,His role is to pass the results of the operation to the later range (the file can beOr the default system console)

In the nt series command line,The scope of redirection has changed from the entire command line to a single command statement.Limited by the command separators&,&&, || and statement blocks.

such as:

Use the command:echo hello>1.txt to create a file 1.txt with the content "hello" (note a space at the end of the line)

Use the command:echo hello>1.txt to create a file 1.txt with the content "hello" (note that there are no spaces at the end of the line)

4,>>redirection

Output redirection command

This symbol works a bit like>But their difference is that>is passed and appended at the end of the file,And>is overlay

Same usage as above

Also take 1.txt as an example

Use the command:

echo hello>1.txt
echo world>>1.txt

At this time, the content of 1.txt is as follows:

hello

world

5.<&&&&redirector

These three commands are also pipeline commands,But they are not commonly used,All you need to know is OK, of course, if i want to study it carefully,You can check the information yourself.(I have checked.(No relevant information can be found online)

<, Enter the redirect command,Read command input from a file,Instead of reading from the keyboard.

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

This way you can modify the current date without waiting for input

>&, Write the output of one handle to the input of another handle.

<&,&&&;Instead, read input from one handle and write it to another handle output.

Common handles:0, 1, 2, undefined handles:3-9

1>nul means that outputting correct information is prohibited

2>nul indicates that output of error information is prohibited.

1 and 2 are the addresses representing the input and output of a data stream (nt cmd is called a handle,msdos calls it device).

Handle 0:stdin, keyboard input

Handle 1:standard output stdout, output to the command prompt window (console, code is con)

Handle 2:standard error stderr, output to the command prompt window (console, code is con)

Among them stdin can be redirected, and stdout can be redirected.

We already know that you can use the for command to read the contents of the text, but if you only need to read the first line, use the for command.The simple way is as follows:

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

Run the first line showing the batch file itself:@echo off

6, | command pipe character

Format:First command | Second command [| Third command...]

Use the result of the first command as a parameter to the second command,Remember this is very common in unix.

E.g:

dir c:\ | find "txt"

The above command is:find c:\ all, and find the txt string.

find function please use find /?to see for yourself

Without auto-formatting parameters for format,This is how I automatically format the A drive

echo yformat a:/s/q/v:system

Anyone who has used format knows,Enter y to check whether the grid is selected.Add echo y to this command and use 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 harmful.Please be careful when testing)

7, ^ escape character

^ Is a leading character for the special symbol<,>,&In the command he removed the special functions of the above 3 symbols,Just treat them as symbols without using their special meaning.

such as

echo test ^>1.txt

The result is:test>1.txt

He did not append it to 1.txt, huh, huh. Just show up

In addition, this escape character can also be used as a continuation line symbol.

To give a simple example:

@echo off
echo hero ^
Yes ^
Good ^
the man
pause

Needless to say,Try it for yourself and understand.

Why does the escape character at the end of the line work as a continuation character?the reason is simple,Because there is an invisible symbol at the end of each line,The carriage return,Escapes the carriage return when it is at the end of the line,Thus played a role in continuing.

8,&combination command

Syntax:first command&second command [&third command...]

&,&&, || are combined commands,As the name suggests,That is, multiple commands can be combined and executed as one command.This is allowed in batch scripts,And it is very widely used.Because batches do not recognize the number of orders.

This symbol allows more than 2 different commands to be used on one line,When the first command fails,It does not affect the execution of subsequent commands.

The commands here&on both sides are executed sequentially,Performed from the beginning.

such as:

dir z:\&dir y:\&dir c:\

The above command will continuously display the contents of the z, y, and c disks.Ignore whether the disk exists

9,&&combination command

Syntax:First command&&Second command [&&Third command...]

This method can execute multiple commands at the same time,When encountering an execution error command, the subsequent commands will not be executed.If there is no error, all commands are executed.

This command is similar to the one above,But the difference is,When the first command fails,The following commands will not be executed.

dir z:\&&dir y:\&&dir c:\

10, || Combined commands

Syntax:first command || second command [|| third command...]

This method can execute multiple commands at the same time,The second command is executed when one command fails,After encountering the execution of the correct command, the subsequent commands will not be executed.If the correct command does not appear, all commands have been executed;

Tip:You must pay attention to the priority when using the combination command and redirect command together.

Pipe commands take precedence over redirect commands,Redirection commands take precedence over combined commands

Question:List the files and folders of the C drive and D drive into the a.txt file. See example:

dir c:\&&dir d:\>a.txt

After this execution, only the information of d disk is in a.txt! why?Because the priority of the combined command is not higher than that of the redirect command! So this sentence divides this line into two parts when executing:dir c:\ and dir d:\>a.txt, not the two parts as i want:dir c:\&&dir d:\>a.txt. To use the combined command&&to meet the requirements of the question,Must be written like this:

dir c:\>a.txt&&dir d:\>>a.txt

In this way, depending on the priority level,dos will divide this sentence into the following two parts:dir c:\>a.txt and dir d:\>a.txt. The differences in the few sentences in Example 18 are special.It is worth studying and experiencing.

Of course, you can also use the&command (think of it yourself):

dir c:\>a.txt&dir d:\>a.txt

[This can also be achieved with dir c:\;d:\>>a.txt]

11, "" string delimiter

Double quotes allow spaces in strings,To enter a special directory, you can use the following methods

cd "program files"

cd progra ~ 1

cd pro *

The above three methods can enter the program files directory

12, comma

A comma is equivalent to a space,"," Can be used as a space in some cases

such as

dir, c:\

13 .;semicolon

Semicolon, when the commands are the same,You can use different goals;To isolate, but the execution effect remains the same,If an error occurs during execution,Returns only error reports,But the program will still execute.(Some say it won't continue.Actually test it out)

such as:

dir c:\;d:\;e:\;z:\

The above command is equivalent to

dir c:\

dir d:\

dir e:\

dir f:\

If the z-disk does not exist,Running display:The system cannot find the path specified.Then terminate the execution of the command.

Example:dir c:\;d:\;e:\ 1.txt

The above command is equivalent to

dir c:\

dir d:\

dir e:\ 1.txt

The file e:\ 1.txt does not exist, but the e disk exists. There is an error message.The command will still execute.

why?If the destination path does not exist,Then terminate execution;If the path exists,Only the file does not exist,Continue execution.

That's all! Please post any comments you have! If you have any questions, please post to the bat exchange area! Improve in the next section!

14, () brackets

Parentheses have a special role in batch programming,Left and right brackets must be used in pairs,Multi-line commands can be included in parentheses,These commands will be seen as a whole,Treated as a command line.

Parentheses are common in for and if statements,Used to nest loops or conditionals,In fact, parentheses () can also be used alone,See examples.

example:

Command:echo 1&echo 2&echo 3

Can be written as:

(

echo 1

echo 2

echo 3

)

The above two writing methods have the same effect.Both of these wordings are considered a command line.

Note:When such multiple commands are considered as one command line,If there are variables in it,It involves the problem of variable delay.

15 ,! Exclamation mark

Nothing to sayIn the variable delay problem,Used to represent variables,That is,%var%should be expressed as! Var !, please see the introduction of the setlocal command.

  • Previous C # Anonymous Method Foundation Review
  • Next Parsing pointers to objects in C ++