Wednesday, August 1, 2007

Where is my old copy of Batch Files for Dummies?

So I spent the bulk of yesterday morning reminding myself how to write DOS batch files. We have a new customer who is trying to write pre/post scripts for Windows, and has never heard of using a non-zero exit code to indicate failure.

Well I haven't done any batch file programming in about a decade, so I'm a little rusty. For example, I had forgotten about this little gem:

@echo off

call runsomeprogram.bat
if errorlevel 0 goto dosomething

:dosomething
whatever...

In this case, "errorlevel" doesn't do what you would expect it to. You would think that if errorlevel was equal to zero, then the "goto dosomething" would happen. According to Microsoft's infinite wisdom, checks for errorlevel are for "equal to or greater than". Therefore in this case if the return value was 63 it would still evaluate to true. In fact, in EVERY case the if statement would evaluate to true (DOS doesn't support negative exit codes). Doh.

The irony of course, is that I'm doing this at all. Sure I had to look up some of the nuance of the syntax, but it's not like there is some shortage of documentation on the Interweb for batch file programming. I had to write a series of examples for the customer demonstrating how to use exit codes and environment variables. Surely there has to be some better use of my time?