reg query an item

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

reg query an item

#1 Post by Adrianvdh » 04 Oct 2013 05:56

Hi everyone...

This question has been answered so many time but I can't find the correct code.

I have this setup:

Code: Select all

reg query "%RegKey%" /v "DISCLAIMER_UI" >nul 2>nul
echo %errorlevel%
pause
if "%errorlevel%"=="0" for /f "tokens=2*" %%i in ('reg query "%RegKey%" /v "DISCLAIMER_UI"') do set "DisclaimerUI=%%j"


Which was provide by aGerman.

So I want to check if a reg item exists without it prompting that it does not exist, if it doesn't.

The errorlevel variable stays at always 0. How can I change this?

The reg query is to check if it does exist and the if statement will read the item's data if it does exist, but the errorlevel stays the same.
Therefore it throws and error if it does not exist.

How can I fix this? I can't put the >nul 2>nul on the if statement can I?

Any help?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: reg query an item

#2 Post by aGerman » 04 Oct 2013 06:37

I assume you try to expand %errorlevel% in a block of command lines (enclosed into parentheses) where you would need
- either delayed expansion and !errorlevel!
- or the old school errorlevel handling

Code: Select all

if not errorlevel 1 ...

Regards
aGerman

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: reg query an item

#3 Post by Adrianvdh » 04 Oct 2013 07:06

So?

1. Use "!" instead of "%"?
2. Use choice.exe errorlevel:

Code: Select all

if not errorlevel 1 ...

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: reg query an item

#4 Post by aGerman » 04 Oct 2013 07:12

That's exactly what I already wrote. (Unless there is no "choice.exe errorlevel". There are different possibilities to process the errorlevel as if /? would show you.)

Regards
aGerman

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: reg query an item

#5 Post by Adrianvdh » 04 Oct 2013 08:37

THANKS!!!, Using the "!" instead worked :), Thanks

P.S. Would you recommend I do this for ALL errorlevel arguments? (Use "!" instead)

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: reg query an item

#6 Post by aGerman » 05 Oct 2013 06:40

No, I wouldn't. You would need the delayed variable expansion where exclamation marks in strings could cause an issue. Use the old school errorlevel handling if you don't already need the delayed expansion for other reasons.

Regards
aGerman

Post Reply