8

I have not found a way to get the error code, if returned, of rmdir. It seems like the MS-DOS internal commands do not return an error code. Can someone confirm that?

How does a script that uses these commands know if the commands succeed or fail in order to decide the next step? The easiest way is to read their return code, if it is returned.

Thanks in advance.

Duat Le
  • 3,576
  • 2
  • 17
  • 18
  • I had a comment here stating DOS commands *do* return error codes, but I see now from the answers and my own testing that it's not an always thing for all commands (and I suppose that may relate to many "DOS commands" actually just calling external executables in the %PATH%, which executables apparently may differ in properly returning error codes. I wouldn't put it past cmd internal commands to be inconsistent with error codes, either, though ;) – Alex Hall Feb 07 '16 at 19:31
  • Another example of MS doing lousy work. Any properly written tool gives useful exit codes, but no ... not Microsoft; they're just too cool for that. >8^( – antred Dec 20 '16 at 12:35

4 Answers4

16

No, it appears not to. If you echo %errorlevel% after either a successful or failed rmdir, you get 0 in both cases:

c:\pax> mkdir qqq
c:\pax> rmdir qqq
c:\pax> echo %errorlevel%
0
c:\pax> rmdir qqq
The system cannot find the file specified.
c:\pax> echo %errorlevel%
0

For that particular use case, you're probably best off checking the directory existence afterwards:

if exist dodgy\. rmdir dodgy
if exist dodgy\. echo dodgy directory still exists

Interestingly enough, if you call on a separate copy of cmd.exe to perfom the operation, you can get the error level:

c:\pax> mkdir qqq
c:\pax> cmd /c rmdir qqq
c:\pax> echo %errorlevel%
0
c:\pax> cmd /c rmdir qqq
The system cannot find the file specified.
c:\pax> echo %errorlevel%
2

However, I'm unconvinced that's better than simply checking that the directory is gone after you remove it, since it requires you to start up a whole new command interpreter.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • 3
    If `rmdir dodgy` fails, but there is a file named 'dodgy', then you're in trouble :) To test the existence of a directory use: `if exist "dodgy\."` – huysentruitw Apr 15 '13 at 07:31
  • @Wouter, good advice, changed to suit, though probably well outside any agreed SLAs :-) – paxdiablo Nov 24 '14 at 02:29
  • @paxdiablo they do return exit codes, it's just the errorlevel which is not set for some reason, but if you go through cmd /c you can actually get the codes – illegal-immigrant Feb 04 '16 at 20:49
5
md test
2>nul rmdir test&&echo ok||echo err
2>nul rmdir test&&echo ok||echo err

This prints ok for the first rmdir and err for the second.

rmdir is an internal cmd.exe command so %errorlevel% is probably not updated.

Anders
  • 83,372
  • 11
  • 96
  • 148
4

rmdir returned 0 when either succeeded or failed. It seems intuitive that it should return an error code. However, other internal commands does (at least mkdir and dir commands I've tested).

tcf
  • 51
  • 3
0

They do, it's just hard to find the docs for specific commands, but here's the proof that rmdir (which according to MS docs here does not return exit codes) actually sets exit code:

$process = Start-Process -FilePath "cmd" -ArgumentList "/c rmdir /q /s C:\folder\unexistingfolder" -NoNewWindow -PassThru -Wait
$process.ExitCode

the output will be 2 if the directory does not exist, there are other codes for permission issues, etc.

illegal-immigrant
  • 7,648
  • 7
  • 46
  • 78
  • Is this Powershell script? That'd be different from DOS (per the question). – Alex Hall Feb 07 '16 at 19:32
  • powershell is just used to start cmd and read cmd's exit code to prove it's propagated by the 'rmdir' command. you can use any other language to do the same, the statement remains same - dos commands do return exit codes – illegal-immigrant Feb 08 '16 at 18:46