Solved

Check the data of Registry key in batch script

Posted on 2011-03-15
5
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

751 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