Spur of the moment post. I haven’t touched batch scripts in a very long while, but just recently picked up an old script I had, to tailor it for something I’m doing now. What it did isn’t exactly important for the purposes of this discussion.
Every batch script should ideally have a help text. For example, imagine foo.cmd, when you run it with the generic help option "/?":
Usage: foo.cmd [options]
This script foo will do wonderful things for you depending on the options:
/bar – will do thing 1
/baz – will do thing 2
/? – displays help
How does one do this in a batch script? Simple:
if "%1"=="/?" (
echo.
echo Usage: %0 [options]
echo This script will do wonderful things depending on the options:
echo /bar – will do thing 1
echo /baz – will do thing 2
echo /? – displays help
goto :EOF
)
Great! Now let’s run it. Here’s the output:
Usage: foo.cmd [options]
This script foo will do wonderful things for you depending on the options:
/bar – will do thing 1
/baz – will do thing 2
Displays messages, or turns command-echoing on or off.ECHO [ON | OFF]
ECHO [message]
What the hell? You have to escape question marks? You might see the bug in the script right away, but I will admit it took me several minutes of scratching my head before figuring out what the hell was going on. My thought process went something like this:
C:>echo /?
Displays messages, or turns command-echoing on or off.ECHO [ON | OFF]
ECHO [message]C:>echo ?
?C:>echo /
/C:>echo /^?
Displays messages, or turns command-echoing on or off.ECHO [ON | OFF]
ECHO [message]C:>echo ^/?
Displays messages, or turns command-echoing on or off.ECHO [ON | OFF]
ECHO [message]
I am ashamed to admit it took many iterations of fooling around like this before I figured out what the hell is going on. (What’s the problem? ..figure it out yourself.)
So what is the solution?
C:>echo. /? – displays help
/? – displays help
Yet another batch script oddity.