OK, I somehow fooled myself with my Win 10 testing. I can't find any difference in behavior on this topic between Win 7 and Win 10 - thank goodness
Where I ran into trouble was I had multiple batch files named things line "REM .BAT", "REM;.BAT", etc. And I did not structure them in a way that let me identify which one ran.
So when I tested something like
I assumed it was x.bat that was running, when actually it was "REM;.BAT" that ran. So I arrived at the wrong conclusions as to what worked and what did not.
From what I can tell, the following form:
rem<C><tokenDelimiter> works always when
<C> is one of [backSlash], [forwardSlash], or [colon], and
<tokenDelimiter> is one of [space], [tab], [0xFF], [comma], [semicolon], or [equal]. I don't see any need to escape the
<C> character.
So all of the following work, even if x.bat exists in the current directory. Note - this is not an exhaustive list of all working permutations.
Code: Select all
@echo off
echo 1
REM\ \..\..\x.bat
echo 2
REM/ \..\..\x.bat
echo 3
REM: \..\x.bat
REM\ \..\..\x.bat & echo 4
REM/ \..\..\x.bat & echo 5
REM: \..\x.bat & echo 6
echo 7
REM:;\..\x.bat
echo 8
REM:,\..\x.bat
echo 9
REM:=\..\x.bat
REM:;\..\x.bat & echo 10
REM:,\..\x.bat & echo 11
REM:=\..\x.bat & echo 12
A critical component to why the above work is that : \ and / cannot appear in file names.
One additional form that seems to work is
REM^[TAB]<tokenDelimiter>, but I'm not sure why. I can get the [TAB] in a file name, but the problem cases below don't seem to matter with [TAB].
As jeb already said,
REM. is no good because it fails always if a file named "REM" (without an extension) exists in the current directory.
All the other candidates can fail in a batch file because they can appear in a file name. Note - only one of the following lines can be tested at a time
Code: Select all
REM^ This fails if "REM .BAT" exists
REM^; This fails if "REM;.BAT" exists
REM^, This fails if "REM,.BAT" exists
REM^= This fails if "REM=.BAT" exists
REM^[0xFF] This fails if "REM[0xFF].BAT" exists
REM[ This fails if "REM[.BAT" exists
REM] This fails if "REM].BAT" exists
REM+ This fails if "REM+.BAT" exists
But append
& echo something to each line above, and they all work.
All potential characters can fail if there is not an unescaped token delimiter afterward because of the
\..\x.bat issue. The unescaped token delimiter is required to disrupt the file path parsing.
So my results are similar to what jeb reported, accept
- I don't see any need to escape the : / or \
- = Cannot always be used safely, even if escaped
Dave benham