Reading a registry value containing spaces using a batch file

I have a batch file that reads a registry value.

However, the record I am reading contains spaces, and I only seem to grab 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 line: "C: \ Program Files (x86) \ Dir1 \ Dir2", and the batch file only fixes this: C: \ Program

Does anyone have an idea how to fix this?

thanks

+6
source share
2 answers

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

 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 ) 

An asterisk ( * ) means pass the rest of the string to the next variable (with all spaces).

+7
source
 @ECHO OFF SETLOCAL set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\My Entry" set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Space Sciences Laboratory, UC 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 do not have the key ...\My Entry as the key, I replaced the key that I have.

The first element - since the required data contains a space, and the space is the default separator, you need to use 1,2* as tokens - the first, second and remaining lines.

The next small difficulty is that the ALSO data element contains ) , which is interpreted as the closing bracket FOR/f , therefore, gives an error - in your case \Dir1\Dir2 not expected here

To overcome this, you need to quote, then divide the value assigned to home

But again, the closing bracket in home serves to complete the TRUE part of the IF statement.

the inclusion / absence of the value assigned by HOME overcomes this. Obviously, the same can be said for Valuename , although there is a hidden failure.

If the requested registry entry is not found, then SET in FOR/f not executed, so the values โ€‹โ€‹of the three variables do not change from any value that they can have before executing FOR/f . Therefore, if home (or Valuename , if you decide to use this value) has a value of someexistingvalue before FOR/f , then unexpected results may be presented.

(BTW - it is legitimate and tidier to break FOR/f in both or both brackets of the IN clause)

+4
source

Source: https://habr.com/ru/post/943885/


All Articles