Solved

Check the data of Registry key in batch script

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

Suggested Solutions

Title # Comments Views Activity
Batch file or script with password 22 91
VB.net and sql server 4 54
Formula for calculating ROI on training 6 69
Visual Studio hangs on running project 6 60
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

749 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