You are not logged in.

#1 13 Apr 2015 07:32

rycho
Member
Registered: 13 Apr 2015
Posts: 3

RD does NOT return errorlevel 0

There is an information on the man page for rd (ss64.com/nt/rd.html) that "RD will return an Errorlevel of 0, irrespective if it succeeds or fails for any reason". This is not exactly true. Actually RD will leave the errorlevel information intact whatever it was before executing the command. I've found this when I was using a combination of robocopy and rd where errorlevels of robocopy below 4 are warnings for me and the program can continue. Check the combination of batch scripts:

b.bat:

@echo off
setlocal

if not exist ccc mkdir ccc

exit /b 4

c.bat:

@echo off
setlocal

call b.bat
if errorlevel 8 goto :error

rd /q /s ccc
if errorlevel 1 goto :error

goto :eof

:error
echo error %errorlevel%

How to check if the rd operation was successfull? Check if the dir still exists after the command or see this post: stackoverflow.com/questions/11137702/batch-exit-code-for-rd-is-0-on-error-as-well.
Also read here: https://devblogs.microsoft.com/oldnewth … 0/?p=20743

Offline

#2 13 Apr 2015 14:53

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: RD does NOT return errorlevel 0

Is strange, but based on my test and reading the post that you link, I can write this:

RD left errorlevel untouched on success and also on error, but you can set the errorlevel on error if you uses the || operator. But for catch success set errorlevel to 0 before.
Then for check the success you need set errorlevel to 0:
Set "ErrorLevel="
Cmd /C Exit /B 0
RD Folder || Rem
Echo(%ErrorLevel%

About the difference between ErrorLevel and %ErrorLevel%, is a variable that is expanded to the expected value if you are not set a value to it, like Random:
For example:

Set "Random=foo"
Echo %Random%
Set "Random="
Echo %Random%

Then, the difference between %ErrorLevel% and ErrorLevel is none if never set a variable called ErrorLevel.



With that

Offline

#3 13 Apr 2015 18:47

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: RD does NOT return errorlevel 0

Interesting discovery, I have added some notes and a link to the stackoverflow thread to the RD page http://ss64.com/nt/rd.html

Offline

#4 13 Apr 2015 20:01

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: RD does NOT return errorlevel 0

Simon, but if  the command is success it left errorlevel unchanged, because it I set the errorlevel to 0 before call RD.

Offline

#5 14 Apr 2015 20:29

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: RD does NOT return errorlevel 0

carlos wrote:

Simon, but if  the command is success it left errorlevel unchanged, because it I set the errorlevel to 0 before call RD.

Ah I see what you mean now, if the error is already non zero the workaround won't fix it - I have updated the page again to reflect this now

Thanks

Offline

#6 14 Apr 2015 20:40

carlos
Member
From: Chile
Registered: 04 Nov 2008
Posts: 251
Website

Re: RD does NOT return errorlevel 0

Simon, in my windows 7 sp1 color 00 set errorlevel to 1, not 0.

C:\Windows\System32>cmd /c exit /b 5

C:\Windows\System32>echo %errorlevel%
5

C:\Windows\System32>color 00

C:\Windows\System32>echo %errorlevel%
1

Edit. I found that the command

cd .

set the errorlevel to 0.

Last edited by carlos (14 Apr 2015 21:46)

Offline

#7 15 Apr 2015 00:11

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: RD does NOT return errorlevel 0

Good catch, its been a long day!
fixed now

thanks again

Offline

Board footer

Powered by