DOS for command details

Source: Internet
Author: User
Tags eol

The for command is basically used to process the text. In addition to the function of processing the text, we need to talk about 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 a single lowercase A-Z or an uppercase A-Z, they are case sensitive ~, For will give each read value to him!
In: Command Format. Just write it!
(Related file or command): For reads something and assigns it to the variable. If you do not understand it, refer to 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! I will also give it here for your reference.
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.

1. Parameter/d
For/d % variable in (SET) do command [command-parameters]
If the set contains wildcards, it is specified to match the directory name instead of the file name.
Name match.
If set (that is, the "related file or command" I wrote above) contains wildcards (* And ?), Execute the specified command for each directory that matches the set instead of the file group in the specified directory.
This parameter is mainly used for Directory Search and does not search for files. For example
@ Echo off
For/d % I in (C:/*) Do echo % I
Pause
Running will print out all the directory names under the root directory of the C drive, and 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.
Here we will explain the * and? * Indicates any n characters, while? It only represents any character
Now that you know what this means, let's give you a 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! Display: Windows
The/d parameter can only display the directory name under the current directory. Please note that!

2. Parameter/R
For/R [[drive:] path] % variable in (SET) do command [command-parameters]
Check the directory tree with [drive:] path as the root and point
For statement. If no directory is specified after/R, the current
Directory. If the set is only a single point (.) character, the directory tree is enumerated.

Recursion
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? He is much more powerful than/D!
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!
NOTE 2:
1. If the file name in set contains wildcards (? Or *), lists the directory specified by the/R parameter and all the files that match the set in the subdirectories below it, and the directories that do not match the file are not listed.
2. On the contrary, if set is a specific file name without wildcards, enumerate the directory tree (that is, list the Directory and all its subdirectories), regardless of whether the specified file in set exists. This is similar to the single point (.) Enumeration directory tree mentioned above. A single point represents the current directory and can be viewed as a file.
Example:
@ Echo off
For/r c:/% I in (*. EXE) Do echo % I
Pause
Let's save the bat to drive D and execute it at will. I will see that he put the C root directory, and all the EXE files under each subdirectory are listed !!!!
Example:
@ Echo off
For/R % I in (*. EXE) Do @ echo % I
Pause
The parameters are different! The C:/or the search path is not added before this command, so that he will use the current directory as the search path. For example, if you use bat, you can put it in the D:/test directory for execution, then, he will list the Directory D:/test and all the EXE files in its subdirectories !!!
Example:
@ Echo off
For/r c:/% I in (boot. INI) Do echo % I
Pause
In this example, all the directories on drive C are enumerated. To list only the directories in Boot. ini, you can change them to the following:
@ Echo off
For/r c:/% I in (boot. INI) do if exist % I echo % I
Pause
Using this command to search for files is really good ......
You should understand this parameter! It's still fun!

3. Parameter/L
For/L % variable in (START, step, end) do command [command-parameters]
This set indicates a sequence of numbers from start to end in incremental form.
Therefore, (, 5) will generate the sequence 1 2 3 4 5, (5,-) will generate
Sequence (5 4 3 2 1 ).
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
End # Compare to execute iteration variables. If start # is smaller than end #, the command is executed. If the iteration variable exceeds
End #, the command interprets the program to exit this loop. You can also use the negative step # To gradually execute the value in this range in a descending value. For example, (, 5) generates sequence 1 2
3 4 5, while (5,-) generates a sequence (5 4 3 2 1 ). Syntax:
It seems a bit dizzy! Let's take a look at the 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!
If it is dizzy, print a number and use p... to meet your needs. Let's take a 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 (65535,), I will first tell you how to open the CMD window .... so many of you are not dead!
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.
Iv. Parameter/F
/Iteration and File Parsing
Use File Parsing to process command output, strings, and file content. Use iteration variables to define the content or string to be checked, and use various options to further modify the resolution method. Use the options token option to specify which tokens should be passed as iteration variables. Note: When the token option is not used,/F checks only the first token.
The file parsing process includes reading output, string, or file content, dividing it into independent text lines, and parsing each line into zero or more tokens. Then, call the for loop by setting the variable value as the token. By default,/F passes the first blank separator number of each line of each file. Skip empty rows.

The Detailed Help format is:
For/f ["options"] % variable in (file-set) do command [command-parameters]
For/f ["options"] % variable in ("string") do command [command-parameters]
For/f ["options"] % variable in ('command') do command [command-parameters]
The quoted string "options" contains one or more
Specify keywords for different resolution options. These keywords are:
EOL = C-refers to the end of a line comment character (just one)
Skip = N-indicates the number of rows ignored at the beginning of the file.
Delims = xxx-refers to the delimiter set. This replaces the space and the hop key.
Default delimiter set.
Tokens = x, y, M-n-indicates which symbol of each row is passed to each iteration.
For itself. This leads to the allocation of additional variable names. M-n
Format is a range. Use the nth symbol to specify MTH. If
The asterisks of the last character in the symbol string,
Then the extra variable will be resolved after the last symbol
Distribute and accept reserved text of rows.
Usebackq-use quotation marks (the key on the left side of number 1 on the keyboard ').
If usebackq is not used, file-set indicates the file, but it cannot contain spaces.
Double quotation marks indicate a string, that is, "string"
A single quotation mark indicates the command to be executed, that is, 'command'
When usebackq is used, both file-set and "file-set" indicate files.
When the file path or name contains spaces, it can be enclosed in double quotation marks.
Single quotes indicate a string, that is, 'string'
The quotation marks indicate command execution, that is, 'command'

The above example uses /? The help information obtained by the command is copied directly.
Dizzy! Here is an example to help you understand these parameters!

For command Example 1: ****************************************
@ Echo off
Rem first sets up the authorization file test.txt
Echo; Comment line, this is a temporary file, used up to delete> test.txt
Echo 11 segment 12 segment 13 segment 14 segment 15 Segment 16> test.txt
Echo 21, 22, 23, 24, 25, 26> test.txt
Echo 31-32-33-34-35-36> test.txt
For/F "EOL =; tokens = 1, 3 * delims =,-" % I in (test.txt) Do echo % I % J % K
Pause
Del test.txt
Running result:
11, 13, 14, 15, 16
21, 23, 24, 25, and 26
31, 33, 34, 35, and 36
Press any key to continue...
Why? Let me explain:
EOL =; behavior comment line starting with a semicolon
Tokens = 1st * assign the 3rd, and remaining fields to the variables % I, % J, % K, respectively.
Delims =,-(there is a space after the minus sign) Use comma minus sign and space as the separator, the space must be placed at the end

For command Example 2: ****************************************
@ Echo off
For/F "EOL = delims =" % I in (test.txt) Do echo % I
Pause
All content of test.txt, including the comment line, will be displayed at runtime.

For command Example 3: ****************************************
In addition, the/F parameter can be viewed in the output command.
@ Echo off
For/F "delims =" % I in ('net user') Do @ echo % I
Pause
In this way, the names of all your local accounts will come out, and the content in the extension number will be enclosed in two single quotes to indicate that when the command is executed, for will return the results of each line of the command, the "delims =" is added to enable the entire line of space to be displayed. If it is not added, only the left column of space is displayed!

----------------------------

For
/F "delims =/tokens = 8" % I in ('dir/a-D/S/B "C:/documents and
Settings/Administrator/Local Settings/Temporary Internet Files/*. MP3 "')
Do @ echo % I

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.