Windows Batch Processing Command learning 3

Source: Internet
Author: User
Tags eol

This article is transferred from: Batch Processing for command details ()

For command

 

This is a very useful command. The for command is actually a loop command. If we want to repeat a statement, we can use the for command to control the number of loops.

For parameter % variable name in (related file or command) Do Command executed

Parameters: For has four parameters/D/L/R/f. Their functions are explained in the following example:
% Variable name: this variable name can be lowercase A-Z or uppercase A-Z, they are case sensitive, for will give each read value to him;
In: Command Format. Just write it as needed;
(Related files or commands): For reads and assigns values to variables. See the following example.
Do: Command Format. Just write it!
The command to be executed: This is where you want to perform any operation on the value of each variable.

You can enter for/? In cmd /? See the help provided by the system! Comparison
For % variable in (SET) do command [command-parameters]

% Variable specifies a single letter replaceable parameter.
(SET) specifies one or more files. Wildcard characters can be used.
Command specifies the Command executed on each file.
Command-Parameters
Specify parameters or command line switches for specific commands. It is worth noting that there is a space between in and (in (SET.


Now let's start with the meaning of each parameter.

/D
Only directory
If set (that is, the "related file or command" I wrote above) contains wildcards (* And ?), For each object that matches the set

Directory (instead of the file group in the specified directory) to execute the specified command.

System Help format: For/d % variable in (SET) do command
It is mainly used for Directory Search and does not search for files. Let's look at this example.

@ Echo off
For/d % I in (*) Do @ echo % I
Pause

Save it in the root directory of drive C and print out the names of all directories under drive C. The file name is not displayed!
Next, for example, we want to name the folder in the current path with only 1-3 letters

@ Echo off
For/d % I in (???) Do @ echo % I
Pause

In this case, if your current directory has only 1-3 letters in name, it will be displayed and will not be displayed if it does not exist.

 

In a small example, all the subdirectories in the current directory are scanned by batch processing.

Echo off

For/d % I in (E: \ *) Do echo % I

Note: This command only scans the subdirectory name of the current directory and does not scan the subdirectory name contained in the subdirectory.


Question:

@ Echo off
For/d % I in (window ?) Do @ echo % I
Pause

Save it to drive C for execution. What will it display? Let's take a look!
The/d parameter can only display the directory name under the current directory. Please note that!

 

/R
Recursion
Go to the root directory tree [drive:] path and execute the for statement in each directory of the tree. If no directory is specified after/R

Current directory. If set is only 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. Now this/R is also related to the directory. What can he do? Rest assured

/D is much more powerful!
You can read all the names of the files in the current or specified path. Note that the file name is used as an example!

@ Echo off
For/R c: \ % I in (*. EXE) Do @ echo % I
Pause

How can we save the bat to drive D and execute it? I will see that it stores all the C root directory and all the subdirectories under each directory

And C: \ is the directory.

Another
@ Echo off
For/R % I in (*. EXE) Do @ echo % I
Pause

The parameters are different. The C: \ or search path is not added before this command, so that the current directory is used as the search path. For example

Bat, you run it under the D: \ test directory for disaster prevention, then it will list the D: \ test directory and all the EXE files in its subdirectories.

Come on !!!

 

In one example, batch processing scans all sub-directories in the current directory and the sub-directory names of all sub-directories.CodeThe exception is as follows:

For/R c: \ % I in (.) Do echo % I

 

Note: This code will scan all the subdirectories in the current directory and all the subdirectories in the subdirectories.


/L
Iteration value range
Use the iteration variable to set the start value (start #), and then gradually execute a set of range values until the value exceeds the set end value (END #)

. /L execute iteration variables by comparing start # And end. If start # is smaller than end #, the command is executed.

If the iteration variable exceeds end #, the command will explainProgramExit this loop. You can also use the negative step # To gradually execute it in a descending value.

Value in this range. For example, (, 5) generates a sequence 1 2 3 4 5, while (5,-) generates a sequence (5 4 3 2 1 ). Syntax:

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 the execution and check the effect. It will print 5 numbers from 1 2 3 4 5.
(, 5) This parameter indicates that 1 is added each time from 1 until 5 ends!

Let's look at this example.
@ Echo off
For/L % I in (1, 1, 5) do start cmd
Pause

After the execution, I was shocked. Why did I have five additional cmd windows! If you change (65535, 5) to (,), what will happen,

I will first tell you that we will open 65535 cmd windows... so much you will not be able to crash!

Of course, we can also change that startcmd to MD % I so that a specified directory will be created !!! The name is 1-65535.

After reading the parameter with the destructed nature granted by me, let's look at the last parameter.

 

/F

For details with/F

The for with/F is of great use. It is most used in batch processing. The usage is as follows:
Format:
For/f ["options"] % I in (File) do command

For/f ["options"] % I in ("string") do command

For/f ["options"] % I in ('command') do command

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

File indicates one or more files.

String represents a string

Command represents the command

["Options"] Optional

For/F % Iin (File) do command

File is the file name. According to the official statement, for will open the files in the file in sequence, and read each file to the memory before proceeding to the next file, and divide each row into one element, ignore blank rows. Let's look at an example.

Assume that the.txt file contains the following content:

1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns

What Command do you want to use to display a.txt content? Of course, it is type, type a.txt

For can also complete the same command:

For/F % I in (a.txt) Do echo % I

We should execute the statement in parentheses first. Because the statement contains the parameter/F, we will open a.txt first. Then we will read all the content in a.txt, use it as a set, and use each row as an element, so we will generate such a set,

{"1st rows, 1st columns, 1st rows, 2nd columns, 1st columns, 3rd columns", // The first element

"2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd columns", // second element

"3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd columns"} // third element

There are only three elements in the Set, and % I is also used to replace each element in sequence, and then the command after do is executed.

Specific process:

Replace "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, and 3rd columns" with % I, show "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd columns ",

Replace "2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd columns" with % I, show "2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd columns ",

Until each element is replaced.

To enhance understanding of the role of/F, execute the following two commands for comparison:

For/F % I in (a.txt) Do echo % I // This will display the content in a.txt, because/F will read a.txt
.

For % I in (a.txt) Do echo % I // This only displays the.txt name and does not read the content.

Through the above learning, we found that for/F will use each row as an element by default, but what if we want to break down each row into smaller content? Don't worry, the for command also provides us with more detailed parameters so that we can divide each row into smaller elements.

These are delims and tokens.

Delims is used to tell each line for what should be used as a separator. The default Delimiter is space and Tab key.

For example, if the above file is used, we can execute the following command:

For/F "delims =" % I in (a.txt) Do echo % I

The result is as follows:

1st rows and 1st Columns
2nd rows and 1st Columns
3rd rows and 1st Columns

Why. The delims parameter = is followed by a space, which means that each element is separated by a space. By default, only the first element after the separation is obtained.

The execution process is:

The first element "1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, and 3rd columns" is divided into three elements: "1st rows and 1st columns" "1st rows and 2nd columns" "1st rows and 3rd columns", it takes only the first one by default, that is, "1st rows and 1st columns ", then execute the command after do, and so on.

But there are limitations. What if we want the second column of each row?

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

It is used to control which one or more elements to take when each row is divided into smaller elements through delims.

In the preceding example, run the following command:

For/F "tokens = 2 delims =" % I in(a.txt) Do echo % I

Execution result:

1st rows and 2nd Columns
2nd rows and 2nd Columns
3rd rows and 2nd Columns

To display the third column, replace it with tokens = 3.

At the same time, tokens supports wildcard * and a limited range.

If you want to display the second and third columns, replace them with tokens = 2, 3 or tokens = 2-3. If there are more columns, such as tokens = 2-10.

The command is as follows:

For/F "tokens = 2, 3 delims =" % Iin (a.txt) Do echo % I % J

How to add one more % J?

This is because your tokens needs to take the two columns of each row, replace the second column with % I, and replace the third column with % J.

And it must be in alphabetical order. % J cannot be changed to % K, Because I is followed by J

The execution result is:

1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 2nd columns, 3rd rows, 3rd Columns

If the wildcard * is used, the entire line or the rest of the line is treated as an element.

For example:

For/F "tokens = * delims =" % I in(a.txt) Do echo % I

The execution result is:

1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns

In fact, it is the same as the execution result of do echo % I for/F % Iin (a.txt.

Another example is:

For/F "tokens = 2, * delims =" % Iin (a.txt) Do echo % I % J

The execution result is:

1st rows, 2nd columns, 1st rows, 3rd Columns
2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 2nd columns, 3rd rows, 3rd Columns

Replace the second column with % I, and replace the remaining columns with % J.

Finally, there is a combination of skip and EOL. The two are simple. Skip is to ignore the first number of lines of the file, and EOL is used to specify the symbols starting with a line, and ignore it.

For example:

For/F "Skip = 2 tokens = *" % I in(a.txt) Do echo % I

Result:

3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns

Skip is used to tell for to skip the first two rows.

If tokens = * is not added, the execution result is:

3rd rows and 1st Columns

I don't know what's going on.

For example, when the.txt content is changed:

. 1st rows, 1st columns, 1st rows, 2nd columns, 1st rows, 3rd Columns
. 2nd rows, 1st columns, 2nd rows, 2nd columns, 2nd rows, 3rd Columns
3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns

Run for/F "EOL =. tokens = *" % I in (a.txt) Do echo % I and the result is:

3rd rows, 1st columns, 3rd rows, 2nd columns, 3rd rows, 3rd Columns

EOL is used to tell for to ignore rows starting.

You must also add tokens = *. Otherwise, only "3rd rows and 1st columns" are displayed"

 

Author: clever101 posted on 23:07:33 Original article link Read: 162 comments: 0 views comments

Related Article

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.