Batch process for command usage guide _dos/bat

Source: Internet
Author: User

For this command is basically used to process text, but there are some other useful features!

Look at his basic format (here I refer to the format in the batch, which requires only one% at the command line)

For parameter% variable name in (related file or command) do execution command
Parameter: For has 4 parameters/d/l/r/f Their function I'll explain it in the following example
% percent Variable name: This variable name can be lowercase a-Z or uppercase-A-Z, they are case-sensitive, for the value of each read to him;

In: The format of the order, according to the written;
(related file or command): for what to read and then assign to the variable, see the following example

Do: The format of the order, according to write it!
Command executed: What to do with the value of each variable is written here.
You can enter the for/? In CMD to see the help of the system!

For%%variable in (set) do command [Command-parameters]

%%variable specifies a single letter replaceable parameter.
(set) to specify one or a set of files. You can use wildcard characters.
command specifies the commands that are executed for each file.
command-parameters specifies a parameter or command-line switch for a particular command. Now start talking about the meaning of each parameter

/d is directory only

If set (that is, "related files or commands" written above) contains wildcard characters (* and?), each item that matches the set
Record (rather than the filegroup in the specified directory) executes the specified Command.
System Help format: FOR/D%%variable in (set) do command
He is mainly used for directory search, will not search files, see such examples

@echo off 
for/d%%i in (*) doing @echo%%i 
pause

Put him in the C-Packing directory execution, will be the C-disk directory of all the directory name printed out, and file name does not show!
In one, for example we're going to put the name of the folder under the current path with just 1-3 letters.

@echo off 
for/d%%i on (???) do @echo%%i 
pause

In this case, if you have a directory name in the current directory with only 1-3 letters, it will be displayed, no, not shown.

Thinking Topics:

@echo off 
for/d%%i in (Windows?) do @echo%%i 
pause

Save to C-disk execution, what will be shown? Look at it!

The/d parameter can only display the directory name under the current directory, this should be noted!

/R recursion

Go to the root tree [Drive:]path, execute the FOR statement in each directory of the tree. If no directory is specified after/R, the current directory is considered. If the Set is just a period (.), only the directory tree is enumerated.

System Help format: FOR/R [[Drive:]path]%%variable in (set) do command
As we know above,/d can only display the directory name under the current path, so now this/R is also related to the directory, what could he do? He is much stronger than D/a!
He can read the file name of the current or your designated path, note that the file name, what to look at the example!

@echo off 
for/r C:%%i in (*.exe) do @echo%%i 
pause

We save this bat to D and then I'll see, he put the C-packing directory, and the subdirectory of each directory below all the EXE files are listed here, the C: is the directory.
One more.

@echo off 
for/r%%i in (*.exe) do @echo%%i 
pause

The parameters are different, this command is not preceded by the C: that is, the search path, so that he will be the current directory for the search path, such as you this bat you put him in the disaster d:est directory execution, then he will be the D:est directory and his subdirectory below the full EXE file list to!!!

/L iterated Algebra value range

Use an iteration variable to set the starting value (start#), and then step through a set of ranges of values until the value exceeds the set termination value (end#). /L The iteration variable is executed by comparing the start# with the end#. If the start# is less than end#, the command is executed.

If an iteration variable exceeds end#, the command interpreter exits the loop. You can also use negative step# to progressively execute values in this range in descending numeric values. For example, (1,1,5) generates a sequence of 1 2 3 4 5, whereas (5,-1,1) generates a sequence (5 4 3 2 1).
The syntax is:
System help format: for/l% Variable in (start#,step#,end#) do Command
For example:

@echo off 
for/l%%i in (1,1,5) do @echo%%i 
pause

Save execution See effect He will print from 1 2 3 4 5 such 5 numbers
(1,1,5) This parameter means that starting from 1 each time plus 1 until 5 terminates!
And look at this example.

@echo off 
for/l%%i into (1,1,5) do start cmd 
pause

After the implementation is not startled, how many 5 cmd window, hehe! If you change that (1,1,5) to (1,1,65535), what will happen,
Let me first tell you, will open 65,535 cmd window .... So much you don't crash count you strong!
Of course, we can also change that start CMD to MD%%i so that we will set up a specified directory!!! Name is 1-65535
After reading this parameter that I've been given a destructive nature to, let's look at the last argument.

/F

For detailed description containing/F
A for with/F is of great use in batch processing, with the following usage:
Format:

for/f ["Options"]%%i in (file) does command
for/f ["Options"]%%i in ("string") do command
for/f ["Options"]%%i In (command) do command

This is probably the most commonly used and strongest command, used primarily to process the output of files and some commands.
File represents one or more files
String representing strings
Command Representative commands
[Options] Optional

For the for/f%%i in (file) do command

File is the file name, according to the official view, the for will open the files in file in turn and read each file into memory before going to the next file, dividing each line into one element, ignoring the blank line, for example.
If the file a.txt has the following contents:

1th line 1th column 1th line 2nd column 1th line 3rd
2nd line 1th column 2nd line 2nd column 2nd line 3rd
3rd Line 1th column 3rd line 2nd column 3rd line 3rd

What commands would you use to display the contents of a a.txt? Of course it's Type,type a.txt.

For you can also complete the same command:

for/f%%i in (a.txt) do echo%%i

Or is it first performed from parentheses, because it contains the parameter/F, so the for will open the A.txt first, then read out all the contents of the a.txt, take it as a collection, and take each row as an element, so it produces such a collection,

{1th row 1th column 1th line 2nd column 1th row 3rd column,//First element
"2nd line 1th Column 2nd line 2nd 2nd row 3rd Column",//second element
"3rd line 1th Column 3rd line 2nd 3rd row 3rd Column"}//Third element

There are only 3 elements in the collection, with%%i in place of each element, followed by the command following do.

Specific process:

Use%%i instead of the 1th line 1th column 1th 2nd 1th line 3rd column, execute the Echo%%i behind do, and display "1th line 1th 1th row 2nd row 1th 3rd columns",
Use%%i instead of the 2nd Line 1th column 2nd 2nd 2nd line 3rd column, execute echo%%i, display "2nd line 1th column 2nd line 2nd column 2nd line 3rd column",

sequentially, until each element is replaced.

To enhance the understanding of/F, execute a two command, and you can see by contrast:
for/f%%i in (a.txt) do echo%%i//This will display the contents of the A.txt, because the role of/F will read the contents of the a.txt.
For%%i in (a.txt) do echo%%i//And this will only show the name A.txt, and will not read the contents.

With the above learning, we find that for/f will default to each line as an element, but what if we want to decompose each row into smaller content? Don't worry, the for command also provides us with more detailed parameters, making it possible for each row to be divided into smaller elements.
They are: delims and tokens.

Delims is used to tell for each row what should be taken as a separator, and the default delimiter is the Space and tab keys
For example, or the above file, we execute the following command:

for/f "delims="%%i in (a.txt) do echo%%i

The results displayed are:

1th row, column 1th
2nd row, column 1th
3rd row, column 1th

Why is that so? Because there is a delims this parameter, = after a space, meaning that each element is separated by a space, the default is to take only the first element after the split.

The execution process is:

Divides the first element "1th row 1th column 1th line 2nd column 1th row 3rd column" into three elements: "1th row 1th column" 1th line 2nd Column "" 1th row 3rd column ", it takes only the first one, that is," 1th row 1th column ", then executes the command after do, and so on.

But there's a limit to this, so what if we want the second column of each row?

At this time, tokens jumped out and said, I can do it.

Its role is when you divide each line into smaller elements by delims, it controls which or which ones to take.

Or the example above, execute the following command:

for/f "tokens=2 delims="%%i in (a.txt) do echo%%i

Execution results:
1th row, column 2nd
2nd row, Column 2nd
3rd row, column 2nd

If you want to display the third column, replace it with tokens=3.
At the same time, tokens supports wildcard *, as well as limited scope.
If you want to display the second and third columns, change to tokens=2,3 or tokens=2-3, if there are more: tokens=2-10 and so on.

The command at this time is:

for/f "tokens=2,3 delims="%%i in (a.txt) do echo%%i%%j

How do you get one more%%j?

This is because your tokens after you take two columns of each row, replace the second column with%%i, and replace the third column with%%j.
and must be in alphabetical order,%%J can not be replaced by%%k, because I followed by j

The results of the execution are:
1th line 2nd Column 1th line 3rd column
2nd line 2nd Column 2nd line 3rd column
3rd line 2nd Column 3rd line 3rd column

For wildcard *, the whole line or the remainder of the line is treated as an element.

Like what:

for/f "tokens=* delims="%%i in (a.txt) do echo%%i

The results of the execution are:
1th line 1th column 1th line 2nd column 1th line 3rd
2nd line 1th column 2nd line 2nd column 2nd line 3rd
3rd Line 1th column 3rd line 2nd column 3rd line 3rd
In fact, the result is the same as for/f%%i in (a.txt) do echo%%i.

Another example:

for/f "tokens=2,* delims="%%i in (a.txt) do echo%%i%%j

The results of the execution are:
1th line 2nd Column 1th line 3rd column
2nd line 2nd Column 2nd line 3rd column
3rd line 2nd Column 3rd line 3rd column
Replace the second column with%%i, replacing all the remaining with%%j

Finally there is the skip EOL, the two simple, skip is to ignore the first few lines of the file, and EOL used to specify when a line begins with what symbol, ignore it.
Like what:

for/f "skip=2 tokens=*"%%i in (a.txt)

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.