0

So apparently it's possible to have invalid syntax in batch file comments. Could someone please explain why the code below causes a syntax error when it's in a batch file?

rem %~mdir%

The exact message cmd.exe spits out says:

The following usage of the path operator in batch-parameter
substitution is invalid: %~mdir%

For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.
Jenna Sloan
  • 372
  • 8
  • 22
  • @Compo Yes, that's the entire content of the batch file. I ran it using cmd.exe and it says "`The following usage of the path operator in batch-parameter substitution is invalid: %~mdir% For valid formats type CALL /? or FOR /? The syntax of the command is incorrect.`" – Jenna Sloan Aug 11 '19 at 23:35

1 Answers1

2

The rem command is recognised after (immediate) variable expansion, which also includes expansion of argument references, like %0, %1, %2, etc. These arguments also support ~-modifiers (like %~1 or %~f1, for instance). So the command interpreter detects %~ and now it expects either a decimal digit or a valid modifier (d, p, n, x, f, s, a, t, z, or $ENV:, where ENV is a variable), both of which are not present. That is why a fatal syntax error arises, which even aborts execution of the remaining script.

Take also a look at this thread for more details: How does the Windows Command Interpreter (CMD.EXE) parse scripts? (pay particular attention to this answer)

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Also rem /? will try to give a help message or error, even if /? is part of a webpage address. – js2010 Aug 19 '20 at 17:41
  • Yes, @js2010, `rem` is quite greedy for `/?`, but luckily this can be avoided when writing `rem/ /?`… – aschipfl Aug 19 '20 at 22:35