[Batch] for command

for command

FOR this command it has largely been used to process text, but there are some other useful features!

Look at his basic format (here I quote a batch format, direct need only number one percent in the command line) 
the FOR command parameter variable name %% IN (relevant documents or command) DO executed

Parameters: FOR There are four parameters / d / l / r / f   their role is explained below with examples I 
%% variable name : variable name can be uppercase or lowercase az AZ, they are case sensitive , FOR would each the read value to him; 
the iN: format of the command, as it wants to write; 
(relevant file or command): fOR What should read and assigned to a variable, See the examples below 
do: format of the command, according to the write He wants! 
command execution: the value of each variable on what to do at this writing.

You can enter in the CMD for /? To see the help provided by the system! Controls about 
FOR %% variable IN (set) DO command [command-parameters]

%% variable parameter specifies a single letter replaceable. 
(set) to specify one or a group of files. You can use wildcards. 
command specified command for each file. 
command-parameters 
             specify parameters for a particular command or command-line switches.


Now start talking about the meaning of each parameter

/d 

Directory only 
if Set (namely "relevant documents or command," I have written) that contains wildcard characters (* and?), Each project will have to match with the Set

Record (instead of specifying a file group directory) Executes the specified Command.

Help formats: FOR / D %% variable IN (  set) DO command
he mainly used for directory search, not search for files, see such examples

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

 

He saved the root directory on the C implementation will be put under the C drive directory entire directory name printed, and the file name is not a show
In to a, for example, we want the name of the current path of the folder only 1 -3 letter to break out

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

 

So if you have a directory under the name of the current directory only 1-3 letters, it will be displayed, showing no no


Thinking Title:

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

Save the C drive to perform, what would it show? See for yourself! 
/ D parameter can only display the directory name in the current directory , that we should pay attention!

/R 

Recursive 
into the root of the tree [Drive:] Path, executing the for statement in each directory tree. If you do not specify a directory after / R, it is considered to be

Current directory. If Set is just a period (.), Only enumerate the directory tree. 
Help formats: the FOR / R & lt [[Drive:] path] %% the IN variable (SET) Command the DO

Above we know, / D can only display a directory name in the current path, so now the / R directory and also related to his able Mani? At ease than he

/ D and more powerful! 
He can put your current or file name under the specified path all the reading, note the file name , what's the use to see examples!

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

 

Ye are saved to the BAT where the D drive and then do whatever, I'll'll see, he put all subdirectories below the root directory C, and each directory

EXE files are listed, where c: \ is the directory.

another one 

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

 

Parameter is not the same, the front command did not add the C: \ is the search path, and he will at the current directory as the search path , such as this you

You put him a BAT disaster d: \ directory to the next test, he would be the D: \ test directory of all EXE files and subdirectories listed below him

Come!!!


/L 

Iterative numerical range 
using the iteration variable starting value provided (Start #), and then step through a set of ranges of value, until the stop value exceeds the set value (End #)

. / L will execute the iteration variable by Start # and End # compare. If Start # is less than End #, it will execute the command.

If the iteration variable exceeds End #, the command interpreter to exit the loop. You can also use a negative Step # to decreasing numerical-by-step execution

Row value within this range. For example, (1,1,5) generates a sequence of 12 345, and (5, -1) then generates a sequence (54,321). The syntax is:

Help formats: for / L %% Variable in (Start #, Step #, End #) do Command

E.g:

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

Saving the see results, he will print from 12,345 this five numbers 
(1,1,5) This parameter is expressed from the beginning of each 1 plus 1 until 5 terminated!

Look at this example 

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

 

After the execution was not shocked, how much 5 CMD window, Oh! If that (1,1,5) into (1,1,65535) what happens,

Let me tell you, will open 65,535 CMD window .... so much you do not crash count you strong!

Of course, we can also change the startcmd md %% i This will establish a specified directory named 1-65535 !!!

After reading this parameter was I given the nature of the damage, we look at the last parameter

/f

Containing / F for a detailed description of

Containing / F ratio is very useful for use in the batch at most, are used as follows:
Format: 
the FOR / F [ "Options"] %% I the IN (File) DOcommand

FOR/F ["options"] %%i IN ("string") DO command

FOR/F ["options"] %%i IN ('command') DO command

This is probably the most common, is the strongest command is mainly used to process the file and output the results of some commands.

file on behalf of one or more files

string represents a string

command represents the command

[ "Options"] Optional

对于FOR /F %%i IN (file) DO command

file name for the file, according to the official argument is that, for the file will be followed by the file is opened, and before proceeding to the next file is read into memory each file, in accordance with each line into the elements one by one, ignoring blank OK, look at an example.

If the file a.txt the following contents:

Row 1, column 1, line 1, column 2, line 1, column 3
row 2, column 1, line 2, column 2, line 2, column 3
, line 3, column 1, line 3, column 2 3 line column 3

You want to display the contents of a.txt, what command it? Course type, type a.txt

The same can be done for the command:

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

Or start execution brackets, because it contains the parameters / f, so that for a.txt will first open, then Read all a.txt inside, it as a set, and in each row as an element, so that there occurs collection

{ "Row 1, column 1, line 1, column 2, column 3, line 1", the first element //

"Row 2, column 1, line 2, column 2, column 3, line 2", the second element //

"Row 3, column 1, line 3, column 2, line 3, column 3"} // third element

Set only three elements, each element similarly replaced with %% i sequentially, and then performs the latter do command.

Specific process:

"Row 1, column 1, line 1, column 2, line 1 of 3" as used %% i instead of performing the later do echo %% i, display "row 1, column 1, line 2, column 1 1 line 3 "

"Row 2, column 1, line 2, column 2, line 2 of 3" as used %% i instead of performing echo %% i, display "row 2, column 1, line 2, column 2, line 2 of 3 '

In turn, until each element instead of the last.

In order to strengthen the role of understanding / f, do what two commands you can understand comparison:

for / f %% i in (a.txt ) do echo %% i // a.txt this displays the contents inside, because the role / f, it will read in a.txt
content.

for %% i in (a.txt) do echo %% i // a.txt and this will only show the name, and will not read the contents.

Through the above study, we found that for / f defaults to each line as an element, but if we want to break down each row and then a smaller content, how to do it? Do not worry, for command also provides a more detailed argument for us, that we should give each row is divided into smaller elements possible.

They are: delims and tokens

delims used to tell for each line should be what means as a separator, the default separator is a space and tab key

For example, or the above documents, we execute the following command:

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

The results show that:

Row 1, column 1
row 1, column 2
, line 3, column 1

Why is this happen. With this parameter because there delims, = followed by a space, meaning that each element then separated by spaces, the default is the first element to take only after segmentation.

Implementation process are:

The first element "row 1, column 1, line 1, column 2, column 3, line 1" is divided into three elements: "row 1, column 1" "row 1, column 2" "Line 1 column 3 ", which takes only the first default, i.e.," row 1, column 1 ", and then execute the command later do, and so on.

But this is still a limited, if we want a second column elements of each row, so what?

At this time, tokens out of it, I can do it.

Its role is when you each row is divided into smaller elements by delims, to take control of it to which one or a few.

Or the above example, the following command:

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

Results of the:

Row 1, column 2
, line 2, column 2
, line 3, column 2

If you want to display the third column, it would be replaced by tokens = 3.

Meanwhile support wildcard * tokens, and to limit the scope.

To display the second and third columns, is replaced by tokens = 2,3 or tokens = 2-3, if there was more: tokens = 2-10 and the like.

At this time the command is:

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

How more than a %% j?

This is because the back of your tokens to take two each row, second column replaced with %% i, the third column is replaced with %% j.

And must be arranged alphabetically, %% j can be replaced %% k, j is because behind i

Implementation of the results:

Row 1, column 2, column 3, line 1
, line 2, column 2, column 3, line 2
, line 3, column 2, line 3, column 3

For wildcard *, it is to put all of the remainder of the line or this line as an element of.

such as:

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

Implementation of the results:

Row 1, column 1, line 1, column 2, line 1, column 3
row 2, column 1, line 2, column 2, line 2, column 3
, line 3, column 1, line 3, column 2 3 line column 3

In fact, just the results for / f %% i in (a.txt) do echo %% i is the same.

Another example:

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

Implementation of the results:

Row 1, column 2, column 3, line 1
, line 2, column 2, column 3, line 2
, line 3, column 2, line 3, column 3

Instead of the second row with %% i, instead of using all of the remaining %% j

Finally, there skip co-eol, maybe a simple, skip ahead is the number of files to ignore, and eol to specify when a line begins with what symbol, just ignore it.

such as:

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

The results are:

Row 3, column 1, line 3, column 2, line 3, column 3

Skip to tell with the first two lines for skipped.

If you do not add tokens = *, then the results are:

Row 3, column 1

I do not know how it is.

Again, when a.txt content becomes:

Row 1, column 1, line 1, column 2 row 1, column 3
. Row 2, column 1, line 2, column 2, line 2, column 3
, line 3, column 1, line 3, column 2 line 3 of 3

Performed for / f ". Eol = tokens = *" %% i in (a.txt) do echo %% i result:

Row 3, column 1, line 3, column 2, line 3, column 3

Eol to tell for use to ignore. "" Beginning of the line.

The same must also add tokens = *, otherwise it will display "row 3 1"

 

Original: https://www.cnblogs.com/micro-chen/p/5694423.html

Guess you like

Origin www.cnblogs.com/xiangtingshen/p/11374234.html