July 30th, 2012

Why don't any commands work after I run my batch file? I'm told that they are not recognized as an internal or external command, operable program, or batch file.

I sort of forgot to celebrate CLR Week last year, so let’s say that CLR week is “on hiatus” until next year. To fill the summertime time slot, I’m going to burn off a busted pilot: This week is Batch File Week 2012. Remember, nobody actually enjoys batch programming. It’s just something you have to put up with in order to get something done. Batch programming is the COBOL of Windows. (Who knows, if people actually like Batch File Week [fat chance], maybe it’ll come back as a regular series.)

We’ll open Batch File Week with a simple puzzle.

A customer reported that after running their batch file, almost no commands worked any more!

C:\> awesomebatchfile.bat
... awesome batch file does its work ...
C:\> reg query "HKLM\Software\Clients\Mail" /ve
'reg' is not recognized as an internal or external command,
operable program or batch file.

Wha? Maybe I can run regedit.

C:\> regedit
'regedit' is not recognized as an internal or external command,
operable program or batch file.

OMG OMG OMG OMG.

C:\> notepad
'notepad' is not recognized as an internal or external command,
operable program or batch file.

Okay, first, sit down and take a deep breath. Maybe take a Chill Pill.

My first question was “Does awesomebatchfile.bat modify the PATH variable?” (This was, strictly speaking, a psychic debugging question, but a rather obvious one.)

The customer replied, “Nope. Here, I’ll send you the whole thing.”

And there it was, right there at the top of awesomebatchfile.bat:

set path=C:\awesomedir
if NOT "%1"=="" set path=%1
cd /d %path%
echo Awesomeness commencing in the %path% directory!
...

The customer figured it would be convenient to have a variable called path, unaware that this variable has special meaning to the command interpreter. The customer didn’t make the connection that their seemingly private variable called path was connected to the system variable of the same name (but by convention capitalized as PATH).

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.