[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[其他] 每天一文:for命令微软官网翻译。12.11.15

for命令着实话费了我不少功夫,由于该命令的重要性 我是每一句话都思考再三应该怎么翻译才敲定最终版本的。
老规矩 该命令的任何问题在回帖中提问,即使我不会版主也会回答的。。。

For


Runs a specified command for each file in a set of files.
为设定的每个文件执行指定操作。

Syntax
语法
________________________________________
for {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]

Parameters
参数
________________________________________

Parameter                                                           Description
参数                                                                        描述

{%%|%}<变量>                                                       Required. Represents a replaceable parameter. Use a single percent sign (%)to carry out the for command at the command prompt.
                                                                       Use double percent signs (%%) to carry out the for  command within a batch file. Variables are case sensitive, and they must be
                                                                       represented with an alphabetical value such as %A, %B, or %C.
                                                                       必须有的。表示一个可替换的参数。在命令行中使用一个百分号(%)来进行for命令。在批处理文件中使用两个百分号(%%)来进行for命令。该变量名区分大小写,
                                                                       并且你必须使用字母来表示变量就像%A,%B,或者%C。(在win7中实验发现一些符号和数字其实也可以来表示变量,不知道是功能扩展了还是我对上文的理解有错误。)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(<Set>)                                                               Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command. The parentheses are required.
                                                                       必须有的。指定一个或更多的文件,目录,或者文本字符,或者一个值的范围(这个是在指定l参数中的用法还是set中几个不同数字就代表值的范围?)来运行命令。双括号必须带上。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<Command>                                                       Required. Specifies the command that you want to carry out on each file, directory, or text string, or on the range of values included in Set.
                                                                       必须有的。指定你想要对每个在set中设定的文件,目录,或者文本字符,值的范围进行的操作。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<CommandLineOptions>                                              Specifies any command-line options that you want to use with the specified command.
                                                                      指定你想要使用的命令的选项。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/?                                                                       Displays help at the command prompt.
                                                                       在命令行中显示提示。

Remarks
备注
________________________________________
•        Using for
        使用for

You can use the for command within a batch file or directly from the command prompt.
你可以在批处理文件中使用for命令,或直接在命令行中使用。

•        Using batch parameters
使用批处理参数

The following attributes apply to the for command:

下面的属性应用于for命令

•        The for command replaces %Variable or %%Variable with each text string in the specified set until the specified command processes all of the files.
For命令将%variable或者%%variable置换为在set中指定的每个文本字符直到指定的命令运行完所有的文件。

•        Variable names are case sensitive, global, and no more than 52 can be active at a time.
整个命令中变量名字区分大小写,应用于for命令的全局中,最多同时可以使用52个字母。

•        To avoid confusion with the batch parameters %0 through %9, you can use any character for Variable except the numerals 0 through 9. For simple batch files, a single character such as %%f will work.
为了避免在批处理参数中使用%0-%9产生歧义,你可是使用任何字母作为变量除了字母0-9.对于简单的批处理文件来说,简单的一个字母%%f就行。

•        You can use multiple values for Variable in complex batch files to distinguish different replaceable variables.
在整个批处理文件中你可以使用多个值作为变量来区别不同的可替换变量。
(这句话怎么理解?是为了区别普通变量和for中设定的变量?设定%%i是否就存在了%i%变量?)

•        Specifying a group of files
指定一组文件

The Set parameter can represent a single group of files or several groups of files. You can use wildcard characters (* and ?) to specify a file set. The following are valid file sets:
Set参数可以代表一组文件或几组文件。你可以使用通配符(*和?)来指定文件设定。下列是允许的设定:
•        (*.doc)
•        (*.doc *.txt *.me)
•        (jan*.doc jan*.rpt feb*.doc feb*.rpt)
•        (ar??1991.* ap??1991.*)

When you use the for command, the first value
in Set replaces %Variable or %%Variable, and then the specified command processesin Set replaces %Variable or %%Variable, and then the specified command processes this value. This continues until all of the files (or groups of files) that correspond to
the Setvalue are processed.
当你使用for命令时,在set中的第一个值替换到%variable或%%variable中,然后指定的命令运行这个值。如此反复一直到所有set中的文件(或文件组)被运行。

•        Using the in and do keywords
        使用in和do关键字

In and do are not parameters, but you must use them with for. If you omit either of these keywords, an error message appears.
In和do不是参数,但你必须在for中使用他们。如果你漏掉任意一个关键字,就会提示出错。

•        Using additional forms of for
        使用for的扩展形式

If command extensions are enabled (that is the default), the following additional forms of for are supported:
如果命令行扩展是可用的(默认情况可用)for命令支持以下的附加形式:

•        Directories only
        只对目录操作

If Set contains wildcard characters (* or ?), the specified Command executes for each directory (instead of a set of files in a specified directory) that matches Set.
如果set包含通配符(*或?)指定的命令会执行set中每个每个目录(而不是设定的目录中的文件)。

The syntax is:
语法是:
•        for /d {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]

•        Recursive
        递归的

Walks the directory tree that is rooted at Driveath and executes the for statement in each directory of the tree. If no directory is specified after /r, the current directory is used as the root directory. If Set is just a single period (.), it only enumerates the directory tree.
遍历在drive:path下的目录树对目录树中的每个文件执行for语句。如果/r参数后没有指定目录。那么当前目录作为根目录。如果set仅仅是一个句点(.),则只列举目录树。

The syntax is:
语法是:
•        for /r [[<Drive>:]<ath>] {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]

•        Iterating a range of values
        在一定值范围内重复操作(迭代)
Use an iterative variable to set the starting value (Start#) and then step through a set range of values until the value exceeds the set ending value (End#). /l will execute the iterative by comparing Start# with End#. IfStart# is less than End# the command will execute. When the iterative variable exceeds End#, the command shell exits the loop. You can also use a negative Step# to step through a range in decreasing values. For example, (1,1,5) generates the sequence 1 2 3 4 5 and (5,-1,1) generates the sequence 5 4 3 2 1.
使用迭代变量来设置开始值然后在值的范围内增长直到超过结束值。/l将会执行这个重复通过比较开始值和结束值。如果开始值小于结束值,命令行将会执行。当迭代变量超过结束值时命令行会退出循环。你也可以使用负数作为step来反向增长数值。例如(1,1,5)形成数列1 2 3 4 5然而(5,-1,1)形成数列5 4 3 2 1

The syntax is:
语法是
•        for /l {%%|%}<Variable> in (<Start#>,<Step#>,<End#>) do <Command> [<CommandLineOptions>]

•        Iterating and file parsing
        迭代并解析文本

Use file parsing to process command output, strings, and file content. Use iterative variables to define the content or strings that you want to examine, and use the various ParsingKeywords options to further modify the parsing. Use the ParsingKeywords token option to specify which tokens should be passed as iterative variables. Note that when used without the token option, /f will only examine the first token.
使用文件解析来运行某个命令输出的字符,和文件内容。使用迭代变量来定义你要检查的内容或字符,然后使用不同的解析关键字选项来进一步修饰解析的具体方法细节。使用解析关键字token选项来确定哪些项将被传递给迭代变量。注意当不使用token选项时,/f将仅仅提取第一项。

File parsing consists of reading the output, string, or file content, and then breaking it into individual lines of text and parsing each line into zero or more tokens. The for loop is then called with the iterative variable value set to the token. By default, /f passes the first blank separated token from each line of each file. Blank lines are skipped.
文件解析包括读取输出,字符,或文件内容。并且将它分成独立的文本行并把每个行解析为0个或更多的项。For循环中的迭代变量被调用并设置为项。默认情况下,/f参数传递文件中每行第一个空格前面的项。空行跳过。

The syntaxes are:
语法为:
•        for /f ["<ParsingKeywords>"] {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]
•        for /f ["ParsingKeywords"] {%%|%}<Variable> in ("<LiteralString>") do <Command> [<CommandLineOptions>]
•        for /f ["<ParsingKeywords>"] {%%|%}<Variable> in ('<Command>') do <Command> [<CommandLineOptions>]

The Set argument specifies one or more file names. Each file is opened, read, and processed before moving to the next file in Set. To override the default parsing
behavior, specify ParsingKeywords. This is a quoted string that contains one or
more keywords to specify different parsing options.
set参数指定一个或多个文件名称。在继续到set中的下一个文件之前,每个文件都会被打开、读取和处理。为了覆盖默认解析选项,自己设定 "ParsingKeywords
(解析关键字)"。”parsingkeywords”是一个用引号括起来的字符串,它包含一个或多个关键字以指定不同的解析选项。


If you use the usebackq option, use one of the following syntaxes:

如果你使用usebackq选项,使用以下语法中的一个:
for /f ["usebackq <ParsingKeywords>"] {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]
for /f ["usebackq <ParsingKeywords>"] {%%|%}<Variable> in ("<LiteralString>") do <Command> [<CommandLineOptions>]



for /f ["usebackq <ParsingKeywords>"] {%%|%}<Variable> in ('<Command>') do <Command> [<CommandLineOptions>]

The following table lists the parsing keywords that you can use
下面的表格列出了你可以使用的传递关键字
for ParsingKeywords.

Keyword                               Description
关键字                                描述
----------------------------------------------------------------------------------------------------------------------------------
eol=<c>                                Specifies an end of line character (just one character).
                                        指定一个行结束符(只能是一个)。
----------------------------------------------------------------------------------------------------------------------------------
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 space and tab.
                                         指定分隔符设置。这将取代默认的空格和tab分隔符。
----------------------------------------------------------------------------------------------------------------------------------
tokens=<X,Y,M–N>                 Specifies which tokens from each line are to be passed to the for loop for each iteration. As a result, additional
                                         variable names are allocated. M–N specifies a range, from the Mth through the Nth tokens. If the last character in
                                         the tokens= string is an asterisk (*), an additional variable is allocated, and it receives the remaining text on the line after the last token that is parsed.
                                         确定每行的哪些项被取出传递给for循环中的每个迭代变量。当每行不止一个项时、剩余的项结果就分配给了附加变量。m-n 格式是一个范围,指定从第 m个 到 第n个项。如果 tokens=字符串中最后一个字符是星号 (*),
                                         则将除前面已经分配的项外所有的项给赋值到一个附加变量。

usebackq        Specifies to execute a back-quoted string as a command, and a single-quoted string as a literal string command. Also, allows file names in Set to be enclosed in quotation marks.
指定后引号中的字符作为命令执行,单引号中的字符作为字符串,当然,允许文件名置于set中并用双引号括起来。

•        Variable substitution
变量替换

The following table lists optional syntax (for any variable I).
以下列表列出了可选语法(可用于任何i变量)

Variable with modifier                                      Description
%~I                                                              Expands %I which removes any surrounding quotation marks (" ")
                                                              扩展%i删除任何引号(" ")
%~fI                                                              Expands %I to a fully qualified path name.
                                                              扩展%I到合格的路径名。
%~dI                                                              Expands %I to a drive letter only.
                                                              仅扩展%I一个驱动器字母。
%~pI                                                              Expands %I to a path only.
                                                              仅扩展%I到路径不含驱动器。
%~nI                                                              Expands %I to a file name only.
                                                              仅扩展%I到文件名。
%~xI                                                              Expands %I to a file name extension only.
                                                              仅扩展%I到文件扩展名。
%~sI                                                              Expands path to contain short names only.
                                                              仅扩展路径包含短名称。
%~aI                                                            Expands %I to the file attributes of file.
                                                              扩展%I到文件的属性。
%~tI                                                              Expands %I to the date and time of file.
                                                              扩展%I到文件的日期和时间。
%~zI                                                           Expands %I to the size of the file.
                                                              扩展%I到文件的大小。
%~$PATH:I                                                      Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first directory found.
                                                              If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.
                                                              搜索path环境变量中列出的目录并扩展%I到找到的第一个完全正确的目录名。如果环境变量名没有定义或文件没有发现,这个修饰符扩展到空字符。

The following table lists modifier combinations that you can use to get compound results.
以下列表列出你可以使用并得到符合结果的修饰组合。

Variable with combined modifiers                             Description
%~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 that are listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found.
%~ftzaI                                                             Expands %I to an output line that is like dir.

In the above examples, you can replace %I and PATH with other valid values. A valid for variable name terminates the %~ syntax.

在上面例子中,你可以使用其他的有效值置换%I和path。有效的变量名以这个格式结束%~语法


By using uppercase variable names such as %I, you can make your code more readable and avoid confusion with the modifiers, which are not case sensitive.
通过使用大写字母的变量名例如%I,你可以让你的代码更可读并避免由于修饰符和变量都小写导致的迷惑和歧义。
•        Parsing a string
解析字符

You can use the for /f parsing logic on an immediate string by wrapping Set in single quotes--for example, ('Set'). Set is treated as a single line of input from a file, and then it is parsed.
你可以使用for /f解析逻辑对直接字符串进行解析。通过使用单引号将set括起来例如——(’set’).set会被当作从文件中的一行输入,然后进行解析。
•        Parsing output
解析输出

You can use the for /f command to parse the output of a command by making a back-quoted string from the Set between the parentheses. It is treated as a command line, which is passed to a child Cmd.exe. The output is captured into memory and parsed as if it is a file.
你可以使用for /f命令通过使用后引号括起命令字符来解析命令输出.括号中的字符将会被当作命令行,传递给一个cmd.exe子程序,输出将会被抓取传递到内存然后当作文件传递。
Examples
例子
________________________________________
To use for in a batch file, use the following syntax:
要在批处理文件中使用for命令,使用以下的语法:
for {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]
To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type:
为了使用%f显示当前目录下所有以.doc和.txt为后缀的所有文件的内容,输入:
for %f in (*.doc *.txt) do type %f
In the preceding example, each file that has the .doc or .txt extension in the current directory is substituted for the %f variable until the contents of every file are displayed. To use this command in a batch file, replace every occurrence of %f with %%f. Otherwise, the variable is ignored and an error message is displayed.
在前面的例子中,当前目录下每个以.doc或.txt为扩展文件的都被替换为%f变量直到每个文件的内容都被显示。如果在批处理中使用这个命令,将每个出现的%f替换为%%f。否则,变量将会被忽略并显示错误信息。
To parse a file, ignoring commented lines, type:
为了在解析文件过程中忽略评论行,输入:
for /f "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %k
This command parses each line in Myfile.txt. It ignores lines that begin with a semicolon and passes the second and third token from each line to the for body (tokens are delimited by commas or spaces). The body of the for statement references %i to get the second token, %j to get the third token, and %k to get all of the remaining tokens. If the file names that you supply contain spaces, use quotation marks around the text (for example, "File Name"). To use quotation marks, you must use usebackq. Otherwise, the quotation marks are interpreted as defining a literal string to parse.
这个命令解析Myfile.txt文件中的每一行。它会忽略以分号开头的行并传递每一行的第二个和第三个项到for语句(每一项以空格和逗号分隔)。For语句引用%i来得到第二项,%j来得到第三项,%k得到剩余的所有项,如果你提供的文件名包含空格。使用引号标记在文件名字符的两边(例如,”文件 名”)。为了使用引号标记文件名,你必须使用usebackq。否则,引号标记部分被当作字符串来解析。
%i is explicitly declared in the for statement. %j and %k are implicitly declared by using tokens=. You can use tokens= to specify up to 26 tokens, provided that it does not cause an attempt to declare a variable higher than the letter "z" or "Z."
For语句中的%i是变量是明示出的,%j和%k变量通过使用tokens=赋值但并没有被明确显示。你可以使用tokens来指定最多26个项,提供的变量不会超过”z” 或者”Z”
The following example enumerates the environment variable names in the current environment. To parse the output of a command by placing Set between the parentheses, type:
下面的例子列举了当前环境中所有的环境变量的名字。通过放置set在括号中来解析命令的输出,输入:
for /f "usebackq delims==" %i in ('set') do @echo %i

返回列表