Read registry value that contains spaces using batch file

11,128

Solution 1

This problem happens because the string contains spaces and the second part of the string (possibly more parts if there are more spaces) are treated as another token (in 4, 5, etc.) To fix this, pass the rest of the line to %%C with an asterisk like this:

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set ValueName=%%A
    set ValueType=%%B
    set Home=%%C
)

The asterisk (*) means to pass the rest of the line into the next variable (with all the spaces).

Solution 2

@ECHO OFF
SETLOCAL
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\My Entry"
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Space Sciences Laboratory, U.C. Berkeley\BOINC Setup"
set VALUE_NAME=migrationdir

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (
 `REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    set "ValueName=%%A"
    set "ValueType=%%B"
    set Home="%%C"
)

IF DEFINED home SET home=%home:"=%
if defined Home echo Home = %Home%
if NOT defined Home echo %KEY_NAME%\%VALUE_NAME% not found.

Since I don't have ...\My Entry as a key, I've substituted a key I do have.

First item - since the data required contains a space, and space is a default delimiter, you need to use 1,2* as tokens - the first, second and remainder-of-line.

The next little difficulty is that the data item ALSO contain ) whic is interpreted as the closing parenthesis of the FOR/f hence giving an error - in your case, \Dir1\Dir2 not expected here

A way to overcome this is to quote, then strip the value assigned to home

But once again, the closing-parenthesis in home serves to terminate the TRUE portion of the IF statement.

switching on the presence/absence of a value assigned to HOME overcomes this. Obviously, the same could be said for Valuename although there's a hidden fail-to-fail here.

If the requested registry entry is not found, then the SETs in the FOR/f are not performed, hence the values of the three variables are not changed from any value they may have before the FOR/f is executed. Therefore, if home (or valuename if you choose to use that value) has a value of someexistingvalue before the for/f then unexpected results may be presented.

(BTW - it's legitimate and neater-looking to break the for/f at either or both of the parentheses of the IN clause)

Share:
11,128

Related videos on Youtube

Yos
Author by

Yos

Updated on September 14, 2022

Comments

  • Yos
    Yos over 1 year

    I have a batch file that reads the registry value.

    However the entry that I am reading contains spaces and I only seem to capture everything before the first space character when setting my variable.

    set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\My Entry"
    set VALUE_NAME=Home
    
    FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
        set ValueName=%%A
        set ValueType=%%B
        set Home=%%C
    )
    
    if defined ValueName (
        @echo Home = %Home%
    ) else (
        @echo %KEY_NAME%\%VALUE_NAME% not found.
    )
    

    The home registry entry actually contains this string: "C:\Program Files (x86)\Dir1\Dir2" and the batch file only captures this: C:\Program

    Does anybody have an idea of how to fix this?

    Thanks

  • Yos
    Yos almost 11 years
    Wow what a great and in-depth answer! I am using the solution as described by user2033427 and it seems to work great. However you have given me some stuff to think about and good to have an explanation. The trick was adding the * to the tokens command. Thanks guys!
  • Yos
    Yos almost 11 years
    Hi, after playing around with both examples it seems that this one allowed me to check whether the home variable was assigned, the solution above was throwing an error as you pointed out when trying to check whether home was defined. Thanks I have used this code now as it seems to work great. So did the above apart from what I have just mentioned! :-)