Page 1 of 1

reg query an item

Posted: 04 Oct 2013 05:56
by Adrianvdh
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?

Re: reg query an item

Posted: 04 Oct 2013 06:37
by aGerman
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

Re: reg query an item

Posted: 04 Oct 2013 07:06
by Adrianvdh
So?

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

Code: Select all

if not errorlevel 1 ...

Re: reg query an item

Posted: 04 Oct 2013 07:12
by aGerman
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

Re: reg query an item

Posted: 04 Oct 2013 08:37
by Adrianvdh
THANKS!!!, Using the "!" instead worked :), Thanks

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

Re: reg query an item

Posted: 05 Oct 2013 06:40
by aGerman
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