1.

Solve : How can i check the return result of "reg query" command??

Answer»

I can list all subkeys and values of an appointed registry key with "reg query" command?. for example: reg query hklm\SOFTWARE\CryptionDirectory /s

now, i want to CHECK the return result of "reg query" command, if some subkeys exist, execute "test1.exe", if no subkeys exist, execute "test2.exe".

how to write the batch file??

thanks.Hi,

You might be able to create a for loop, but as i know very little about them, I'd use a if statement;

You'd have to check for the keys individually using the /v switch in reg query. something like;


Code: [Select]reg query /v hklm\SOFTWARE\CryptionDirectory\install
if "%errorlevel%" GTR "0" (goto fail) ELSE goto pass

echo This shoud never be seen


:fail
echo unable to find key
start test2.bat
exit

:pass
echo Key's exist ok
start test1.bat
exit


I've never used reg query before, but I'd guess that if you looked for them individually you COULD use the %errorlevel% produced. If you looked for more than one at one time, I THINK you get unpredictable results.

I might well be wrong, but I thought I'd throw in my 2 cents. 


Hope it helps.


if errorlevel 1 actually means "if errorlevel is 1 or more".

the && (success) and || (failure) operators might be useful here.

e.g.

reg query /v hklm\SOFTWARE\CryptionDirectory\install && goto yes


The subkey count is  uncertain, and i don't know the name of subkey, how can i do???Why do you want to do this?
if no subkey exists, the execute result of reg query is as following:

C:\Documents and Settings\linuxyf>reg query hklm\SOFTWARE\CryptionDirectory /s

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SOFTWARE\CryptionDirectory



if subkey  exists, the execute result of reg query is as following:


C:\Documents and Settings\linuxyf>reg query hklm\SOFTWARE\CryptionDirectory /s

! REG.EXE VERSION 3.0

HKEY_LOCAL_MACHINE\SOFTWARE\CryptionDirectory
    subkey1     REG_SZ  1
    subkey2     REG_SZ  2

I only want to know whether subkey exists or not under the "hklm\SOFTWARE\CryptionDirectory" in registry?

if exists, then execute test1.exe, or not, execute test2.exenot knowing 'reg query' all that well, you might have to output it a txt file and pull the lines that contain the reg keys names back in. Then you can say 'if the lies are blank do this...'


something like;

Code: [Select]echo off

rem outputing result of reg query to a txt file

reg query hklm\SOFTWARE\CryptionDirectory /s > C:\regOUTput.txt

rem bringing in line 4

setlocal enabledelayedexpansion
set count=0
for /f %%v in (c:\regOUTput.txt) do (
set /a count=!count!+1
if !count!==4 set /a reg=%%v
)


rem if line 3 is blank = no reg keys, run test1.bat

if "%reg%" == "" (start test1.bat) ELSE start test2.bat


This is untested and you'll have to play around with it to get it to work. I'd SUGGEST running the first line (outputting to a txt file) from a command prompt and then opening the file to see the results. Count the number of lines down and note the line number that the first result is on. (from what you've posted, I'd say line 4, but better to check)

You'll have to sub that number for the number 4 that I've used in the code above.

Let us know how you get on.
becase the subkey names are full path, so i meet requirement with following code:


echo off
cls

setlocal enabledelayedexpansion

for /f "tokens=2 delims=:" %%i in ('reg query hklm\SOFTWARE\CryptionDirectory /s
') do goto success 

echo no subkey!
pause>nul

:success
echo find key ok
pause>nul



Discussion

No Comment Found