14

I'm struggling to read the value of a registry key into a variable. The registry value may, or may not, contain spaces. In this case I'm trying to look up SDK paths.

It's easy to get the value with reg query, but it's returned in a remarkably unhelpful format:

C:\Users\Administrator\Desktop>reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools
    InstallationFolder    REG_SZ    C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\

The key name, key type, and key value are separated by a series of spaces, not tabs.

You'd think you could use something like:

FOR /F "usebackq tokens=3* skip=2" %%L IN (
    `reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder`
) DO SET sdkpath=%%L

... but if the key contains spaces, as in this case, the result emitted on the command line is:

C:\Users\Administrator\Desktop>SET sdkpath=C:\Program

Which isn't helpful. There doesn't seem to be a wildcard variable to say "All matches". And you can't just write:

DO SET sdkpath=%%L %%M

... because if there are not spaces in the path, that will produce a literal %M (and would also produce a trailing space).

So. Is there any way to do this simple thing in a batch file?

I've written most of my tooling in Powershell (and some Perl), but I need to use a batch file for the "simple" task of calling the visual studio / windows sdk environment script and then invoking the code in sane languages once the environment is set up.

You'd think that after 10+ years of cmd.exe's existence, and command.com before it, this would be easy. Help?

(I can use Perl and the Win32:: packages to query the registry, but it doesn't help me get it into a batch var...)

Using Win2k12.

I've read:

and many others, but none handle the general case of correctly reading a value from a key without knowing whether or not it contains spaces / how many.

2 Answers 2

16

Ah this is one of the annoying details about the for /f command. In order to read all the characters including the spaces with the * the previous token needs to be declared. tokens=2,* The functional reason is stated in the documentation.

If the last character in the tokens= string is an asterisk (*), an additional variable is allocated and receives the remaining text on the line after the last token that is parsed.

FOR Documentation

FOR /F "usebackq tokens=2,* skip=2" %%L IN (
    `reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1\WinSDKTools" /v InstallationFolder`
) DO SET sdkpath=%%M
2
  • 6
    Wow. Now I hate batch files more than ever. But thankyou SO much. Mar 12, 2014 at 13:54
  • If you're like me and have reading comprehension issues, note that %%L automatically becomes %%M.
    – Jason
    Dec 21, 2018 at 20:06
0

Use Quotation marks

SET "sdkpath=%%M"

instead SET sdkpath=%%M

This will avoid problems with spaces.

2
  • This isn't an answer to the question, it should be a comment to David Ruhmann's answer
    – jeb
    Nov 19, 2019 at 22:01
  • Yes u are right, but i can't comment his post. (<50 reputation).
    – Chris
    Nov 20, 2019 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.