2007-08-30

Windows BAT vs. Spaces in Filenames

The BAT construct FOR %%I in (%~dp0\somefile.txt) will fail if the current directory has a space in the fully qualified path.  It will interpret the path as a list of space-separated paths instead.  The usual solution, to put the path in quotes, will not work in this context since FOR treats a quoted string as a literal line to be processed instead of as a single filename.

The workaround is to use %~dps0 instead of %~dp0.  Thus, short 8.3 filenames will be used instead, eliminating the problem.