简体   繁体   English

windows bat文件不能注释掉这个

[英]windows bat file cannot comment out this

I am trying to comment out some useful hints.我正在尝试注释掉一些有用的提示。 I followed this Stack Overflow page to apply the most defensive comment - using double quotes.我按照这个 Stack Overflow 页面应用了最具防御性的评论 - 使用双引号。 Comment error . 评论错误 But I still get error但我仍然收到错误

This is my simple script: test_comment.cmd这是我的简单脚本: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.我正在使用 windows 10,最新补丁,默认 cmd.exe。

I followed the link in @Stephan's comment and read: Batch Line Parser我按照@Stephan 评论中的链接阅读: Batch Line Parser

Phase 0) Read Line:阶段 0) 读取线:

Phase 1) Percent Expansion:阶段 1) 扩张百分比:

Phase 2) Process special characters, tokenize, and build a cached command block阶段 2) 处理特殊字符、标记化并构建缓存的命令块

In short: Comment is parsed in Phase 2, after the Percent Expansion in Phase 1.简而言之:在第 1 阶段的百分比扩展之后,在第 2 阶段解析注释。

Solution: use % to escape the % in the comment, as suggested by @Magoo in the comment.解决方案:按照@Magoo 在评论中的建议,使用 % 转义评论中的 %。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM