简体   繁体   English

批处理FOR循环delims不行为

[英]Batch FOR loop delims not behaving

I am trying to return the install location of a program using REG QUERY inside a FOR loop. 我试图在FOR循环中使用REG QUERY返回程序的安装位置。 My command is as follows: 我的命令如下:

FOR /F "TOKENS=2 DELIMS=REG_SZ" %%a IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\TeamSpeak 3 Client" /V "" 2^>NUL') DO SET TSLOC=%%a

Now, REG QUERY on its own returns: 现在,REG QUERY自己的回报:

    (Default)    REG_SZ    C:\Program Files\TeamSpeak 3 Client

But using the delimiter REG_SZ returns: 但是使用分隔符REG_SZ返回:

C:\Users\Jake\Desktop>FOR /F "TOKENS=2 DELIMS=REG_SZ" %a IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\TeamSpeak 3 Client" /V "" 2>NUL') DO SET TSLOC=%a
C:\Users\Jake\Desktop>SET TSLOC=Y
C:\Users\Jake\Desktop>SET TSLOC=    C:\Program Files (x86)\Team

So obviously I only get up to \\Team at the end. 显然我最终只能到达\\团队。 I have no idea where it gets the "Y" from or why the delim is causing it to split at \\Team. 我不知道它从哪里获得“Y”或为什么delim导致它在\\ Team分裂。 If I change my token # to 3 it returns: 如果我将令牌#更改为3,则返回:

peak 3 Client

So somehow it must be interpreting " s" as my delimiter, right? 所以它必须以某种方式将“s”解释为我的分隔符,对吧?

My question is why (or if not, what is it doing?) and how can I get it to give me the whole thing. 我的问题是为什么(或者如果没有,它在做什么?)以及如何让它给我整个事情。 What am I doing wrong? 我究竟做错了什么?

DELIMS is a set of characters, not a single string. DELIMS是一组字符,而不是单个字符串。 Therefore any character out of R , E , G , _ , S , or Z will act as a delimiter. 因此, REG_SZ任何字符都将充当分隔符。

If REG QUERY returns the string below (spaces included): 如果REG QUERY返回下面的字符串(包含空格):

    (Default)    REG_SZ    C:\Program Files\TeamSpeak 3 Client

... then you want the token after the second one separated by spaces (that is the default tokens separator), that is: ...然后你想要第二个之后用空格分隔的标记(即默认的标记分隔符),即:

FOR /F "TOKENS=2*" %%a IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\TeamSpeak 3 Client" /V "" 2^>NUL') DO SET TSLOC=%%b
SET "tsloc=    (Default)    REG_SZ    C:\Program Files\TeamSpeak 3 Client"
SET "tsloc=%tsloc:*REG_SZ    =%"
ECHO +%tsloc%+

Since REG QUERY on its own returns: 由于REG QUERY自行返回:

(Default)    REG_SZ    C:\Program Files\TeamSpeak 3 Client

the above SET command will remove the unwanted data. 上面的SET命令将删除不需要的数据。 Result shown between + to demonstrate absence of spaces. 结果显示在+之间,表明没有空格。

Formula: set var=%somevar:*string1=string2% 公式:set var =%somevar:* string1 = string2%

will assign to var the value of somevar with all characters up to string1 replaced by string2 . 将分配给varsomevar所有的字符,直到达到string1替换string2 The enclosing quotes in a set command ensure that any stray trailing spaces on the line are not included in the value assigned. set命令中的封闭引号可确保该行上的任何杂散尾随空格包含在分配的值中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM