rd/rmdir and its errorlevel

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
npocmaka_
Posts: 480
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

rd/rmdir and its errorlevel

#1 Post by npocmaka_ » 02 Dec 2014 17:54

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.

dbenham
Expert
Posts: 2198
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: rd/rmdir and its errorlevel

#2 Post by dbenham » 02 Dec 2014 18:26

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

npocmaka_
Posts: 480
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: rd/rmdir and its errorlevel

#3 Post by npocmaka_ » 02 Dec 2014 18:44

:shock:

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

dbenham
Expert
Posts: 2198
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: rd/rmdir and its errorlevel

#4 Post by dbenham » 02 Dec 2014 19:31

It doesn't matter what command is executed as part of the negative conditional, as long as it is an internal command that succeeds.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: rd/rmdir and its errorlevel

#5 Post by Liviu » 02 Dec 2014 22:09

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

dbenham
Expert
Posts: 2198
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: rd/rmdir and its errorlevel

#6 Post by dbenham » 02 Dec 2014 22:32

Ooh, interesting... But I think I will stick with the conditional operator in practice :wink:

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: rd/rmdir and its errorlevel

#7 Post by Liviu » 02 Dec 2014 23:16

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

dbenham
Expert
Posts: 2198
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: rd/rmdir and its errorlevel

#8 Post by dbenham » 02 Dec 2014 23:32

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 :evil: I find it hard to believe that was a conscious decision.


Dave Benham

Post Reply