The DOS CD Command (or how to send stderr to nul)

Good old DOS.

Don’t you hate getting this message: The system cannot find the path specified. What path?

It’s usually the result of a failed CD command. But what if it’s inside a batch file? Hopeless. Wouldn’t it be better instead to have a message like: Sub-Directory “hhh” not found.

It’s easy to do in a batch file. We just have to figure a way to nullify that DOS error message.

Let’s try “cd xxx > nul”. That should do it.

What? That fails?

The reason that the error message still appears is that it is not written to Standard Output (stdout), but to Standard Error (stderr). We have to send the 2nd output to nul as well. Here’s how to do it:

cd xxx > nul 2 > nul

or cd xxx > nul 2>&1

The first sends outputs 1 and 2 to nul. The second sends stderr (output 2) to the same place as stdout (output 1). Very Unix/Linux – like syntax here.

The next thing to do is to test the success of the CD command. Luckily DOS sets the errorlevel to 0 for success and 1 (or higher) for failure.

In the DOS batch file, below, we are trying to go to a subdirectory in some deep directory tree and either doing a full dir listing, or in the case of CD failure, just listing the subdirectories available (those with <DIR> on the lines):

Rem If no parameter, just exit the batch file

if “%1” == “” goto end

cd %1 > nul 2 > nul

Rem if the CD works, give the full dir command:

if not errorlevel 1 goto fulldir

Rem “if not errorlevel 1…” means if errorlevel is not 1 or higher
Rem (in other words, zero), then execute the goto command.
Rem If the CD fails, list the subdirectories we could have chosen.

echo Sub-Directory “%1” not found:-
dir | find “<DIR>” | find /v ” .” | more
goto end



All those “echo.” commands insert blank output lines for readability. And the “find” with the /v parameter means select lines which do not contain a blank followed by a dot (” .”) – in other words do not list the directories “.” or “..”.

Here’s a typical use for such a feature. I want to go to the VB6 directory containing the source of my Application Programs:

C:\>#vb asd

Sub-Directory “asd” not found:-

23/12/2006 01:00 AM <DIR> pdfflip
04/05/2006 05:33 PM <DIR> pict
24/12/2006 04:14 PM <DIR> psflip
24/06/2005 12:17 PM <DIR> stretch
26/09/2008 04:07 PM <DIR> unscr1
13/10/2008 04:06 AM <DIR> unscr2

C:\Program Files\Microsoft Visual Studio\VB98\Aprog>

In this case the misspelt subdirectory is obvious but in general, given the original DOS error message, the user would have no idea what path The system cannot find.

PS: The more command, above, allows the output to pause at page boundaries, if necessary. The initial dir command, however, does not pause when the output is piped to another command (even with the /p parameter set by default).

One thought on “The DOS CD Command (or how to send stderr to nul)

  1. Hi my name is Donna and I just wanted to drop you a quick note here instead of calling you. I came to your The DOS CD Command (or how to send stderr to nul) | Bob’s Blog page and noticed you could have a lot more hits. I have found that the key to running a successful website is making sure the visitors you are getting are interested in your niche. There is a company that you can get targeted traffic from and they let you try their service for free for 7 days. I managed to get over 300 targeted visitors to day to my site. Visit them here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s