Home > Enterprise >  windows bat file cannot comment out this
windows bat file cannot comment out this

Time:09-15

I am trying to comment out some useful hints. I followed this Stack Overflow page to apply the most defensive comment - using double quotes. Comment error. But I still get error

This is my simple script: test_comment.cmd

@echo off

:: "   %~nI        - expands %I to a file name only "
:: "   %~xI        - expands %I to a file extension only "
for /F "delims=" %i in ("c:\foo\bar baz.txt") do @echo %~nxi

This is the error I got when I run it

>test_comment
The following usage of the path operator in batch-parameter
substitution is invalid: %~nI        - expands %I to a file name only "


For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.

If I removed those two commented lines, error would disappear; so that I know the comment didn't work.

How should I comment out those two lines?

I really don't want to delete them.

I am using windows 10, latest patch, default cmd.exe.

CodePudding user response:

I followed the link in @Stephan's comment and read: Batch Line Parser

Phase 0) Read Line:

Phase 1) Percent Expansion:

Phase 2) Process special characters, tokenize, and build a cached command block

In short: Comment is parsed in Phase 2, after the Percent Expansion in Phase 1.

Solution: use % to escape the % in the comment, as suggested by @Magoo in the comment.

@echo off

:: "   %%~nI        - expands %%I to a file name only "
:: "   %%~xI        - expands %%I to a file extension only "
for /F "delims=" %%I in ("c:\foo\bar baz.txt") do @echo %%~nxI

Then I got the desired result

>test_comment
bar baz.txt
  • Related