![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-13 23:02 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS学习入门 & 精彩文章 (教学室) » Introducing the FOR Command (repost) |
| Printable Version 655 / 2 |
| Floor1 gjfmlj | Posted 2003-09-13 00:00 |
| 中级用户 Posts 118 Credits 421 | |
|
FOR %variable IN (set) DO command
%variable Specifies a single-letter replaceable parameter. (set) Specifies one file or a set of files. Wildcards may be used. command Specifies the command to carry out for each file. command-parameters Specifies parameters or command-line switches for the specified command. When using the FOR command in a batch file, specify the variable as %%variable instead of %variable. Variable names are case-sensitive, so %i is different from %I. If command extensions are enabled, the following additional forms of the FOR command are supported: FOR /D %variable IN (set) DO command If set contains wildcards, then it specifies matching against directory names instead of file names. FOR /R path] %variable IN (set) DO command Examines the directory tree rooted at path, and executes the FOR statement in each directory of the tree. If no directory is specified after /R, then the current directory is used. If set is just a single period (.) character, then the directory tree is enumerated. FOR /L %variable IN (start,step,end) DO command The set represents a sequence of numbers from start to end, in incremental form. Thus, (1,1,5) produces the sequence 1 2 3 4 5, and (5,-1,1) produces the sequence (5 4 3 2 1). FOR /F %variable IN (file-set) DO command FOR /F %variable IN ("string" DO command FOR /F %variable IN ('command' DO command Or, if the usebackq option is present: FOR /F %variable IN (file-set) DO command FOR /F %variable IN ("string" DO command FOR /F %variable IN ('command' DO command filenameset consists of one or more file names. Before moving on to the next file in filenameset, each file has been opened, read, and processed. Processing consists of reading the file, breaking it into individual lines of text, and then parsing each line into zero or more tokens. The For loop is then called with the variable value(s) set to the token string(s) found. By default, /F passes the first blank-separated token from each line of each file. Blank lines are skipped. You can override the default parsing behavior by specifying the optional "options" parameter. This quoted string consists of one or more keywords specifying different parsing options. These keywords are: eol=c - specifies an end-of-line comment character (just one) skip=n - specifies the number of lines to skip at the beginning of the file. delims=xxx - specifies a delimiter set. This replaces the default delimiter set of spaces and tabs. tokens=x,y,m-n - specifies which token from each line is to be passed to the for body for each iteration. This causes additional variable names to be allocated. The m-n form is a range, specifying the mth through nth tokens. If the last character in the token string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed. usebackq - specifies that the new syntax is in use in the following cases: a back-quoted string is executed as a command and a single-quoted character is used as a literal string command, and also allows double-quoted file names in filenameset. Some examples may help: FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k would parse each line in myfile.txt, ignoring those lines that begin with a semicolon, passing the second and third token from each line to the for body; with comma and/or space as delimiters. Note that the statements in the for body reference %i to get the second token, %j to get the third token, and %k to get all remaining tokens after the third token. For file names that contain spaces, you need to enclose the file name 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 parse. %i is explicitly declared in the for statement, while %j and %k are implicitly declared through the tokens= option. You can specify up to 26 tokens on a tokens= line, provided it does not attempt to declare a variable higher than the letter 'z' or 'Z'. Keep in mind that FOR variables are single-letter, case-sensitive, and global; also, no more than 52 can be in use at one time. You can also use the FOR /F parsing logic on an immediate string; to do this, enclose the filenameset between the parentheses in single quotes. In that case, the string will be treated as a single input line from a file. Finally, you can use the FOR /F command to parse the output of a command. To do so, make the filenameset between the parentheses a back-quoted string. This string will be treated as a command line, passed to a child CMD.EXE whose output will be captured into memory and parsed as if it were a file. Therefore, 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 optional syntax: ~I - removes any surrounding quotes(" , expanding %I%~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes %~tI - expands %I to file date/time %~zI - expands %I to file size %~$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, then this combination expands to the empty string Modifiers can be combined to get compound results: %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~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 by other valid values. The %~ syntax is terminated by a valid FOR variable name. Using uppercase variable names like %I is more readable, and avoids confusion with case-insensitive combinations. |
|
| Floor2 cn_archer | Posted 2003-09-13 00:00 |
| 元老会员 Posts 991 Credits 2,903 From 福建省 | |
|
Not bad, bumping it up
|
|
| Floor3 duxuelj520 | Posted 2004-08-03 00:00 |
| 初级用户 Posts 55 Credits 217 | |
|
Mm
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |