The most basic form:
in the cmd window : for %I in (Command1) do Command2
in a batch file: For%%iIn (Command1) do Command2
In batches, common parameters for the for command are/d,/R,/F,/L.
A,/d parameter--query directory
Format:
FOR/D%%i In (COMMAND1) do Command2
Usage:
Summary:/d is used to search for a directory (or folder).
Attention:
Search Folders only, do not search for files. Searches only the specified directory and does not search subdirectories.
Example one: Search all directories under the C drive:
@echo off
FOR/D%%i in (c:/*) do echo%%i
Pause
The result is that all the folder names under the C drive are displayed, but not subdirectories.
Example two: Searching for folders in the current directory
@echo off
FOR/D%%i in (*) do echo%%i
Pause
The result of the run is to display all folders in the directory where the batch is located.
Second,/R parameters--Traversal search
Format:
FOR/R%%i In (COMMAND1) do Command2
FOR/R [[Drive:]path]%%variable in (set) do command [Command-parameters]
Attention:
The path can be specified after/R [[Drive:]path].
Usage:
Summary: Traverse all addresses to search for files.
Includes searching all subdirectories. The range is larger than/D search. Traverse? Recursion? All, every corner.
Typically,/d searches only the current directory or the specified directory, not the subdirectory.
The/R search scope starts with the current directory or the specified directory and delves into all subdirectories. All-pervasive, great live to see people die to see the body digging land three feet also to find out the momentum of you.
Example three: Search all exe files under the current directory.
@echo off
FOR/R%%i in (*.exe) do echo%%i
Pause
The results will show the EXE files under the folders in the C and C drives. Anti-Virus is very useful oh.
Example four: Search for C-disk cool dog music:
@echo off
FOR/R c:/%%i in (KuGou.exe) do if exist%%i start%%i
Pause
Running results will turn on cool dog music, provided your cool dog music is mounted on the C drive. The purpose of using the if exist is to avoid constant prompt errors when the file does not exist.
Example five: Search all the directories in F disk
@echo off
FOR/R f:/%%i in (.) Do echo%%i
Pause
The results of the operation will show the F and all the folders under the F drive.
------------------------------------------------------' Below is not available for programming, can be ignored--------------------------------------------------
III./L parameter--sequence
Format:
FOR/R%%i In (COMMAND1) do Command2
FOR/L%%variable in (start,step,end) do command [Command-parameters]
Usage:
Summary: Establishes a sequence for the specified object.
The/l parameter is the range of iterated values for a sequence of numbers from start to finish in increments.
Use the iteration variable to set the starting value (start#), and then step through a range of values until the value exceeds the set termination value (end#).
/l will perform iteration variables by comparing start# to end#. If start# is less than end#, the command is executed. If the iteration variable exceeds end#, the command interpreter exits the loop. You can also use negative step# to progressively execute values in this range in a way that decrements the numeric value.
Use for to give 1 to 5 of these numbers in order.
Example six: Increment mode.
@echo off
FOR/L%%i in (1,1,5) do @echo%%i
Pause
The result of the run is the build sequence: 1 2 3 4 5.
Example VII: Descending mode.
@echo off
FOR/L%%i in (1,-1,5) do @echo%%i
Pause
The result of the run is the build sequence: 5 4 3 2 1.
Attention:
Set (Start,step,end), start represents the starting value of the sequence, and end represents the end value of the sequence, which together determines the range of values for the sequence.
Step controls how start and end are generated. When step is a positive increment sequence, when step is negative, it is a descending sequence. What if Step=0 is going to cycle?
Example VIII: The numerical relationship of the three start,step,end.
@echo off
FOR/L%%i in (2,2,13) do echo%%i
Pause>nul
Run Results Branch display 2, 4, 6, 8, 10, 12
Explanation: The numerical relationship of the Start,step,end is: Start > End when step is positive, Start+step determines the minimum range of%%i, and end determines the maximum range of values for the%%i. When step is negative, Start+step determines the maximum range of%%i, and end determines the minimum range of values for the%%i.
Example nine: Create three folders named Number 1, 2, 3.
@echo off
FOR/L%%i in (1,1,3) do MD%%i
Exit>nul
The results of the run will create three digitally named folders under the current directory.
Four,/F parameters--slicing strings
delims= symbol Set delimiter
Format:
for/f "delims= symbol set"%%i In (COMMAND1) do Command2
Usage:
Summary: Omit the delimiter and slice the string.
1) define the delimiter with the punctuation in the text. Separators are usually the space bar and the jumping bar.
2) punctuation that is defined as a delimiter is ignored (hidden), and if there is content after the delimiter, the default delimiter is ignored with the content unless it is displayed using the tokens definition. Therefore, the command can be used to mask punctuation and extract information from the first column.
3) the content before and after the defined delimiter becomes a separate subsection (or column), which can be edited for the for command, such as using tokens extraction.
Attention:
Symbol set, which refers to the punctuation mark used as a delimiter, can be one or more.
When Delims defines multiple separators, spaces between the symbols are not used, and the space used for the delimiter is placed at the end.
The first thing to figure out is the separator. A delimiter is a punctuation mark specified with Delims, which is the punctuation mark in the text. Note: The delimiter here is not all punctuation in the text, but the punctuation specified by Delims. Such as
Second, we must make clear the sub-section, the breakdown. We refer to the text content between two separators per line as a subsection, a column. Note: Is between delimiters, not between punctuation marks
Example of DOS command for fine Solutions