question about reg query command

  • Thread starter Thread starter Adam Sandler
  • Start date Start date
A

Adam Sandler

Hello,

I'm trying to use reg.exe to check to make sure some registry key
values are correct. I want to do this all from a batch file. Let's
say I have the hypothetical registry key of HKLM\SOFTWARE\System Admin
Scripting Guide\Test and Test's value is 5. I want the batch file to
compare the key's actual value

My first thought was I'd have to get what is returned by REG QUERY
into a variable so a comparison could be performed against it...

IF %RETURNVALUE% EQU 5 (
echo Passed!
) ELSE (
echo Failed!
)

When I enter
reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" I get this
back:

REG_SZ Test 5

So at least I know I'm using reg.exe correctly. Here's where I get
stuck... how do I get what's returned by REG QUERY into a variable???
I've tried:

set RETURNVALUE=reg query "HKLM\SOFTWARE\System Admin Scripting
Guide\Test"

and this:

reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" >
%RETURNVALUE%

Both examples fail miserably. Any help is greatly appreciated -
thanks!
 
Adam said:
Hello,

I'm trying to use reg.exe to check to make sure some registry key
values are correct. I want to do this all from a batch file. Let's
say I have the hypothetical registry key of HKLM\SOFTWARE\System Admin
Scripting Guide\Test and Test's value is 5. I want the batch file to
compare the key's actual value

My first thought was I'd have to get what is returned by REG QUERY
into a variable so a comparison could be performed against it...

IF %RETURNVALUE% EQU 5 (
echo Passed!
) ELSE (
echo Failed!
)

When I enter
reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" I get this
back:

REG_SZ Test 5

So at least I know I'm using reg.exe correctly. Here's where I get
stuck... how do I get what's returned by REG QUERY into a variable???
I've tried:

set RETURNVALUE=reg query "HKLM\SOFTWARE\System Admin Scripting
Guide\Test"

and this:

reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" >
%RETURNVALUE%

Both examples fail miserably. Any help is greatly appreciated -
thanks!

Try something like

for /f "tokens=3" %%a in (
'reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test"'
) do set RETURNVALUE=%%a
 
In said:
Hello,

I'm trying to use reg.exe to check to make sure some registry
key values are correct. I want to do this all from a batch
[ ]
When I enter
reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" I
get this back:

REG_SZ Test 5

So at least I know I'm using reg.exe correctly. Here's where I
get stuck... how do I get what's returned by REG QUERY into a
variable???
I've tried:

set RETURNVALUE=reg query "HKLM\SOFTWARE\System Admin Scripting
Guide\Test"

and this:

reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" >
%RETURNVALUE%

Both examples fail miserably. Any help is greatly appreciated -
thanks!

If you don't actually need to use an environment variable, but
simply need to confirm the valuename and data is present, perhaps
a different approach will be suitable.

======= screen cap =========
C:\TEMP>reg query "HKEY_CURRENT_USER\_TEST" /v test

! REG.EXE VERSION 2.0

HKEY_CURRENT_USER\_TEST
test REG_SZ 5


C:\TEMP>reg query "HKEY_CURRENT_USER\_TEST" /v test | find /i "test" | find "5"
test REG_SZ 5

C:\TEMP>echo %errorlevel%
0

C:\TEMP>reg query "HKEY_CURRENT_USER\_TEST" /v test | find /i "test" | find "4"

C:\TEMP>echo %errorlevel%
1

C:\TEMP>reg query "HKEY_CURRENT_USER\_TEST" /v TOAST | find /i "test" | find "5"

Error: The system was unable to find the specified registry key or value

C:\TEMP>echo %errorlevel%
1
======= end screen cap =========

Thus a simple errorlevel test can confirm expected data is present.

(reg.exe v2.0, W2K)
 
Adam said:
I'm trying to use reg.exe to check to make sure some registry key
values are correct. I want to do this all from a batch file. Let's
say I have the hypothetical registry key of HKLM\SOFTWARE\System Admin
Scripting Guide\Test and Test's value is 5. I want the batch file to
compare the key's actual value
Hi Adam,

the definitions of hive, rootkey, subtree, key, subkey, entry, value,
data and type seem to be a bit ambigous.
When I use reg query I usually use the nomenclature of the help
"reg query /?" (and there are subtle version/language discrepancys)

In general a combination of for and find/findstr are best to parse. I
looked through my set of reg query examples and found a lot of
different uses.
Depending on the values you are after:
- single values (ev. with multi word entry names)
- the standard entry listed with (NO NAME)
- enumerations or
- sets of entries
different approaches are required.
My first thought was I'd have to get what is returned by REG QUERY
into a variable so a comparison could be performed against it...

IF %RETURNVALUE% EQU 5 (
echo Passed!
) ELSE (
echo Failed!
)

When I enter
reg query "HKLM\SOFTWARE\System Admin Scripting Guide\Test" I get this
back:

REG_SZ Test 5
Is that right? Or really
Test REG_SZ 5
or
(NO NAME) REG_SZ Test 5

That is important to specifiy the correct tokens parameter in the for.

You may take a look at some reg query uses I have posted:
http://groups.google.com/groups?q=author:matthias.tacke "reg.query"

HTH
 
Phil,

Thanks for your help... your suggestion is EXACTLY what I was trying to
do. At any rate, I've never really done looping with dos-like syntax
before (I usually use Perl or VBscript if I'm tryingsuch things). What
does the "tokens=3" %%a mean?

Thanks!
 
Adam said:
Phil,

Thanks for your help... your suggestion is EXACTLY what I was trying to
do. At any rate, I've never really done looping with dos-like syntax
before (I usually use Perl or VBscript if I'm tryingsuch things). What
does the "tokens=3" %%a mean?

Thanks!

The result of 'reg query' contains three tokens:

REG_SZ Test 5

So the '"tokens=3" %%a' part of the 'for /f' command basically says
'assign the third token of the result to metavariable %%a'; and this
value of %%a is subsequently assigned to the environment variable
RETURNVALUE.
 
Back
Top