Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do MS-DOS built-in commands return an error\exit code?

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.

like image 383
Duat Le Avatar asked Jun 28 '11 00:06

Duat Le


People also ask

What is exit code in command?

When you execute a command or run a script, you receive an exit code. An exit code is a system response that reports success, an error, or another condition that provides a clue about what caused an unexpected result from your command or script.

How do I check for errors using CMD?

To find more details on many of these meaningless Windows error codes, open a Windows command prompt and type “net helpmsg xxx” where 'xxx' is the error code in question.

How do you exit a command prompt?

To close an interactive command prompt, the keyboard shortcut ALT + F4 is an alternative to typing EXIT.

How do I find Windows exit code?

Every command or application returns an exit status, also known as a return status or exit code. A successful command or application returns a 0 , while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.


1 Answers

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.

like image 192
paxdiablo Avatar answered Oct 21 '22 19:10

paxdiablo