
每天一文for命令微軟官網(wǎng)翻譯121115for命令著實(shí)話費(fèi)了我不少功夫,由于該命令的重要性 我是每一句話都思考再三應(yīng)該怎么翻譯才敲定最終版本的。老規(guī)矩 該命令的任何問題在回帖中提問,即使我不會版主也會回答的。。。ForRuns a specified command for each file in a set of files.為設(shè)定的每個文件執(zhí)行指定操作。Syntax語法________________________________________for {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]Parameters參數(shù)________________________________________ Parameter Description參數(shù)描述{%%|%}<變量> 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 forcommand within a batch file. Variables are case sensitive, and they must berepresented with an alphabetical value such as %A, %B, or %C. 必須有的。表示一個可替換的參數(shù)。在命令行中使用一個百分號(%)來進(jìn)行for命令。在批處理文件中使用兩個百分號(%%)來進(jìn)行for命令。該變量名區(qū)分大小寫, 并且你必須使用字母來表示變量就像%A,%B,或者%C。(在win7中實(shí)驗發(fā)現(xiàn)一些符號和數(shù)字其實(shí)也可以來表示變量,不知道是功能擴(kuò)展了還是我對上文的理解有錯誤。)-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(<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參數(shù)中的用法還是set中幾個不同數(shù)字就代表值的范圍?)來運(yùn)行命令。雙括號必須帶上。-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<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中設(shè)定的文件,目錄,或者文本字符,值的范圍進(jìn)行的操作。-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<CommandLineOptions>Specifies any command-line options that you want to use with the specified command.指定你想要使用的命令的選項。-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/? Displays help at the command prompt. 在命令行中顯示提示。Remarks備注________________________________________•Using for使用forYou can use the for command within a batch file or directly from the command prompt.你可以在批處理文件中使用for命令,或直接在命令行中使用。•Using batch parameters 使用批處理參數(shù)The following attributes apply to the for command:下面的屬性應(yīng)用于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中指定的每個文本字符直到指定的命令運(yùn)行完所有的文件。•Variable names are case sensitive, global, and no more than 52 can be active at a time. 整個命令中變量名字區(qū)分大小寫,應(yīng)用于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. 為了避免在批處理參數(shù)中使用%0-%9產(chǎn)生歧義,你可是使用任何字母作為變量除了字母0-9.對于簡單的批處理文件來說,簡單的一個字母%%f就行。•You can use multiple values for Variable in complex batch files to distinguish different replaceable variables. 在整個批處理文件中你可以使用多個值作為變量來區(qū)別不同的可替換變量。(這句話怎么理解?是為了區(qū)別普通變量和for中設(shè)定的變量?設(shè)定%%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參數(shù)可以代表一組文件或幾組文件。你可以使用通配符(*和?)來指定文件設(shè)定。下列是允許的設(shè)定:•(*.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.當(dāng)你使用for命令時,在set中的第一個值替換到%variable或%%variable中,然后指定的命令運(yùn)行這個值。如此反復(fù)一直到所有set中的文件(或文件組)被運(yùn)行。•Using the in and do keywords 使用in和do關(guān)鍵字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不是參數(shù),但你必須在for中使用他們。如果你漏掉任意一個關(guān)鍵字,就會提示出錯。•Using additional forms of for使用for的擴(kuò)展形式If command extensions are enabled (that is the default), the following additional forms of for are supported:如果命令行擴(kuò)展是可用的(默認(rèn)情況可用)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包含通配符(*或?)指定的命令會執(zhí)行set中每個每個目錄(而不是設(shè)定的目錄中的文件)。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下的目錄樹對目錄樹中的每個文件執(zhí)行for語句。如果/r參數(shù)后沒有指定目錄。那么當(dāng)前目錄作為根目錄。如果set僅僅是一個句點(diǎn)(.),則只列舉目錄樹。The syntax is: 語法是:•for /r [[<Drive>:]<ath>] {%%|%}<Variable> in (<Set>) do <Command> [<CommandLineOptions>]•Iterating a range of values 在一定值范圍內(nèi)重復(fù)操作(迭代)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. 使用迭代變量來設(shè)置開始值然后在值的范圍內(nèi)增長直到超過結(jié)束值。/l將會執(zhí)行這個重復(fù)通過比較開始值和結(jié)束值。如果開始值小于結(jié)束值,命令行將會執(zhí)行。當(dāng)?shù)兞砍^結(jié)束值時命令行會退出循環(huán)。你也可以使用負(fù)數(shù)作為step來反向增長數(shù)值。例如(1,1,5)形成數(shù)列1 2 3 4 5然而(5,-1,1)形成數(shù)列5 4 3 2 1The 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.使用文件解析來運(yùn)行某個命令輸出的字符,和文件內(nèi)容。使用迭代變量來定義你要檢查的內(nèi)容或字符,然后使用不同的解析關(guān)鍵字選項來進(jìn)一步修飾解析的具體方法細(xì)節(jié)。使用解析關(guān)鍵字token選項來確定哪些項將被傳遞給迭代變量。注意當(dāng)不使用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. 文件解析包括讀取輸出,字符,或文件內(nèi)容。并且將它分成獨(dú)立的文本行并把每個行解析為0個或更多的項。For循環(huán)中的迭代變量被調(diào)用并設(shè)置為項。默認(rèn)情況下,/f參數(shù)傳遞文件中每行第一個空格前面的項。空行跳過。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參數(shù)指定一個或多個文件名稱。在繼續(xù)到set中的下一個文件之前,每個文件都會被打開、讀取和處理。為了覆蓋默認(rèn)解析選項,自己設(shè)定 ParsingKeywords(解析關(guān)鍵字)。”parsingkeywords”是一個用引號括起來的字符串,它包含一個或多個關(guān)鍵字以指定不同的解析選項。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 下面的表格列出了你可以使用的傳遞關(guān)鍵字for ParsingKeywords. Keyword Description關(guān)鍵字描述----------------------------------------------------------------------------------------------------------------------------------eol=<c>Specifies an end of line character (just one character).指定一個行結(jié)束符(只能是一個)。----------------------------------------------------------------------------------------------------------------------------------skip=<N> Specifies the number of lines to skip at the beginning of the file. 指定開始時要跳過的行數(shù)。----------------------------------------------------------------------------------------------------------------------------------delims=<xxx> Specifies a delimiter set. This replaces the default delimiter set of space and tab. 指定分隔符設(shè)置。這將取代默認(rèn)的空格和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, additionalvariable names are allocated. M–N specifies a range, from the Mth through the Nth tokens. If the last character inthe 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循環(huán)中的每個迭代變量。當(dāng)每行不止一個項時、剩余的項結(jié)果就分配給了附加變量。m-n 格式是一個范圍,指定從第 m個 到 第n個項。如果 tokens=字符串中最后一個字符是星號 (*), 則將除前面已經(jīng)分配的項外所有的項給賦值到一個附加變量。usebackqSpecifies 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. 指定后引號中的字符作為命令執(zhí)行,單引號中的字符作為字符串,當(dāng)然,允許文件名置于set中并用雙引號括起來。•Variable substitution 變量替換The following table lists optional syntax (for any variable I).以下列表列出了可選語法(可用于任何i變量) Variable with modifierDescription%~IExpands %I which removes any surrounding quotation marks ( )擴(kuò)展%i刪除任何引號( )%~fIExpands %I to a fully qualified path name.擴(kuò)展%I到合格的路徑名。%~dIExpands %I to a drive letter only.僅擴(kuò)展%I一個驅(qū)動器字母。%~pIExpands %I to a path only.僅擴(kuò)展%I到路徑不含驅(qū)動器。%~nIExpands %I to a file name only.僅擴(kuò)展%I到文件名。%~xIExpands %I to a file name extension only.僅擴(kuò)展%I到文件擴(kuò)展名。%~sIExpands path to contain short names only.僅擴(kuò)展路徑包含短名稱。%~aIExpands %I to the file attributes of file.擴(kuò)展%I到文件的屬性。%~tIExpands %I to the date and time of file.擴(kuò)展%I到文件的日期和時間。%~zI Expands %I to the size of the file.擴(kuò)展%I到文件的大小。%~$PATH:ISearches 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環(huán)境變量中列出的目錄并擴(kuò)展%I到找到的第一個完全正確的目錄名。如果環(huán)境變量名沒有定義或文件沒有發(fā)現(xiàn),這個修飾符擴(kuò)展到空字符。The following table lists modifier combinations that you can use to get compound results.以下列表列出你可以使用并得到符合結(jié)果的修飾組合。 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。有效的變量名以這個格式結(jié)束%~語法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,你可以讓你的代碼更可讀并避免由于修飾符和變量都小寫導(dǎo)致的迷惑和歧義。•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解析邏輯對直接字符串進(jìn)行解析。通過使用單引號將set括起來例如——(’set’).set會被當(dāng)作從文件中的一行輸入,然后進(jìn)行解析。•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命令通過使用后引號括起命令字符來解析命令輸出.括號中的字符將會被當(dāng)作命令行,傳遞給一個cmd.exe子程序,輸出將會被抓取傳遞到內(nèi)存然后當(dāng)作文件傳遞。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顯示當(dāng)前目錄下所有以.doc和.txt為后綴的所有文件的內(nèi)容,輸入: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.在前面的例子中,當(dāng)前目錄下每個以.doc或.txt為擴(kuò)展文件的都被替換為%f變量直到每個文件的內(nèi)容都被顯示。如果在批處理中使用這個命令,將每個出現(xiàn)的%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 %kThis 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得到剩余的所有項,如果你提供的文件名包含空格。使用引號標(biāo)記在文件名字符的兩邊(例如,”文件 名”)。為了使用引號標(biāo)記文件名,你必須使用usebackq。否則,引號標(biāo)記部分被當(dāng)作字符串來解析。%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:下面的例子列舉了當(dāng)前環(huán)境中所有的環(huán)境變量的名字。通過放置set在括號中來解析命令的輸出,輸入:for /f usebackq delims== %i in ('set') do @echo %i

