You are not logged in.
Pages: 1
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
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
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
Simon, but if the command is success it left errorlevel unchanged, because it I set the errorlevel to 0 before call RD.
Offline
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
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
Good catch, its been a long day!
fixed now
thanks again
Offline
Pages: 1