I want to query registry for a value, if the value exists I want to modify it, if it doesn't exist I want to go to the next similar key and check.
Key:
HKCU\SOFTWARE\MySoftware\001 HKCU\SOFTWARE\MySoftware\002 HKCU\SOFTWARE\MySoftware\003
The value is the same for each key but I need to cycle through each one to check until I have checked all of them.
What I am currently doing is:
reg query "HKCU\SOFTWARE\MySoftware\001" /v MyValue
if %ERRORLEVEL%==0 (
reg add "HKCU\SOFTWARE\MySoftware\001" /v MyValue /f /t REG_DWORD /d 10
) else (
goto :KEY2
)
This will cycle through all the keys I need to check and modify each but it is quit a few lines and I am thinking perhaps I could build a subroutine of some type to accomplish this but I am kinda stumped.
The following code queries the registry key HKEY_CURRENT_USER\SOFTWARE\MySoftware
for all immediate sub-keys, filters for those whose names consist of three decimal figures, loops through them in ascending order, queries the value MyValue
for each and if found, modifies the respective data:
for /F delims^=^ eol^= %%K in ('
reg query "HKEY_CURRENT_USER\SOFTWARE\MySoftware" /K /F * ^
^| findstr "^HKEY_.*\\[0-9][0-9][0-9]$" ^| sort
') do (
reg query "%%K" /V "MyValue" && reg add "%%K" /V "MyValue" /f /t REG_DWORD /d 10
)
The used &&
operator lets the following command execute only if the preceding one succeeded.