Read registry value that contains spaces using batch file
batch-file, registry
Solution
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).
Problem
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