
[分享]突然明白了批處理for/f的userbackq的用法最后由 pcl_test 于 -11-18 17:32userbackq就是use(使用) back(反) quotes(引號)。順便翻譯一下英文的for /f部分。FOR /F [options] %variable IN (filenameset) 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 usebackq option present:如果usebackq選項出現:FOR /F [options] %variable IN (filenameset) 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 file names. filenameset是一個或多個文件名。 Each file is opened, read and processed before going on to the next file in filenameset.每個文件都會被打開、讀取和處理,在繼續處理filenameset中的下一個文件之前前。Processing consists of reading in the file, breaking it up into individual lines of text and then parsing each line into zero or more tokens.處理過程包括讀取文件內容,并將其分成獨立的文本行,然后解析每一行到0個或以上token。The body of the for loop is then called with the variable value(s) set to the found token string(s).然后for循環的主主體被調用,同時 進行變量值的設定(給已知的token字符串)。By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped.默認情況下,/F 穿過(即無視)每個文件每一行的第一個空白的孤立的token。空白行會被跳過(即被無視)。You can override the default parsing behavior by specifying the optional options parameter.通過指定可選的options參數,你可以不理會上述默認的解析操作。This is a quoted string which contains one or more keywords to specify different parsing options. 它是一個“被引號”的字符串,包含一個或多個用來指定不同解析選項的關鍵詞。The 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 space and tab. 指定分隔符集合。這會取代默認的分隔符集合(空格和Tab)tokens=x,y,m-n- specifies which tokens from each line are to be passed to the for body for each iteration.指定每次迭代過程中,每一行的哪個token被傳遞到FOR主體。This will cause additional variable names to be allocated.這將引起額外的變量名稱被分配。The m-n form is a range, specifying the mth through the nth tokens.m-n形式表示一個范圍,指定第m至第n個token。If the last character in the tokens= string is an asterisk,then an additional variable is allocated and receives the remaining text on the line after the last token parsed.如果最后一個字符是星號,一個附加的變量被分配,用于最后一個token被解析后接收剩余的文本。usebackq- specifies that the new semantics are in force, where a back quoted string is executed as a command and a single quoted string is a literal string command and allows the use of double quotes to quote file names in filenameset.強制指定新語法:即反引號中的字符串將作為一個命令被執行,單引號的中字符串是字面意義上的字符串,從而允許在filenameset中使用雙引號括起文件名。Some examples might help: 一些例子可能會有所幫助: FOR /F eol=; tokens=2,3* delims=, %i in (myfile.txt) do @echo %i %j %kwould parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces.將會解析myfile.txt中的每一行,忽略那些以分號開頭的行,傳遞來自每一行的第二和第三個token給FOR主體,同時用逗號和空格來界定token。Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd.注意,FOR主體語句使用%i來獲取第二個token,使用%j來獲取第三個token,使用%K來獲取第三個token以后所有剩余的token。For file names that contain spaces, you need to quote the filenames with double quotes.對于那些包含空格的文件名,你需要使用雙引號將他們包括起來。In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.為了在這種情形下使用雙引號,你需要使用usebackq選項,否則雙引號將被解釋成“定義一個要解析的字面意義上的字符串”。%i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option.%i在for語句中是被明確聲明的,%j和%k是通過tokens= 選項被被隱含聲明的。You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z' or 'Z'.你可以通過tokens= 行 指定最多26個token,它不會嘗試 去聲明一個高于字母“z”或“Z”的變量。Remember, FOR variables are single-letter, case sensitive, global, and you can't have more than 52 total active at any one time.請記住,for變量是單一字母的,大小寫敏感的,全局作用范圍的,而且你不能同時使用52個以上的FOR變量。You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis a quoted string,你還可以在一個“直接性的字符串”上使用FOR /F 邏輯,通過使用單引號把()中的filenameset變成一個有引號的字符串。using single quote characters.It will be treated as a single line of input from a file and parsed.這個字符串將被當作一個從一個文件輸入的單行,被解析。Finally, you can use the FOR /F command to parse the output of a command.最后,你可以使用FOR /F 命令來解析一個命令的輸出。You do this by making the filenameset between the parenthesis a back quoted string.你可以通過把 括號中的 filenameset 變成一個帶反括號的字符串 來實現這一點。It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file.該字符串被當作一個命令行,被傳遞到一個cmd.exe子進程,其輸出被捕獲到內存,并像一個文件一樣被解析。So the following example:因此下面的例子:FOR /F usebackq delims== %i IN (`set`) DO @echo %iwould enumerate the environment variable names in the current environment.將枚舉當前環境變量下的環境變量名稱。

