Batch Processing for command details

Source: Internet
Author: User
Tags eol

The FOR command is basically used to process text, but there are other useful functions!
Look at its basic format (here I reference the format in the batch processing, and only one % number is needed directly in the command line)
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. 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 way, if your current directory has only 1-3 letters in name, it will be displayed, and it will not be displayed if it does not exist.
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
.
Another
@ Echo off
For/r % I in (*. exe) do @ echo % I
Pause
The parameters are different. The C: that is, the search path is not added before this command, so that the current directory will be used as the search path.
BAT. If you execute it under the d: est directory, then it will list the D: est directory and all the EXE files in its subdirectories.
Come on !!!
/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 interpreter exits 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 the start cmd 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 % I IN (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 =" % I in (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 % I in (a.txt.
Another example is:
For/f "tokens = 2, * delims =" % I in (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)

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.