Solved

Check the data of Registry key in batch script

Posted on 2011-03-15
5
1,127 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

615 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