首页 文章

如何在批处理/ cmd中“注释掉”(添加注释)?

提问于
浏览
686

我有一个批处理文件,它运行几个进行表修改的python脚本 .

  • 我想让用户注释掉他们不想运行的1-2个python脚本,而不是从批处理文件中删除它们(所以下一个用户知道这些脚本作为选项存在!)

  • 我还想添加注释,以便在运行它之前特别注意批处理文件中需要更新的变量 . 我看到我可以使用 REM . 但它看起来像's more for updating the user with progress after they'已经运行它 .

是否有更适当添加评论的语法?

7 回答

  • 42

    不,普通的旧批处理文件使用 REM 作为注释 . ECHO 是在屏幕上打印内容的命令 .

    对于文件的"comment out"部分,您可以使用 GOTO . 所有这些命令/技术的示例:

    REM it starts here the section below can be safely erased once the file is customised
    ECHO Hey you need to edit this file before running it!  Check the instructions inside
    ECHO Now press ctrl-c to interrupt execution or enter to continue
    PAUSE
    REM erase the section above once you have customised the file
    python executed1.py
    ECHO Skipping some stuff now
    GOTO End
    python skipped1.py
    python skipped2.py
    :END
    python executed2.py
    

    我能说什么?批处理文件是很久以来的遗留物,它们很笨重而且丑陋 .

    你可以阅读更多on this website .

    编辑:稍微修改了示例,使其包含您正在寻找的元素 .

  • 2

    在计算机不是很快的时候,优选使用::而不是REM . 读取REM行,然后重新读取 . ::'ed line一直被忽略 . 这可以加速“旧时代”的代码 . 在REM之后你需要一个空间,在你不需要之后 .

    正如第一条评论中所述:您可以将信息添加到您认为需要的任何行

    SET DATETIME=%DTS:~0,8%-%DTS:~8,6% ::Makes YYYYMMDD-HHMMSS
    

    至于零件的跳过 . 将REM放在每条线的前面可能相当耗时 . 如前所述,使用GOTO跳过部分是一种简单的方法来跳过大量的代码 . 确保在您希望代码继续的位置设置:LABEL .

    SOME CODE
    
    GOTO LABEL  ::REM OUT THIS LINE TO EXECUTE THE CODE BETWEEN THIS GOTO AND :LABEL
    
    SOME CODE TO SKIP
    .
    LAST LINE OF CODE TO SKIP
    
    :LABEL
    CODE TO EXECUTE
    
  • 22

    多行注释

    如果您想要注释掉大量的行,那么如果您可以创建多行注释而不是注释掉每一行,那将会更好 .

    批处理语言没有注释块,但有办法完成效果 .

    GOTO EndComment1
    This line is comment.
    And so is this line.
    And this one...
    :EndComment1
    

    您可以使用 GOTO Label和:Label来进行块注释 .

    或者,如果注释块出现在批处理文件的末尾,则可以在代码结尾处写入 EXIT ,然后在您的理解之后编写任意数量的注释 .

    @ECHO OFF
    REM Do something
      •
      •
    REM End of code; use GOTO:EOF instead of EXIT for Windows NT and later
    EXIT
    
    Start of comment block at end of batch file
    This line is comment.
    And so is this line.
    And this one...
    

    Comments Source

  • 751

    rem 命令确实用于评论 . 运行脚本后,它本身并不会更新任何人 . 但是,某些脚本作者可能会使用它而不是 echo ,因为默认情况下,批处理解释器会在处理之前打印出每个命令 . 由于 rem 命令可以安全地打印它们而没有副作用 . 要避免打印命令,请在其前面加上 @ ,或者,要在整个程序中应用该设置,请运行 @echo off . (避免打印更多命令是 echo off ; @ 是为了避免在回显设置生效之前打印该命令 . )

    因此,在批处理文件中,您可以使用:

    @echo off
    REM To skip the following Python commands, put "REM" before them:
    python foo.py
    python bar.py
    
  • 12

    使用命令将注释放在同一行:使用&:: comment

    color C          & :: set red font color
    echo IMPORTANT INFORMATION
    color            & :: reset the color to default
    

    Explanation:

    & separates two commands,所以在这种情况下 color C 是第一个命令, :: set red font color 是第二个命令 .


    重要:

    带注释的此声明看起来直观正确:

    goto error1         :: handling the error
    

    但它是评论的 not a valid use . 它的工作原理只是因为 goto 忽略了第一个之后的所有参数 . 证明很简单,这个 goto 也不会失败:

    goto error1 handling the error
    

    但类似的尝试

    color 17            :: grey on blue
    

    由于 color 命令未知的4个参数,执行命令失败: :: ,_ greyonblue .

    它只能用作:

    color 17     &      :: grey on blue
    

    So the ampersand is inevitable.

  • 28

    您可以使用 ::REM 注释掉某些内容:

    your commands here
    :: commenttttttttttt
    

    要么

    your commands here
    REM  commenttttttttttt
    

    要在与命令相同的行上执行此操作,必须添加&符号:

    your commands here      & ::  commenttttttttttt
    

    要么

    your commands here      & REM  commenttttttttttt
    

    注意:

    • 在嵌套逻辑中使用 ::IF-ELSEFOR 循环等...)将导致错误 . 在这些情况下,请改用 REM .
  • 690

    使用 ::REM

    ::   commenttttttttttt
    REM  commenttttttttttt
    

    但(正如人们所说):

    • 如果使用内联,则需要添加 & 字符:
      your commands here & :: commenttttttttttt

    • 内部嵌套逻辑( IF/ELSEFOR 循环等...)使用 REM 因为 :: 给出了错误 .

    • :: 可能在 setlocal ENABLEDELAYEDEXPANSION 内失败

相关问题