简体   繁体   中英

Batch file to query registry

I'm trying to get my batch script to check if two registry keys exist and if they do, goto...

So far, my code works but just need it to check if both keys exist as well as if one or the other exists as there may be cases when HKCU\\SOFTWARE\\MICROSOFT\\OFFICE\\14.0 and HKCU\\SOFTWARE\\MICROSOFT\\OFFICE\\15.0 both exist.

I need it to execute :O2010 and :O2013 if both keys exist.

At the moment, if both exist then it just stops after it runs :O2010

@ECHO OFF

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\14.0 > NUL
IF NOT ERRORLEVEL 1 GOTO :O2010

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\15.0 > NUL
IF NOT ERRORLEVEL 1 GOTO :O2013

GOTO :END

:O2010
reg import \\path_to_reg_file\regkey1.reg
GOTO :END

:O2013
reg import \\path_to_reg_file\regkey2.reg
GOTO :END

:END

Any help would be much appreciated.

Do you mean like this?:

@ECHO OFF

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\14.0 > NUL
IF NOT ERRORLEVEL 1 reg import \\path_to_reg_file\regkey1.reg

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\15.0 > NUL
IF NOT ERRORLEVEL 1 reg import \\path_to_reg_file\regkey2.reg

Something like this should do it:

@ECHO OFF

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\12.0 > NUL
IF NOT ERRORLEVEL 1 set f1=1

REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\15.0 > NUL
IF NOT ERRORLEVEL 1 set f2=2

set /a f3=f1+f2
if %f3%==1 (echo reg import \\path_to_reg_file\regkey1.reg)
if %f3%==2 (echo reg import \\path_to_reg_file\regkey2.reg)
if %f3%==3 (echo reg import \\path_to_reg_file\regkey1.reg & echo reg import \\path_to_reg_file\regkey2.reg)

This will import the reg keys as below:

A) if either key exists it will import the associated reg key
B) if both keys exist it will import both keys

@ECHO OFF
REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\14.0 > NUL && reg import \\path_to_reg_file\regkey1.reg
REG QUERY HKCU\SOFTWARE\MICROSOFT\OFFICE\15.0 > NUL && reg import \\path_to_reg_file\regkey2.reg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM