Jump to content

Problems using %ERRORLEVEL%

Recommended Posts


I'm trying to write a batch script that checks to see if previously imported Registry settings succeeded. Here is the code I have so far:

@echo offtitle %~nx0echo.SETLOCAL enabledelayedexpansionFOR %%I IN (HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced) DO (FOR %%J IN (HideIcons SharingWizardOn) DO (FOR /F "tokens=3 delims= " %%K IN ('reg query %%I /v %%J') DO (IF !ERRORLEVEL! EQU 0 (echo successful) ELSE (echo Error)))))PAUSEEXIT

The second parameter in the second loop is there by design. I wanted the script to be able to handle missing keys. The ERRORLEVEL portion works fine when the key exists and is set to 0, allowing the IF statement to do it's thing. When it hits a non existent key, Reg Query spits out an error as you would expect but the ERRORLEVEL is not set. I know this because I placed an ECHO command between the second loop and the IF statement and it just gets ignored on the second iteration. The IF statement is not executed at all.

I've tried various different things. I've used IF ERRORLEVEL 0, IF !ERRORLEVEL! EQU 1/0 ... etc. Every combination I could think of. I've checked the reg query command on it's own and it seems to set the Errorlevels correctly outside a loop. 0 for success and 1 for a fail. Can anyone tell me what I'm doing wrong? Or atleast point me in the right direction. I've been reading for two days now and am no closer to the answer. Thanks for reading.

Here is a screenshot of the script being executed:


Link to comment
Share on other sites

Please try to expand upon your intentions because they currently appear incomplete or confused.

I would suggest that those two values would return only a 1 or a 0, if you have set them both previously to their non default value then that should be all you need to check. That said, even if they are already set to their non-default values, where would the harm be in forcing that data again?

As a side note please bear in mind that changes to this key, especially if part of an unattended setup will not generally take effect unless run at first logon or later.

Also depending upon your Operating System, different versions of reg.exe have different features.

The following example would work from Vista but not in Windows XP:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.

  • Create New...