I refer to question Add text to end of filename (but before extension) using batch file as I have the same problem. Using Windows 7 32-bit Enterprise (I know, I know ...) with all updates I wrote a tiny batch file pdfrename.bat with only three lines:

  1. a comment, starting with REM and no "hidden" continuation sign more to the right of the line

  2. the proposed command, copy-pasted from the source as provided by @Karan, commented out with REM

  3. the adopted command for batch (doubling the %):

    REM Rework 2020-12-16
    REM for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"
    for %%F in (*.pdf) do ren "%%~F" "%%~nF OdB%%~xF"

Running the command (3.) from the command-prompt,

for %F in (*.pdf) do ren "%~F" "%~nF OdB%~xF"

works fine.

But executing the whole batch-file pdfrename.bat from Windows explorer fails. Running the batch file from the command prompt pdfrename.bat produces an error message syntax error:

Die folgende Verwendung des Pfadoperators zur Ersetzung eines Batchparameters
ist ungültig: %~na version 1%~xa"

You need not to understand German. The important point is that the error message refers to the commented out second line (2.), not to the third line!

I tried to retype REM, inserted a tabulator after REM, inserted a second REM after the first (REM REM ....), inserted a third line after the second one with the same content and deleted the second line afterwards - nothing changed: the batch file terminates at the commented out second line with a syntax error. As soon as the incriminated second line is eliminated from the batch, the batch works fine.

I searched for "REM is ignored", but no luck, so I post the matter here. I never heard about or experienced before that the command processor tries at least to analyze a commented out line - and in the case that there is something wrong with the code after the comment-sign that it terminates the batch script.


4 Answers 4


The error-msg refers to the commented out second line

This is due to the very complex parsing that is used by cmd to process scripts.

In short the parser processes % before pretty much everything else (phase 1) and throws an error as some of the %s need to be doubled as %% when used in a batch file.

So in a batch file:

for %%F in (*.pdf) do ren "%%~F" "%%~nF OdB%%~xF"

is a valid command and:

REM for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"

is a broken command (the %a should be %%a, etc).

Note that:

REM for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"

is a valid command when run from the command line as then the % does not need to be doubled.

REM would be processed in phase 2 of the parser, but it never gets there as the % processing in phase 1 has already generated an error and terminated the parsing.

For all the gory details of the cmd parser please read parsing - How does the Windows Command Interpreter (CMD.EXE) parse scripts? - Stack Overflow

  • Technically the for %a in (*.txt) do ren "%~a" "%~na version 1%~xa" is correct so long as it's not in a batch file itself. :)
    – Mokubai
    Dec 16, 2020 at 22:45
  • @DavidPostill with other words "works as designed" - but not as described in tutorials. Even herer docs.microsoft.com/en-us/windows-server/administration/…
    – gkln
    Dec 16, 2020 at 23:54
  • 24
    Another way to look at it: don’t think of REM as a comment, but as an internal command that does nothing with its arguments. Dec 17, 2020 at 17:27
  • 1
    There are also labels in batch scripts, which are written as :: and are also often used as comments: <stackoverflow.com/questions/16632524/…>
    – knittl
    Dec 18, 2020 at 6:52
  • 1
    Wow, this just blew my mind. The CMD parser processes text before acknowledging it's just a comment. Just... WOW.
    – Massimo
    Dec 18, 2020 at 23:18

It doesn't work because even though it is "remarked" it is evaluated for substitutions, and then apparently discarded.

For "batch" your second line is incorrect and should read

REM Rework 2020-12-16
REM for %%a in (*.txt) do ren "%%~a" "%%~na version 1%%~xa"
for %%F in (*.pdf) do ren "%%~F" "%%~nF OdB%%~xF"

The corrections being that in batch programming expansions need to have %% rather than a single %.


To add to the existing answers, although REM is used for comments, it is important to understand that it is actually a command that does nothing, not a comment. This is different from a Unix shell, where you have real comments and can add arbitrary text after a # character.

Not only the substitutes are evaluated for the REM command, also file redirects. So beware that the following line will delete the content of the file, because the REM command produces no output, but the empty output will be redirected to the file

REM some_command > important_file


As a comment pointed out, this is no longer true for modern Windows versions. According to https://stackoverflow.com/a/4095133/10765659 there is now special handling of REM so that redirections are not executed, but this special handling occurs too late to make REM a true comment, as the substitutes are still evaluated.

  • 6
    Have you tested the alleged redirection issue? How exactly? In what OS? My tests (in Win 7 so far) don't confirm the issue, the content of important_file survives. This other answer states REM is special in phase 2 and "treated dramatically different". It seems what I observed confirms the other answer, not yours. Dec 17, 2020 at 12:19
  • @KamilMaciorowski I didn't test this recently. A colleague had this problem in the time of early Windows, probably around or before Windows 95, and I avoided that ever since. I'm glad to hear that it has been fixed.
    – RalfFriedl
    Dec 18, 2020 at 8:53
  • 4
    I was going to upvote this post for actually answering the question better than any of the other answers. But then I saw that, after seeing a comment pointing out an error in your post, you acknowledged it in another comment but you didn't edit the answer. Dec 19, 2020 at 0:23

This fact that REM is not a true comment but does some processing before being discarded points toward the answer to a problem in a batch file I had, one that I finally traced to a REM statement.

This statement in a batch file was meant only as a reminder of what this section was supposed to do:

REM '/?' flag

but instead consistently threw the error:

flag was unexpected at this point

Oddly enough, at previous points these lines produced no errors at all:

REM '-h' flag

REM '--help' flag

Because they worked, it was some time before I found the real cause. It was only by re-creating the original file line by line until I got a failure that I found real culprit.

Neither Google nor Bing could produce any results at all when searching for:

"flag was unexpected at this point"

although both produced plenty for:

"was unexpected at this point"

Perhaps REM was complaining about text following a flag it knew something of. I don't know.

In any case, when I changed REM to ECHO everything worked as expected. So I experimented a little more:

REM Testing '/?' flag

caused the error message to disappear as well.

I can see I'm going to have to be cautious about REM in the future.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .