Is there any way to obtain all the values with suffix ABC (in the above location) and store them into a variable or store them in multiple variables?
This question can be answered with:
Yes, there is a way to get all the values and store them in one or multiple variables.
But most likely you are also interested in what is the code for this task.
Batch file to store each value in a separate environment variable.
@echo off
setlocal EnableDelayedExpansion
set "Count=1"
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ"') do (
set "ValueName=%%A"
if /I "!ValueName:~0,3!"=="ABC" (
set "Value!Count!=%%C"
set /A Count+=1
)
)
endlocal
Batch file to store all values in one environment variable:
@echo off
setlocal EnableDelayedExpansion
set "Value="
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ"') do (
set "ValueName=%%A"
if /I "!ValueName:~0,3!"=="ABC" (
if not "!Value!"=="" set "Value=!Value!;"
set "Value=!Value!%%C"
)
)
endlocal
To understand the batch code, open a command prompt window, execute the following commands, and read help output for each command.
for /?
if /?
reg query /?
set /?
Addition in reference to comment by Rafael.
reg.exe
of Windows 2000 and Windows XP does not support wildcards on variable names. Wildcards in variable names are supported only by reg.exe
since Windows Vista.
The output of reg.exe
depends on version and used command.
Output on Windows XP using the command
%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ"
is
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Software\ABC\XYZ
ABC0 REG_SZ Hello!
ABC1 REG_SZ Okay. Let's go.
ABC2 REG_SZ Finished.
The output starts with a blank line. Next line is a version information. Third line is again a blank line. Fourth line is the registry key. Then the lines with the values follow with 4 spaces as indentation and tabs between value name, type of value and value string.
Output on Windows 7 using the command
%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ"
is
HKEY_CURRENT_USER\Software\ABC\XYZ
ABC0 REG_SZ Hello!
ABC1 REG_SZ Okay. Let's go.
ABC2 REG_SZ Finished.
The first line is again a blank line. There is no version information. Therefore second line contains already the registry key which of course could contain also 1 or more spaces. Then the lines with the values follow with 4 spaces as indentation and also 4 spaces between value name, type of value and value string.
Output on Windows 7 using the command
%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ" /v ABC*
is
HKEY_CURRENT_USER\Software\ABC\XYZ
ABC0 REG_SZ Hello!
ABC1 REG_SZ Okay. Let's go.
ABC2 REG_SZ Finished.
End of search: 3 match(es) found.
In comparison to previous output there is now one more blank line and a summary information which contains also several spaces.
Conclusion:
It is safe to skip the first 2 lines of output of reg.exe
as done now in updated code.
But it is not possible on Windows Vista, Windows 7, Windows 8 or Windows 8.1 to use a simplified code like below because of the search summary line.
@echo off
setlocal EnableDelayedExpansion
set "Value="
for /F "skip=2 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\ABC\XYZ" /v ABC* ') do (
if not "!Value!"=="" set "Value=!Value!;"
set "Value=!Value!%%B"
)
endlocal
However, thanks to Rafael nevertheless as his comment made me looking on differences on output between versions of reg.exe
, analyzing them and documenting it here which will be definitely helpful for me and hopefully also for others in future.