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 call
ing 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.