Search This Blog

Loading...

Monday, 5 May 2008

Quotes in batch files - Correcting a small mistake

When you enter a filename (includes program names) inside a bat file, or enter a command on a command-prompt (cmd.exe) always make sure to enter it between "quotes". Microsoft made a "small mistake" when they developed Windows-95.... They allowed spaces in file-names and directory-names. This has caused a lot of trouble over the years... You know the DEL (delete a file) command?
Simple:
DEL C:\Rubbish\GetRidOfIt.txt
No problem above, the DEL command will know what to do. However, lets assume the directory "C:\Rubbish" is actually called:
C:\My Garbage
(note the space? yes, you do!) - so you enter
DEL C:\My Garbage\GetRidOfIt.txt
One of the improvements in Win95 and above is that you can enter multiple file specs on the command-line for DEL (and a lot of other commands too). The above command will be interpreted by Windows as
DEL C:\My
DEL Garbage\GetRidOfIt.txt

Probably not what you want... The second command (DEL Garbage\....) could be in any directory where your script is running, how would you know? It would use the "current directory" to find out which file you meant.

Lets suppose you wanted to remove some files in the program-files directory:

DEL C:\Program Files\My Program\*.*
Windows would try to
DEL C:\Program
DEL Files\My
DEL Program\*.*

So....

Inside batch scripts and on command-lines, ALWAYS enclose everything in "quotes", this tells Windows: No, I am not entering multiple arguments here, please treat this as a single file-spec

DEL "C:\Program Files\My Program\*.*"
DEL "C:\My Garbage\GetRidOfIt.txt"

Why do I tell you this?

Because our WatchDirectory program and future WatchFTP program starts your batch files with a lot of %VARIABLES% (see here), and they are not surrounded with quotes!

DEL "%WD_FILE%"

Just "quote" everything in your batch script

No comments: