For has 4 parameters/d/l/r/f Their function I'll explain it in the following example
FOR/L%%variable in (start,step,end) do command [Command-parameters]
The set represents a sequence of numbers in increments from start to finish.
Therefore, (1,1,5) will produce sequences 1 2 3 4 5, (5,-1,1) will produce sequences (5 4 3 2 1).
@echo off
FOR/L%%i in (1,1,5) do @echo%%i
Pause
FOR/D%%variable in (set) do command [Command-parameters]
The/d parameter can only display directory names under the current directory
This parameter is mainly used for directory search, does not search for files, see such examples
@echo off
FOR/D%%i in (c:\*) do echo%%i
Pause
For example, we're going to make a 1-3-letter name for the folder under the current path.
@echo off
FOR/D%%i in (???) do echo%%i
Pause
* Number and the function of the sign, the * number denotes any n characters, and the number is only one character
@echo off
for/d%%i in (window.) do echo%%i
Pause
FOR/R [[Drive:]path]%%variable in (set) do command [Command-parameters]
Check the directory tree with [Drive:]path root.
If no directory is specified after/R, the current directory is used. If the set is only a single point (.) character, the directory tree is enumerated.
@echo off
FOR/R c \%%i in (*.exe) do echo%%i
Pause
Save this bat to the D disk anywhere and then execute, will look at the C packing directory, and each directory subdirectory below all the EXE file list
@echo off
FOR/R%%i in (*.exe) do @echo%%i
Pause
This command does not add C: \ 's search path, will be the current directory as the search path, such as you bat you put him in the D:\test directory execution, then he will be the D:\test directory and his sub-directory of all EXE files listed
Lists the directories that exist for the boot. INI:
@echo off
FOR/R c \%%i in (boot. ini) do if exist%%i echo%%i
Pause
Parameters/F
Iterative and file parsing, using file parsing to process command output, strings, and file contents. Use iteration variables to define the content or string to examine, and to further modify the resolution using various options. Use the Options token option to specify which tokens should be passed as iteration variables
。 Note: When no token option is used,/F will only check for the first token.
The file parsing process involves reading the output, string, or file contents, dividing it into separate lines of text, and then parsing each line into 0 or more tokens. The For loop is then called by setting the value of the iteration variable to the token. By default,/F passes each file per
The first blank separator symbol for a line. Skips a blank line.
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" includes one or more
Specifies the keywords for the different parsing options. These keywords are:
Eol=c-refers to the end of a line comment character (just one)
Skip=n-refers to the number of rows that are ignored at the beginning of the file.
Delims=xxx-refers to the delimiter set. This replaces the space and the jump bar
The default delimiter set.
Tokens=x,y,m-n-Indicates which symbol per line is passed to each iteration
For itself. This causes the allocation of additional variable names. M-n
The format is a range. Specify MTH with the nth symbol. If
The last character in the symbol string, the asterisk number,
Then the extra variable will be parsed after the last symbol
Assigns and accepts the retained text of a row. Tested, this parameter is up to
Only 31 fields can be distinguished from each other.
USEBACKQ-Use the back quotation marks (the key on the left of the number 1 on the keyboard ').
when parameter usebackq is not used: File-set represents a file, but cannot contain spaces
double quotes denote a string, or "string"
Single quotation marks indicate execution command, ' command '
When using parameter usebackq: File-set and "File-set" both represent files
When there are spaces in the file path or name, you can enclose them in double quotation marks
Single quotes represent strings, or ' string '
The post-quote indicates command execution, which is ' command '
The above is a direct copy of the Help information obtained with the for/? command.
I'm going to give you an example to help you understand these parameters!
For command Example 1:****************************************
@echo off
REM first establishes temporary files Test.txt
echo; Comment Line, this is a temporary file, run out of delete >test.txt
Echo 11 segment 12 Segment 13 segment 14 Segment 15 segment 16 Segment >>test.txt
echo 21 Segment, 22 segment, 23 segment, 24 segment, 25 segment, 26 segment >>test.txt
Echo 31 Segment-32 Segment-33 Segment-34 Segment-35 Segment-36 segment >>test.txt
For/f "eol=; tokens=1,3* delims=,-"%%i in (test.txt) do echo%%i%%j%%k
Pause
Del Test.txt
Run Display results:
11 Segment 13 Segment 14 Segment 15 Segment 16 segment
21 Segment 23 Segment 24 segment, 25 segment, 26 segment
31 Segment 33 Segment 34 Segment-35 Segment-36 segment
Please press any key to continue ...
Why is this? I'll explain:
eol=; Behavior Comment Line at the beginning of a semicolon
TOKENS=1,3* assigns a variable to the 1th, 3rd, and remaining fields of each line%%i,%%j,%%k
Delims=,-(there is a space after the minus sign) with a comma minus sign and a space separator, the space must be placed in the last
For command Example 2:****************************************
@echo off
for/f "Eol= delims="%%i in (test.txt) do echo%%i
Pause
Run will show test.txt all content, including comment lines, does not explain ha.
For command Example 3:****************************************
The other/F parameter can also be seen in the output command results in this example
@echo off
for/f "delims="%%i in (' Net user ') do @echo%%i
Pause
This is the name of all the accounts in this machine. The contents of the extension are enclosed in two single quotation marks, which means that when the command executes, for will return each line of the command result, plus that "delims=" is to let me the line of space can be displayed, do not add only the left column to show the space!
The article originates from the network
Pipeline operation not supported in for statement
Set a=ping lntf43 |find/c/I "reply" also not
Ping lntf43 |find/c/I//"Reply from" > Reply.txt
for/f%%i in (reply.txt) do set reply=%%i
Echo%reply%
For command explanation