Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a registry value to a batch variable, handling spaces in value

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:

  • Read registry value that contains spaces using batch file
  • Shell script reading windows registry

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.

like image 238
Craig Ringer Avatar asked Mar 12 '14 13:03

Craig Ringer


People also ask

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.

What is the value in registry?

A registry value can store data in various formats. When you store data under a registry value, for instance by calling the RegSetValueEx function, you can specify one of the following values to indicate the type of data being stored.


1 Answers

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
like image 60
David Ruhmann Avatar answered Sep 18 '22 15:09

David Ruhmann