Question

I am trying to return the install location of a program using REG QUERY inside a FOR loop. 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:

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

But using the delimiter REG_SZ returns:

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. If I change my token # to 3 it returns:

peak 3 Client

So somehow it must be interpreting " s" as my delimiter, right?

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?

Was it helpful?

Solution

If REG QUERY returns the string below (spaces included):

    (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

OTHER TIPS

DELIMS is a set of characters, not a single string. Therefore any character out of R,E,G,_,S, or Z will act as a delimiter.

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:

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

the above SET command will remove the unwanted data. Result shown between + to demonstrate absence of spaces.

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

will assign to var the value of somevar with all characters up to string1 replaced by string2. The enclosing quotes in a set command ensure that any stray trailing spaces on the line are not included in the value assigned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top