Page 1 of 1
rd/rmdir and its errorlevel
Posted: 02 Dec 2014 17:54
by npocmaka_
Just curious thing.rd/rmdir never changes the errorlevel though negative conditional execution works
Code: Select all
rmdir no_such_dir||echo %errorlevel%
Its not possible with external executable and so far this is the only command that I know with such behavior.
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 18:26
by dbenham
It does set the ERRORLEVEL upon negative conditional execution
Code: Select all
>rd notexists
The system cannot find the file specified.
>echo %errorlevel%
0
>rd notexists||rem
The system cannot find the file specified.
C:\test>echo %errorlevel%
2
A similar situation exists with failed redirection:
Code: Select all
><notExist echo ok
The system cannot find the file specified.
>echo %errorlevel%
0
><notExist echo ok ||rem
The system cannot find the file specified.
>echo %errorlevel%
1
The situation is even worse with DEL (ERASE). Not even the negative conditional fires upon error
Code: Select all
C:\test>del notExists
Could Not Find C:\test\notExists
C:\test>echo %errorlevel%
0
C:\test>del notExists||echo error
Could Not Find C:\test\notExists
C:\test>echo %errorlevel%
0
Dave Benham
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 18:44
by npocmaka_
Why rem helps to preserve the errorlevel ? And is this approach valid only for redirection , rd and del?
Btw. My main suspicious that the RD does not change the errorlevel is when invalid switch is passed (usually internal command set errorlevel to 1 in case of invalid switch without executing anything):
Code: Select all
>rd /j || echo %errorlevel%
Invalid switch - "j".
0
but it turns out it again can be printed with rem :
Code: Select all
>rd /j || rem
Invalid switch - "j".
>echo %errorlevel%
1
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 19:31
by dbenham
It doesn't matter what command is executed as part of the negative conditional, as long as it is an internal command that succeeds.
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 22:09
by Liviu
Interesting find, thanks for sharing.
Not sure that the conditional is essential. Consider the following, run at a "cmd /v" prompt. There are no conditionals, and the direct "rd /j" doesn't set the errorlevel, however running it inside a nested "cmd /c" somehow returns the error.
Code: Select all
>(echo !errorlevel!) & (rd /j) & (echo !errorlevel!)
0
Invalid switch - "j".
0
>(echo !errorlevel!) & (cmd /c rd /j) & (echo !errorlevel!)
0
Invalid switch - "j".
1
Liviu
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 22:32
by dbenham
Ooh, interesting... But I think I will stick with the conditional operator in practice
Not surprisingly, CMD /C still doesn't help with failed delete.
The DEL behavior is very unfortunate. You could argue that no one cares if a non-existent file was not deleted. But there is no ERRORLEVEL and no || execution even if the DEL fails because the file is read only. I suspect the same is true if the file is locked.
Detecting a failed delete is tricky. I swap stderr with stdout, and then use FINDSTR to detect an error message:
Code: Select all
3>&2 2>&1 1>&3 del notExist|findstr . && echo DEL failed || echo DEL succeeded
Dave Benham
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 23:16
by Liviu
You are right about "del notExist", and think I've seen it before. Yet, that looks to me like a deliberate decision (however questionable) to treat deleting a non-existing file as successful. In contrast, the "rd" behavior looks like a bug particular to "rd". In support of this theory, "del /j" sets the errorlevel correctly.
Liviu
Re: rd/rmdir and its errorlevel
Posted: 02 Dec 2014 23:32
by dbenham
Yes, I pretty much said as much with regard to deleting a non-existent file.
But no error when delete fails because file is read only or locked
... That is just wrong
I find it hard to believe that was a conscious decision.
Dave Benham