0

Say there exists a location in the registry:

HKEY_CURRENT_USER\Software\ABC\XYZ\

In this location there are multiple values present, no fixed number. But the value names are having same prefix like ABC0, ABC1, ABC2, ...

Generally I would use this query to fetch each individual value:

FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKEY_CURRENT_USER\Software\ABC\XYZ\" /v ABC0`) DO (
    SET Value=%%A
)

This would set the value of ABC0 to the variable Value.

But I don't know the number of values that might exist.

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?

Something like:

Value1 = Value_of_ABC0
Value2 = Value_of_ABC1
and so on.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Lastwish
  • 317
  • 10
  • 21
  • Thanks @Mofi, when i try to print the variable out of local why doesnt it resolve ? And if the value of ABC are IPs, how do i use newline command between two IPs i.e. '\n'. In the single variable approach, it resolves to 1.1.1.1;2.2.2.2 – Lastwish Mar 20 '15 at 09:20
  • See for example [Multiline text file, how to put into an environment variable](http://stackoverflow.com/questions/10607739/) where you can see how to define an environment variable with line-feed as string value which can be referenced instead of semi-colon to get the IP addresses into the variable separated by line endings. – Mofi Mar 20 '15 at 11:18
  • `setlocal` is used usually at beginning of a batch file and `endlocal` at end of the batch file if not omitted at all. This avoids the need to pass the value of a local variable in new environment variable list created by `setlocal` to the previous environment variable list restored on `endlocal`. – Mofi Mar 20 '15 at 11:38

1 Answers1

1

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.

  1. for /?
  2. if /?
  3. reg query /?
  4. 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.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Good answer, but you actually don't need to verify the first characters of the value name. Adding `/v ABC*` at the end of command will filter *ABC* starting names only. Like this: `...in ('reg query "HKCU\Software\ABC\XYZ"' /v ABC*)`. But you have to skip the last line that shows the search result. – Rafael Mar 19 '15 at 07:01
  • When i use the reg query with ABC* in CMD directly it works, but when i try to store the value in a variable using the FOR command by a script, i just get `Search:` – Lastwish Mar 19 '15 at 07:15