Solved

Check the data of Registry key in batch script

Posted on 2011-03-15
5
1,068 Views
Last Modified: 2012-06-27
Hi,

I am writing a small batch script that looks to the registry on a client and checks to see if the data of a registry key matches a single string. The reason for this is because the key in question has been duplicated in many instances and I would like to call a whole heap of things in the batch script if the key matches the duplicated string. I have written the following to ensure that I have this working correctly:

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate" /v SusClientId | FIND /I  "Test_String"
IF NOT ERRORLEVEL 0 GOTO :FAIL
ECHO THIS KEY EXISTS!
GOTO :SUCCESS_1
:FAIL
ECHO THIS KEY DOES NOT EXIST!
:SUCCESS_1
PAUSE

Now "Test_String" is not the data of SusClientId, so I guess it should return an ErrorLevel of 1 (or something not 0) and echo "THIS KEY DOES NOT EXIST!". But in fact it doesn't, it echos THIS KEY EXISTS!. I guess I have something wrong with FIND?

Any help would be appreciated. Note that I am not a batch wizard.
0
Comment
Question by:agbnielsen
5 Comments
 
LVL 14

Accepted Solution

by:
sentner earned 500 total points
ID: 35144585
Your problem is in your errorlevel check.  the "IF ERRORLEVEL <N>" check will look for errorlevels of N or higher, so checking for errorlevel 0 checks for ALL errorlevels.

Just change the second line to:

IF ERRORLEVEL 1 GOTO :FAIL
0
 
LVL 14

Expert Comment

by:sentner
ID: 35144588
Some more details on that particular check:  http://support.microsoft.com/kb/69576
0
 
LVL 3

Expert Comment

by:OrenRozen
ID: 35144614
try this in the script

@echo off

for /F "tokens=3" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate" /v SusClientId') DO (
      if "%%a"=="TheScript" (echo yes) else (echo no)
)
0
 

Author Closing Comment

by:agbnielsen
ID: 35144619
thanks
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35145053
I know you have your answer, but for future you might also like to know of && and || as shortcuts to using if errorlevel:

&& runs a command if the previous command is success, || if it fails, so, e.g.

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate" /v SusClientId | FIND /I  "Test_String" && goto success
  echo The key was not found
  pause
goto :eof

:success
  echo The key was found
  pause
goto :eof
pause

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now