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
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:
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).