China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-20 10:47
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » The Strongest DOS Command – for (It is a good resource for novices to learn the FOR statement) View 53,114 Replies 259
Floor 226 Posted 2008-02-20 12:10 ·  中国 北京 联通
新手上路
Credits 12
Posts 6
Joined 2006-12-21 02:12
19-year member
UID 74095
Gender Male
Status Offline
Thanks to the LZ, I've really learned a lot after reading it
Floor 227 Posted 2008-02-20 16:13 ·  中国 广东 东莞 电信
初级用户
Credits 22
Posts 10
Joined 2007-01-23 21:51
19-year member
UID 77446
Gender Male
Status Offline
Give it a thumbs up, thanks for your hard work
Floor 228 Posted 2008-02-21 18:35 ·  中国 河南 驻马店 电信
初级用户
Credits 32
Posts 13
Joined 2007-09-14 00:05
18-year member
UID 97332
Gender Male
Status Offline
Originally posted by bagpipe at 2006-3-2 01:54 PM:
For example, with appropriate parameters, the FOR command can convert the output of date /t from "Sat 07/13/2002" to the format you want, such as "2002-07-13":

c:\>for /f "tokens=2,3,4 delims=/ " %a in ('date /t') do @echo %c-%a-%b
2002-07-13

This first example is invalid in VISTA! It works in XP!
Floor 229 Posted 2008-03-21 11:52 ·  中国 安徽 合肥 电信
初级用户
Credits 27
Posts 25
Joined 2008-03-17 14:52
18-year member
UID 113207
Gender Male
Status Offline
Floor 230 Posted 2008-04-11 10:44 ·  中国 四川 内江 电信
初级用户
★★
Credits 120
Posts 62
Joined 2008-04-11 09:52
18-year member
UID 115566
Gender Male
From 四川内江
Status Offline
I can only learn slowly. I didn't feel anything after watching it once. Maybe it will be better next time! Thanks, forum administrator!
Floor 231 Posted 2008-04-11 15:55 ·  中国 广东 广州 黄埔区 电信
新手上路
Credits 4
Posts 2
Joined 2008-04-11 15:27
18-year member
UID 115599
Gender Female
Status Offline
Floor 232 Posted 2008-04-12 21:42 ·  中国 广东 广州 电信
初级用户
Credits 71
Posts 34
Joined 2008-03-12 19:53
18-year member
UID 112775
Gender Male
Status Offline
Floor 233 Posted 2008-05-18 11:29 ·  中国 河南 周口 联通
初级用户
Credits 35
Posts 19
Joined 2008-05-14 19:22
18-year member
UID 118680
Gender Male
Status Offline
Execute a specific command for each file in a group of files.

FOR %variable IN (set) DO command [command-parameters]

%variable specifies a single alphabetic replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specifies parameters or command-line switches for the specific command.

When using the FOR command in a batch file, specify the variable using %%variable instead of %variable. Variable names are case-sensitive, so %i is different from %I.

If command extensions are enabled, the following additional FOR command formats are supported:

FOR /D %variable IN (set) DO command [command-parameters]

If wildcards are included in the set, it specifies matching directory names instead of file names.

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Checks the directory tree rooted at [drive:]path, directing the FOR statement to each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.), the directory tree is enumerated.

FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set represents a sequence of numbers from start to end in increments. Thus, (1,1,5) will generate the sequence 1 2 3 4 5, and (5,-1,1) will generate the sequence (5 4 3 2 1).

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]

Or, if the usebackq option is used:

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]

filenameset is one or more filenames. Each file is opened, read, and processed before continuing to the next file in filenameset. Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more tokens. The For loop is then called with the found token string variable values. By default, /F separates by the first blank token in each line of each file. Blank lines are skipped. You can substitute the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:

eol=c - specifies the end of a line comment character (just one)
skip=n - specifies the number of lines to skip at the beginning of the file.
delims=xxx - specifies the delimiter set. This replaces the default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens of each line are passed to each iteration of the for itself. This causes the assignment of additional variable names. The m-n format is a range. Specifies the mth through the nth token. If the last character in the token string is an asterisk, then additional variables are assigned and receive the remaining text of the line after the last token parsed.
usebackq - specifies that the new syntax is used in the following cases:
executing a backquoted string as a command and a single quote character as a literal string command and allowing file names to be enclosed in double quotes in filenameset.

Some examples may be helpful:

FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k

will parse each line in myfile.txt, skipping lines that begin with a semicolon, passing the second and third tokens of each line to the for body; delimited by commas and/or spaces. Note that the for body statement references %i to get the second token, %j to get the third token, and %k to get all remaining tokens after the third token. For filenames with spaces, you need to enclose the filename in double quotes. To use double quotes in this way, you also need to use the usebackq option, otherwise, the double quotes will be interpreted as defining a string to be parsed.

%i is specifically explained in the for statement, and %j and %k are specifically explained by the tokens= option. You can specify up to 26 tokens in a tokens= line, as long as you do not attempt to define a variable higher than 'z' or 'Z'. Remember that FOR variables are single alphabetic, case-sensitive, and global; and, there can be no more than 52 in use at the same time.

You can also use the FOR /F parsing logic on adjacent strings; by enclosing the filenameset between parentheses in single quotes. Thus, the string is treated as a single input line in a file.

Finally, you can use the FOR /F command to parse the output of a command. By making the filenameset between parentheses a backquoted string. The string is treated as a command line, passed to a sub CMD.EXE, whose output is captured in memory and treated as a file to parse. Thus, the following example:

FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

will enumerate the names of environment variables in the current environment.

In addition, substitution of FOR variable references has been enhanced. You can now use the following option syntax:

~I - removes any quotes (") and expands %I
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to just a drive letter
%~pI - expands %I to just a path
%~nI - expands %I to just a file name
%~xI - expands %I to just a file extension
%~sI - expanded path contains only short names
%~aI - expands %I to the file attributes of the file
%~tI - expands %I to the date/time of the file
%~zI - expands %I to the size of the file
%~$PATH:I - searches the directories listed in the path environment variable and expands %I to the first fully qualified name found. If the environment variable name is not defined or the file is not found, this combination expands to an empty string

You can combine modifiers to get multiple results:

%~dpI - expands %I to just a drive letter and path
%~nxI - expands %I to just a file name and extension
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the directories listed in the path environment variable and expands %I to the first drive letter and path found.
%~ftzaI - expands %I to a DIR-like output line

In the above examples, %I and PATH can be replaced with other valid values. The %~ syntax terminates with a valid FOR variable name. Choosing an uppercase variable name like %I is more readable and avoids confusion with case-insensitive combinations.
Floor 234 Posted 2008-05-18 15:12 ·  中国 江西 吉安 永新县 电信
初级用户
Credits 45
Posts 22
Joined 2007-09-22 20:42
18-year member
UID 98075
Gender Male
Status Offline
There are some typos in the original text. The correct translation should be: My hard study, there are too many things I don't understand, so I need to make up a lot of lessons.
Floor 235 Posted 2008-05-24 16:47 ·  中国 江苏 南京 电信
初级用户
Credits 58
Posts 30
Joined 2008-05-02 16:30
18-year member
UID 117573
Gender Male
From 山东
Status Offline
FOR IF …………
???
Floor 236 Posted 2008-05-24 17:27 ·  中国 湖南 长沙 电信
新手上路
Credits 6
Posts 3
Joined 2008-05-23 19:08
18-year member
UID 119523
Gender Male
Status Offline
Floor 237 Posted 2008-05-27 10:12 ·  中国 山东 青岛 联通
初级用户
Credits 21
Posts 8
Joined 2007-03-11 23:05
19-year member
UID 81438
Gender Male
Status Offline
Floor 238 Posted 2008-05-27 19:39 ·  中国 重庆 电信
新手上路
Credits 4
Posts 2
Joined 2008-05-21 23:01
18-year member
UID 119354
Gender Male
Status Offline
Floor 239 Posted 2008-05-30 10:12 ·  中国 江苏 南京 电信
新手上路
Credits 8
Posts 4
Joined 2008-05-30 09:48
18-year member
UID 120058
Gender Male
Status Offline
Thanks to the author, thanks for the guidance!
Floor 240 Posted 2008-08-28 19:49 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 133
Posts 75
Joined 2008-08-03 01:08
17-year member
UID 122677
Gender Male
Status Offline
Not familiar, how do you feel something is going to go wrong

Hehe, maybe newbies are a bit unconfident
Forum Jump: