We help IT Professionals succeed at work.

Return code 0 on error

kwatt562
kwatt562 asked
on
Hi running a bat script to delete some registry keys
I don't want the script to fail if it cant find a registry value

reg delete HKCR\Installer\Products\B074548141BECBA438B53EC696D30CD8 /f
I need it to return error value of 0 if a key isn't  found, can someone assist?
Comment
Watch Question

"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
If that is the last command in the batch, just use

exit /b 0

as last statement to exit with a return value of 0.

Commented:
and if it is not the last line in the batch you can simply use labels, goto's and if statements; e.g. -
reg delete HKCR\Installer\Products\B074548141BECBA438B53EC696D30CD8 /f
if %ERRORLEVEL% EQU 1 goto _Section1
:: Do some more stuff because the entry was removed.
echo Doing more stuff, yay...
:_Section1
reg delete HKCR\Installer\Products\<NEXTProductID> /f
if %ERRORLEVEL% EQU 1 goto _Section2
:: Do some more stuff because the entry was removed.
echo Doing more stuff, yay...
:_Section2
...
...
...
:_eof
exit /b 0

Open in new window

-saige-
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
There are only a few situations where you should use labels. In general - no!