How do I check for an existing Registry value in a DOS Batch file?

missymadi
missymadi used Ask the Experts™
on
Experts,

       How do I check to see if there is an existing key in the registry file on an XP machine?
 I'm not sure how to make the batch file look at this key. This install is a little different, where usually there is a {;lkkj9uwje-lkajf;oi-lkj} number it is a directory iscsi200.
EX:  HKLM\software\microsoft\windows\currentversion\uninstall\iscsi200



right now I have
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
You should be able to use the REG command in a batch script.  Depending on the exact contents of that registry entry you may want to look for a particular value, or just make sure something exists.  To get a feel for the command you can do REG /? at a command prompt.  Also try the following from a command promt:

REG QUERY HKLM\software\microsoft\windows\currentversion\uninstall\iscsi200

In a batch file you could just look for the ERROR: text that it returns if the key doesn't exist.  So maybe something like this:

REG QUERY HKLM\software\microsoft\windows\currentversion\uninstall\iscsi200 | find "ERROR:">NUL
if errorlevel 1 echo Registry Entry Missing

~bp

Author

Commented:
I have used this command in the past but for some reason this batch is not running correctly. Please see attached file.
I also get the following error everytime I run the batch ERROR: The system was unable to find the specified registery key or value.

Thanks, Missymadi
@Echo off
::      ISCSI200.bat
::	Installs Diag Software if not already installed. 
::      Writes ISCSI200.bat activity to a file C:\ISCSI200.out. 
::      
 
::            
@Echo Off
 
::  Set Parent Registry Key
Set PTH=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

 
::  Check if Product is Installed
::  Echo Checking to see if ELT/1500 is installed..
Reg Query "%PTH%{iscsi200}" 


::Handle ErrorLevels
If ErrorLevel  1 Goto Install
If ErrorLevel  0 Goto Exit

:Install
Echo Installing ISCSI200...
ALLUSERS=1
Goto END1

:Exit
Echo %date%,%time%, ELT/1500 Software is already installed! >> C:\ISCSI\Initiator-2.08-build3825-x86fre.exe 

Goto END2

:END1
ECHO %date%,%time%,ELT/1500 Software is now installed on your system!  >> C:\ISCSI200.out 

:END2

Pause

Open in new window

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Since this value in the registry is not a GUID, it should not be enclosed in curly braces.  So I think you need the following form for your query statement.

Reg Query "%PTH%iscsi200"

As a test just do the following and it will list all the nodes in the uninstall branch and you can see the format.

Reg Query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

~bp
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
It is listed as
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\iscsi200. I tried removing the curly brackets but now I get back the registry key and the script is not running.

Author

Commented:
to be clear,
When I run the batch file and ISCSI is not installed, the batch file installs ISCSI but it does not display the END1. The END1 tag never runs.
When ISCSI is installed only the registry key for ISCSI is returned. The Exit tag is never run.  
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
So it sounds like the ERRORLEVEL check may be working okay.  If you want to suppress the display of the keys below that node change the query to look like:

Reg Query "%PTH%iscsi200" >NUL 2>NUL

Looking at the script you posted I hav a few questions:

==> ALLUSERS=1

What is that, it doesn't look like a program or command name that would execute?

==> Echo %date%,%time%, ELT/1500 Software is already installed! >> C:\ISCSI\Initiator-2.08-build3825-x86fre.exe  

I don't understand why you are redirecting your log info to an EXE program?  Is this the installer, if so doesn't it belong in the "Install routine?

As far as what branches your code is taking, I'd recommend you change the ECHO OFF to an ECHO ON for some testing, it will show you all the statements executed when the script runs.

~bp

Author

Commented:
ALLUSERS=1  means to allow any user to run the software, not just the installer. We had a problem here that standard users who did not install the product did not have rights to use the software. - this will give them rights.

I don't understand why you are redirecting your log info to an EXE program?  Is this the installer, if so doesn't it belong in the "Install routine? --- I shouldn't use copy and paste :)

Fixed the Echo's. Everything working now. THANKS!
Test your restores, not your backups...
Top Expert 2016
Commented:
Great, glad to hear that resolved it.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial