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

[代码合集] [分享]突然明白了批处理for /f的userbackq的用法

本帖最后由 pcl_test 于 2016-11-18 17:32 编辑

userbackq就是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 %k

        would 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 %i

        would enumerate the environment variable names in the current environment.
将枚举当前环境变量下的环境变量名称。

呵呵 微软的那么多帮助文档不都是没用的

linux 为什么没有流行。。 。帮助文档也是一方面
解释执行的脚本语言与编译语言还是有很大差别的。。。

TOP

2# techon


Linux哪个方面的帮助文档不够详细?
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

LZ的E文很不错啊,可惜我只能在别人的理解中进行理解了,悲剧啊
枫中残雪:风停了,我的心却在动,让我心中的寒意走向远方

TOP

3# Batcher


linux的帮助文档还是算详细的,只是都是英文的,加上国内的linux氛围根本不行……

TOP

本帖最后由 applba 于 2011-5-22 16:40 编辑

6# zm900612


你误解我了。我知道有中文的帮助,只是翻译的水平太差了,很多地方读取来让人摸不着头脑。
我自认为我能比微软翻译的更通顺。

你发的那两个文件时64bit的吧,我是32位的系统,winxp,win7,ubuntu三系统……

TOP

哇,发现竟然能删自己的贴,原来一直都没发现...
我的是32位win7

微软自带的中文帮助翻译得确实不咋地,不过我就是靠这个自学的,感觉还算够用。话说回来,兄台英语真好,有六级不?

TOP

7# zm900612


其实大部分命令的帮助信息微软翻译的还过得去,就是for这一段的翻译完全让人晕掉。

还有我翻遍了所有的原版字典,没有找到token一词的合理解释,为什么要用token啊。

六级和这个不沾边啊,过了六级也看不懂这么些专业术语的,我是当年啃了不少英文原著的缘故。

TOP

返回列表