DOS batch processing command☞For Loop commands

Source: Internet
Author: User
Tags eol
A For command is a command that runs in the command line or batch for a series of objects to execute one or more commands cyclically in sequence. Combined with some programs in Windows Management, its processing capabilities are powerful, and its applications are flexible and convenient. However, the complexity of the help information is often daunting for beginners. Here, based on my learning and understanding, we will simplify its usage, and negligence and errors may be inevitable.
Basic Format
(The format used in the command line is written here. If it is in batch processing, add % to form % ):
For/parameter % variable in (SET) do command
(Note: Except for Chinese characters, the rest are written in accordance with its format requirements, and can be case sensitive)
Parameters: For is divided into four parameters: d l R f, and some parameters can be appended with other options.
Variable: (remember that if the for command is used in batch processing, the % before the variable must be changed to %) this variable name is composed of a single letter and case-sensitive. (In the original help, it is also feasible to use a single number as the variable name ), for example, % B and % B indicate different variables.
The for command assigns the value read from in (SET) to this variable in each loop for reference in the subsequent command.
Set: a set of series files, strings, or content generated by commands (of course, wildcards *?, Environment variables can also be referenced). The for command reads the content of a set multiple times in a certain order and regularity, assigns values to the variables, and executes the do command for the next round of loop, until the content in the set is read, and the brackets are in the required format (there must be spaces between the in and the brackets ).
Command: It can be any qualified DOS command or external program that can be called by DOS, and multiple commands can be enclosed in parentheses and executed in one loop.
Note: Some directories or file names may have spaces, so in many cases, the contents in the set and commands often need to be enclosed by English quotation marks (but sometimes the content in the quotation marks may be considered as strings) in some examples below, we will ignore the case where the file name or directory name contains spaces.
The following uses parameter classification as an example to explain its usage:

1. Parameter/d

For/d % variable in (SET) do command
The/d parameter specifies to only execute the for command for the directory rather than the file.
Example 1:
Input in the command line (not in batch, and will not be explained later)
For/d % A in (c: \ *. *) Do echo %
The running will display all directories under the root directory of the C drive in multiple times, without displaying the file name
It looks a little messy. If you display the command to the explicit shutdown, it will be clear:
For/d % A in (c: \ *. *) Do @ echo %

2. Parameter/R
/R parameters can be followed by drive letters and paths
For/R can contain the PATH % variable in (SET) do command
The path after/R refers to all directories in the entire directory tree under it (equivalent to the range in the doscommand tree). If it is only an English sentence ., it refers to the directory tree under the current path. If the path is omitted, it refers to the current directory, and the later in (SET) is equivalent to the file set that matches the previous directory.
Here, there are two conditions for wildcard characters in (SET ).
1) In (SET) does not contain wildcards
Specifies a single file or a list of specific files (multiple file names are separated by separators, such as spaces and commas)
Example 2
@ Echo
For/R. % I in (abc.txt) Do echo.> % I
Echo on
Note: There is only one path after for/R, and echo.> signature in each loop.
Example 3 (put into batch processing)
@ Echo off
Rem display D: list of all files in the drive named file1 and file2
For/r d: \ % H in (file1, file2) do if exist % H echo % H
Pause
2) In (SET) contains wildcards * or?
In this case, the DO command will process each object in the in (SET) Directory Series specified by/R, regardless of the directories without matching files.
Example 4:
@ Echo off
Rem deletes all *. Chk files in drive C.
For/R c: \ % H in (*. Chk) Do del/Q % H
Pause
Note: del/Q indicates deletion in quiet mode (no confirmation required)

3. Parameter/L

For/L % variable in (start value, value added each time, comparison value at end) do command
(L can also be used in lower case, mainly for the purpose of visually not confusing with number 1, but not using lower case)
(Start value, value added each time, comparison value at the end) is equivalent to an equi-difference digital sequence, starting from the number of the "start value" and increasing each time (can also be set to a negative number) it is "added each time" and compared with "compare value at End". If it exceeds the value, it exits the For Loop (or the do command after this round is not executed)
For example, (, 3) will generate a sequence (1 2 3); (, 9) will generate a sequence (1 3 5 7 9); (5) will generate a sequence (5 4 3 2 1); (, 18) will generate a sequence (1 7 10 13 16)
Example 5
@ Echo off
: Create aa1 ~ in disk D ~ Five aa5 folders
For/L % I in (1, 1, 5) Do md d: \ Aa % I
Pause

Note: At the beginning of a row, a single colon (:) is followed by a name, which is a label line and corresponds to the position indicated after go in batch processing. The double colon (:) is generally used for comments, annotations can be expressed by REM and space in batch processing. The two are slightly different. Rem comments are displayed on the screen when the command is not turned off for display :: it is not displayed under any circumstances.

Iv. Parameter/F
This parameter/F will open the files in the (SET), so that the for command can handle the editing operations such as reading text files, adding, deleting, and replacing text files, which is powerful, therefore, it is relatively complicated.
File Name-Set
For/F "option" % variable in ("string"-set) do command
'COMMAND '-Set
/F can be followed by several options, without options, of course, it is also a qualified format, and parameters must be enclosed in quotation marks as a whole. The following sets are mainly composed of three forms, finally, in each round of the For Loop, a row of strings will be read to assign values to the specified % variable and the additional variable assigned to the option. Then, run the command after do.
The following is an example to illustrate and gradually understand the usage of each sub-item.
Example 6
Assume that the content of D: \ abc.txt is as follows:
Name, gender, age, etc.-level
Michael Zhang 36 A-1
Li Si Men 29 B-2

Zhao liunv31 A-2

Run the following command:
For/F % C in (D: \ abc.txt) Do @ echo % C
The screen is displayed as follows:
Name
Zhang San
Li Si
Zhao Liu

Explanation: This is the default parameter option for/R before "% variable". In the loop, each wheel is separated by space by default and segmented to strings line by line in the open file, because no additional variable (that is, only one variable % C) is added, only the character in the first segment is assigned to % C, and then the do command is executed, and then the next round of the loop is carried out, empty rows are ignored by default.
Modify:
For/F "skip = 1 tokens = 1, 4, delims =" % C in (D: \ abc.txt) Do @ echo % C % d
Shown:
Zhang San A-1
Li Si B-2
Zhao Liu A-2

Solution:
Skip = 1 indicates that the number of lines that the text begins to ignore is 1-ignore several lines
Delims = what single symbol can be used in a row (there can be a combination of multiple characters, and spaces cannot be added between them. It is understood as multiple single characters. If a space character is required, it must be placed at the end) to separate strings as the unit for reading values (forming a segment). In this example, after a medium number, it is null to separate strings with spaces. -- What knife is used for splitting
Tokens = the number after the equal sign indicates to take the nth separated string segment in order to assign the % variable and the variable appended with the sequence respectively, in this example, the first section is assigned to % C, and the second section is assigned to C. The variable is assigned to % d, and can be written as tokens = 1st, 5-7 or tokens = 1, 2, 3 * or tokens = 1, 2, 5, 7 respectively indicate taking 1, 2, 5, 6, 7 (in turn assigned to % C, % d, % E, %, f, % G 5 variables in total), 1, 2, 3, and 3 (three variables to be assigned), 1, 2, 5, 7 (four variables to be assigned ), the number after tokens = can not be in order, but the order of writing corresponds to the order allocated to the variable. This is a value assignment. It is not necessary to use the do command later. In other words
---- Which segments are required at most?
The variable in (variable) represents the name of the starting variable, which is expanded by the total number defined in tokens. For example, the total number is 3, % C attaches % d and % E, and % C attaches % d % E... In this example, tokens = 4th requires only two values. The starting value is % C in the in () Brackets. Then, the first section in each line is assigned to % C, and section is assigned to the variable % d.
Take the second line (the first line is skip = 1 skipped) as an example, in the "Zhang sanmale 36 A-1" (is also separated by spaces) a total of five segments by a space knife, as long as 1st, 4, namely three assigned to % C, A-1 assigned to % d, execute @ echo % C % d and then next round... Empty rows are also removed.

Make a few changes:

For/F "skip = 1 tokens = 4,1 delims =-" % C in (D: \ abc.txt) Do @ echo % C % d

Displayed:
A zhangsan
B Li Si
A Zhao Liu

Example 7
Assume that the content of D: \ aa.txt is as follows:

Volume in drive D is myda
Volume serial number is C35D-8998

Directory of D: TMP

09/25/2001 am 11,235 yg0925.txt
11/12/2001 PM 795 buple.txt
04/11/2002 am 2,043 vitn.txt
3 file (s) 12,673 bytes
0 Dir (s) 5,020,200,655 bytes free

On the command line, enter:
For/F "Skip = 5 tokens = 5" % A In (d: \ aa.txt) Do @ echo %
Will display:
Yg0925.txt
Buple.txt
Vitn.txt
Free
The intention is to display the files listed in the file (of course, you can also use other commands to operate the file)
Ignore the first five lines through skip = 5. After tokens = 5 is separated by spaces by default, the file name is successfully assigned to the variable %, in the United States, the last row is not named by the file name (of course, other methods can be used to process this redundant, but the format of the last few rows is not provided in for/F ), the second to the last row has no fifth segment.
Obviously, the content in aa.txt is the content after the Dir command is executed. It can use similar commands:
Dir> D: \ aa.txt to create
If you add a proper parameter/B to the Dir, you can avoid unnecessary parts, add/ad to only display directories, and add/a-d to only display files.
Therefore, we can directly write commands into the ('COMMAND '-set) after in.
For/F "Skip = 5 tokens = 5" % A in ('dir') Do @ echo %

The effect is the same.
Note: The command set must be enclosed in single quotes to indicate that it is not a file set. If it is enclosed in double quotes, it indicates it is a string set. This example is used to describe the usage of the for command, if there is such a purpose, you are also willing to use the method described above. If nothing is displayed after you execute this example, You need to first execute the command in the set to view the display format, you may need to change tokens = 5 to tokens = 4. You may also need to add the/a-d parameter to the Dir to avoid displaying the directory.
If the set is composed of multiple files, after processing one file, it processes another file. The number of lines of each file varies by the number of cycles (the number of do commands) it will also be different.
If the set is a command-generated system, you must first be familiar with the character system that will produce the effect after the command is executed, in order to correctly arrange the following do command

Eye-catching: No matter which form of the in set, for/F is finally decomposed into strings and whether to "ignore several lines" (skip =) as needed), "What knife is used to split" (delims =), "Which segments are the most required?" (tokens =, the variable assigned to % or % and the variable that may be extended out by line-by-line are executed to execute the do command. Each row is a round of loop. All parameters are not completely described here. Use for/? In the command line /? View. (The italics below are the content in the help of copying)
For example:
For file names with spaces, you must enclose them in double quotation marks. To use double quotation marks in this way, you also need to use the usebackq option. Otherwise, double quotation marks are interpreted as defining a string to be analyzed. -- In other words, when the parameter usebackq (placed in the quotation marks after for/F) is included, double quotation marks in () indicate that the file name is still.
Another option is EOL =: Skip = indicates to ignore the first few rows. In fact, by default, all rows starting with semicolons (;) are ignored, if you want to ignore the rows starting with a semicolon or ignore the rows starting with a specific character, you can use EOL = your own character in the quotation mark parameter after for/F, but unlike delims =, it can define multiple, and only one can be defined.

Another example: % ~ The operator separates file names into separate parts, such as file names, extensions, and drive letters. For more information, see /? (The sample variable is % I ):
In addition, the replacement of the for variable reference has been enhanced. You can use the following syntax:

~ I-delete any quotation marks (") and expand % I
% ~ Fi-extend % I to a fully qualified path name
% ~ Di-only expand % I to one drive letter
% ~ Pi-only expand % I to one path
% ~ Ni-only expand % I to one file name
% ~ Xi-only expand % I to one file extension
% ~ The SI-extended path contains only short names.
% ~ Ai-extend % I to file attributes
% ~ Ti-extend % I to the date/time of the file
% ~ Zi-expand % I to the file size
% ~ $ Path: I-find the directory of the environment variable in the path and expand % I
The first fully qualified name found. If the environment variable name
Not defined, or the file is not found, this key combination is expanded to an empty string

You can combine modifiers to get multiple results:

% ~ DPI-only expand % I to one drive letter and Path
% ~ Nxi-only expand % I to one file name and extension
% ~ FSI-only expand % I to a complete path name with a short name
% ~ DP $ path: I-find the directory of the Environment Variable listed in the path and expand % I
To the first drive letter and path.
% ~ Ftzai-extended % I to the Dir similar to the output line

Note: All % ~ The headers are separated by file names or environment variables. To use each item freely, you need to work hard.

Exercise: (I am a little lazy and don't do it myself ...)
Traverse the C and D disks, find known file names (receiving keyboard input), and record the storage location and time to D: \ mynote.txt. The record format is as follows:

The XX files on drive C and drive D are as follows:
Time position
..... ......
..... ......
..... ......
.
.
.

Tip: doscommands, variables, and parameters that may be used: ECHO, set, set/P, % date %, % ~ >,>>

Summary and tips:
The actual usage of the for command has basically ended. However, this command cannot write batch processing with powerful functions. It is just a doscommand, you need to be familiar with some other DOS commands and the commands provided by Windows systems, and use them in combination to make full use of their powerful and practical functions, so that some complicated things can be processed unexpectedly concise and convenient.

Appendix: A common command or environment setting required in a batch for command:
The for command is actually a loop. If you change the value of an environment variable in each round, in the default state, A for command uses % environment variable % to get the value only once, in the next loop, the previous value (including the execution period of multiple commands in parentheses after do) is changed, and the following command is introduced:
Setlocal enabledelayedexpansion
Start the localization of Environment Changes in the batch file and start the extension of the delayed environment variable. When the execution of setlocal reaches the end of the batch file, an implicit endlocal is executed for each unexecuted setlocal command of the batch file.
When getting the variable value, use! Variable name! The latency environment variable extension allows you to use a different character (Exclamation point) to expand the environment variable during execution. This method is actually applicable to all compound commands in batch processing. If you do not want to retain the changed environment after the batch processing, we recommend that you always add setlocal.
If you combine some other complex system-related and network commands (such as WMIC and net), it is the hero of Fang Xian. For example, you can use commands to traverse a local disk: WMIC logicaldisk where "drivetype = 3" get name is easy to search for a file on all disks and perform corresponding operations, using the for command also requires the combination of other commands and Computer basics. Reprinted link: Workshop. However, the complexity of the help information is often daunting for beginners. Here, based on my learning and understanding, we will simplify its usage, and negligence and errors may be inevitable.
Basic Format
(The format used in the command line is written here. If it is in batch processing, add % to form % ):
For/parameter % variable in (SET) do command
(Note: Except for Chinese characters, the rest are written in accordance with its format requirements, and can be case sensitive)
Parameters: For is divided into four parameters: d l R f, and some parameters can be appended with other options.
Variable: (remember that if the for command is used in batch processing, the % before the variable must be changed to %) this variable name is composed of a single letter and case-sensitive. (In the original help, it is also feasible to use a single number as the variable name ), for example, % B and % B indicate different variables.
The for command assigns the value read from in (SET) to this variable in each loop for reference in the subsequent command.
Set: a set of series files, strings, or content generated by commands (of course, wildcards *?, Environment variables can also be referenced). The for command reads the content of a set multiple times in a certain order and regularity, assigns values to the variables, and executes the do command for the next round of loop, until the content in the set is read, and the brackets are in the required format (there must be spaces between the in and the brackets ).
Command: It can be any qualified DOS command or external program that can be called by DOS, and multiple commands can be enclosed in parentheses and executed in one loop.
Note: Some directories or file names may have spaces, so in many cases, the contents in the set and commands often need to be enclosed by English quotation marks (but sometimes the content in the quotation marks may be considered as strings) in some examples below, we will ignore the case where the file name or directory name contains spaces.
The following uses parameter classification as an example to explain its usage:

1. Parameter/d

For/d % variable in (SET) do command
The/d parameter specifies to only execute the for command for the directory rather than the file.
Example 1:
Input in the command line (not in batch, and will not be explained later)
For/d % A in (c: \ *. *) Do echo %
The running will display all directories under the root directory of the C drive in multiple times, without displaying the file name
It looks a little messy. If you display the command to the explicit shutdown, it will be clear:
For/d % A in (c: \ *. *) Do @ echo %

2. Parameter/R
/R parameters can be followed by drive letters and paths
For/R can contain the PATH % variable in (SET) do command
The path after/R refers to all directories in the entire directory tree under it (equivalent to the range in the doscommand tree). If it is only an English sentence ., it refers to the directory tree under the current path. If the path is omitted, it refers to the current directory, and the later in (SET) is equivalent to the file set that matches the previous directory.
Here, there are two conditions for wildcard characters in (SET ).
1) In (SET) does not contain wildcards
Specifies a single file or a list of specific files (multiple file names are separated by separators, such as spaces and commas)
Example 2
@ Echo
For/R. % I in (abc.txt) Do echo.> % I
Echo on
Note: There is only one path after for/R, and echo.> signature in each loop.
Example 3 (put into batch processing)
@ Echo off
Rem display D: list of all files in the drive named file1 and file2
For/r d: \ % H in (file1, file2) do if exist % H echo % H
Pause
2) In (SET) contains wildcards * or?
In this case, the DO command will process each object in the in (SET) Directory Series specified by/R, regardless of the directories without matching files.
Example 4:
@ Echo off
Rem deletes all *. Chk files in drive C.
For/R c: \ % H in (*. Chk) Do del/Q % H
Pause
Note: del/Q indicates deletion in quiet mode (no confirmation required)

3. Parameter/L

For/L % variable in (start value, value added each time, comparison value at end) do command
(L can also be used in lower case, mainly for the purpose of visually not confusing with number 1, but not using lower case)
(Start value, value added each time, comparison value at the end) is equivalent to an equi-difference digital sequence, starting from the number of the "start value" and increasing each time (can also be set to a negative number) it is "added each time" and compared with "compare value at End". If it exceeds the value, it exits the For Loop (or the do command after this round is not executed)
For example, (, 3) will generate a sequence (1 2 3); (, 9) will generate a sequence (1 3 5 7 9); (5) will generate a sequence (5 4 3 2 1); (, 18) will generate a sequence (1 7 10 13 16)
Example 5
@ Echo off
: Create aa1 ~ in disk D ~ Five aa5 folders
For/L % I in (1, 1, 5) Do md d: \ Aa % I
Pause

Note: At the beginning of a row, a single colon (:) is followed by a name, which is a label line and corresponds to the position indicated after go in batch processing. The double colon (:) is generally used for comments, annotations can be expressed by REM and space in batch processing. The two are slightly different. Rem comments are displayed on the screen when the command is not turned off for display :: it is not displayed under any circumstances.

Iv. Parameter/F
This parameter/F will open the files in the (SET), so that the for command can handle the editing operations such as reading text files, adding, deleting, and replacing text files, which is powerful, therefore, it is relatively complicated.
File Name-Set
For/F "option" % variable in ("string"-set) do command
'COMMAND '-Set
/F can be followed by several options, without options, of course, it is also a qualified format, and parameters must be enclosed in quotation marks as a whole. The following sets are mainly composed of three forms, finally, in each round of the For Loop, a row of strings will be read to assign values to the specified % variable and the additional variable assigned to the option. Then, run the command after do.
The following is an example to illustrate and gradually understand the usage of each sub-item.
Example 6
Assume that the content of D: \ abc.txt is as follows:
Name, gender, age, etc.-level
Michael Zhang 36 A-1
Li Si Men 29 B-2

Zhao liunv31 A-2

Run the following command:
For/F % C in (D: \ abc.txt) Do @ echo % C
The screen is displayed as follows:
Name
Zhang San
Li Si
Zhao Liu

Explanation: This is the default parameter option for/R before "% variable". In the loop, each wheel is separated by space by default and segmented to strings line by line in the open file, because no additional variable (that is, only one variable % C) is added, only the character in the first segment is assigned to % C, and then the do command is executed, and then the next round of the loop is carried out, empty rows are ignored by default.
Modify:
For/F "skip = 1 tokens = 1, 4, delims =" % C in (D: \ abc.txt) Do @ echo % C % d
Shown:
Zhang San A-1
Li Si B-2
Zhao Liu A-2

Solution:
Skip = 1 indicates that the number of lines that the text begins to ignore is 1-ignore several lines
Delims = what single symbol can be used in a row (there can be a combination of multiple characters, and spaces cannot be added between them. It is understood as multiple single characters. If a space character is required, it must be placed at the end) to separate strings as the unit for reading values (forming a segment). In this example, after a medium number, it is null to separate strings with spaces. -- What knife is used for splitting
Tokens = the number after the equal sign indicates to take the nth separated string segment in order to assign the % variable and the variable appended with the sequence respectively, in this example, the first section is assigned to % C, and the second section is assigned to C. The variable is assigned to % d, and can be written as tokens = 1st, 5-7 or tokens = 1, 2, 3 * or tokens = 1, 2, 5, 7 respectively indicate taking 1, 2, 5, 6, 7 (in turn assigned to % C, % d, % E, %, f, % G 5 variables in total), 1, 2, 3, and 3 (three variables to be assigned), 1, 2, 5, 7 (four variables to be assigned ), the number after tokens = can not be in order, but the order of writing corresponds to the order allocated to the variable. This is a value assignment. It is not necessary to use the do command later. In other words
---- Which segments are required at most?
The variable in (variable) represents the name of the starting variable, which is expanded by the total number defined in tokens. For example, the total number is 3, % C attaches % d and % E, and % C attaches % d % E... In this example, tokens = 4th requires only two values. The starting value is % C in the in () Brackets. Then, the first section in each line is assigned to % C, and section is assigned to the variable % d.
Take the second line (the first line is skip = 1 skipped) as an example, in the "Zhang sanmale 36 A-1" (is also separated by spaces) a total of five segments by a space knife, as long as 1st, 4, namely three assigned to % C, A-1 assigned to % d, execute @ echo % C % d and then next round... Empty rows are also removed.

Make a few changes:

For/F "skip = 1 tokens = 4,1 delims =-" % C in (D: \ abc.txt) Do @ echo % C % d

Displayed:
A zhangsan
B Li Si
A Zhao Liu

Example 7
Assume that the content of D: \ aa.txt is as follows:

Volume in drive D is myda
Volume serial number is C35D-8998

Directory of D: TMP

09/25/2001 am 11,235 yg0925.txt
11/12/2001 PM 795 buple.txt
04/11/2002 am 2,043 vitn.txt
3 file (s) 12,673 bytes
0 Dir (s) 5,020,200,655 bytes free

On the command line, enter:
For/F "Skip = 5 tokens = 5" % A In (d: \ aa.txt) Do @ echo %
Will display:
Yg0925.txt
Buple.txt
Vitn.txt
Free
The intention is to display the files listed in the file (of course, you can also use other commands to operate the file)
Ignore the first five lines through skip = 5. After tokens = 5 is separated by spaces by default, the file name is successfully assigned to the variable %, in the United States, the last row is not named by the file name (of course, other methods can be used to process this redundant, but the format of the last few rows is not provided in for/F ), the second to the last row has no fifth segment.
Obviously, the content in aa.txt is the content after the Dir command is executed. It can use similar commands:
Dir> D: \ aa.txt to create
If you add a proper parameter/B to the Dir, you can avoid unnecessary parts, add/ad to only display directories, and add/a-d to only display files.
Therefore, we can directly write commands into the ('COMMAND '-set) after in.
For/F "Skip = 5 tokens = 5" % A in ('dir') Do @ echo %

The effect is the same.
Note: The command set must be enclosed in single quotes to indicate that it is not a file set. If it is enclosed in double quotes, it indicates it is a string set. This example is used to describe the usage of the for command, if there is such a purpose, you are also willing to use the method described above. If nothing is displayed after you execute this example, You need to first execute the command in the set to view the display format, you may need to change tokens = 5 to tokens = 4. You may also need to add the/a-d parameter to the Dir to avoid displaying the directory.
If the set is composed of multiple files, after processing one file, it processes another file. The number of lines of each file varies by the number of cycles (the number of do commands) it will also be different.
If the set is a command-generated system, you must first be familiar with the character system that will produce the effect after the command is executed, in order to correctly arrange the following do command

Eye-catching: No matter which form of the in set, for/F is finally decomposed into strings and whether to "ignore several lines" (skip =) as needed), "What knife is used to split" (delims =), "Which segments are the most required?" (tokens =, the variable assigned to % or % and the variable that may be extended out by line-by-line are executed to execute the do command. Each row is a round of loop. All parameters are not completely described here. Use for/? In the command line /? View. (The italics below are the content in the help of copying)
For example:
For file names with spaces, you must enclose them in double quotation marks. To use double quotation marks in this way, you also need to use the usebackq option. Otherwise, double quotation marks are interpreted as defining a string to be analyzed. -- In other words, when the parameter usebackq (placed in the quotation marks after for/F) is included, double quotation marks in () indicate that the file name is still.
Another option is EOL =: Skip = indicates to ignore the first few rows. In fact, by default, all rows starting with semicolons (;) are ignored, if you want to ignore the rows starting with a semicolon or ignore the rows starting with a specific character, you can use EOL = your own character in the quotation mark parameter after for/F, but unlike delims =, it can define multiple, and only one can be defined.

Another example: % ~ The operator separates file names into separate parts, such as file names, extensions, and drive letters. For more information, see /? (The sample variable is % I ):
In addition, the replacement of the for variable reference has been enhanced. You can use the following syntax:

~ I-delete any quotation marks (") and expand % I
% ~ Fi-extend % I to a fully qualified path name
% ~ Di-only expand % I to one drive letter
% ~ Pi-only expand % I to one path
% ~ Ni-only expand % I to one file name
% ~ Xi-only expand % I to one file extension
% ~ The SI-extended path contains only short names.
% ~ Ai-extend % I to file attributes
% ~ Ti-extend % I to the date/time of the file
% ~ Zi-expand % I to the file size
% ~ $ Path: I-find the directory of the environment variable in the path and expand % I
The first fully qualified name found. If the environment variable name
Not defined, or the file is not found, this key combination is expanded to an empty string

You can combine modifiers to get multiple results:

% ~ DPI-only expand % I to one drive letter and Path
% ~ Nxi-only expand % I to one file name and extension
% ~ FSI-only expand % I to a complete path name with a short name
% ~ DP $ path: I-find the directory of the Environment Variable listed in the path and expand % I
To the first drive letter and path.
% ~ Ftzai-extended % I to the Dir similar to the output line

Note: All % ~ The headers are separated by file names or environment variables. To use each item freely, you need to work hard.

Exercise: (I am a little lazy and don't do it myself ...)
Traverse the C and D disks, find known file names (receiving keyboard input), and record the storage location and time to D: \ mynote.txt. The record format is as follows:

The XX files on drive C and drive D are as follows:
Time position
..... ......
..... ......
..... ......
.
.
.

Tip: doscommands, variables, and parameters that may be used: ECHO, set, set/P, % date %, % ~ >,>>

Summary and tips:
The actual usage of the for command has basically ended. However, this command cannot write batch processing with powerful functions. It is just a doscommand, you need to be familiar with some other DOS commands and the commands provided by Windows systems, and use them in combination to make full use of their powerful and practical functions, so that some complicated things can be processed unexpectedly concise and convenient.

Appendix: A common command or environment setting required in a batch for command:
The for command is actually a loop. If you change the value of an environment variable in each round, in the default state, A for command uses % environment variable % to get the value only once, in the next loop, the previous value (including the execution period of multiple commands in parentheses after do) is changed, and the following command is introduced:
Setlocal enabledelayedexpansion
Start the localization of Environment Changes in the batch file and start the extension of the delayed environment variable. When the execution of setlocal reaches the end of the batch file, an implicit endlocal is executed for each unexecuted setlocal command of the batch file.
When getting the variable value, use! Variable name! The latency environment variable extension allows you to use a different character (Exclamation point) to expand the environment variable during execution. This method is actually applicable to all compound commands in batch processing. If you do not want to retain the changed environment after the batch processing, we recommend that you always add setlocal.
If you combine some other complex system-related and network commands (such as WMIC and net), it is the hero of Fang Xian. For example, you can use commands to traverse a local disk: WMIC logicaldisk where "drivetype = 3" get name is easy to search for a file on all disks and perform corresponding operations, using the for command also requires the combination of other commands and Computer basics.

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.