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

本文翻译自:How to “comment-out” (add comment) in a batch/cmd?

I have a batch file that runs several python scripts that do table modifications. 我有一个批处理文件,它运行几个进行表修改的python脚本。

  1. I want to have users comment out the 1-2 python scripts that they don't want to run, rather than removing them from the batch file (so the next user knows these scripts exist as options!) 我想让用户注释掉他们不想运行的1-2个python脚本,而不是从批处理文件中删除它们(所以下一个用户知道这些脚本作为选项存在!)

  2. I also want to add comments to bring to their attention specifically the variables they need to update in the Batch file before they run it. 我还想添加注释,以便特别注意它们在运行之前需要在批处理文件中更新的变量。 I see that I can use REM . 我看到我可以使用REM But it looks like that's more for updating the user with progress after they've run it. 但看起来更像是在用户运行后更新用户的进度。

Is there a syntax for more appropriately adding a comment? 是否有更适当添加评论的语法?


#1楼

参考:https://stackoom.com/question/lHfW/如何在批处理-cmd中-注释掉-添加注释


#2楼

No, plain old batch files use REM as a comment. 不,普通的旧批处理文件使用REM作为注释。 ECHO is the command that prints something on the screen. ECHO是在屏幕上打印内容的命令。

To "comment out" sections of the file you could use GOTO . 要“注释掉”文件的各个部分,您可以使用GOTO An example of all these commands/techniques: 所有这些命令/技术的示例:

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

What can I say? 我能说什么? batch files are a relic of times long gone, they're clunky and ugly. 批处理文件是很久以来的遗留物,它们笨重而丑陋。

You can read more on this website . 您可以在本网站上阅读更多内容

EDIT: modified the example a bit to have it contain the elements you are apparently looking for. 编辑:稍微修改了一下示例,让它包含您正在寻找的元素。


#3楼

The rem command is indeed for comments. rem命令确实用于评论。 It doesn't inherently update anyone after running the script. 运行脚本后,它本身并不会更新任何人。 Some script authors might use it that way instead of echo , though, because by default the batch interpreter will print out each command before it's processed. 但是,一些脚本作者可能会以这种方式使用它而不是echo ,因为默认情况下,批处理解释器会在处理之前打印出每个命令。 Since rem commands don't do anything, it's safe to print them without side effects. 由于rem命令不执行任何操作,因此可以安全地打印它们而没有副作用。 To avoid printing a command, prefix it with @ , or, to apply that setting throughout the program, run @echo off . 要避免打印命令,请在其@echo off加上@ ,或者要在整个程序中应用该设置,请@echo off (It's echo off to avoid printing further commands; the @ is to avoid printing that command prior to the echo setting taking effect.) (这是为了避免打印更多命令而echo off ; @是为了避免在回声设置生效之前打印命令。)

So, in your batch file, you might use this: 因此,在批处理文件中,您可以使用:

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

#4楼

Use :: or REM 使用::REM

::   commenttttttttttt
REM  commenttttttttttt

BUT (as people noted): 但是(正如人们所说):

  • If you use inline, you need to add & character: 如果使用内联,则需要添加&字符:
    your commands here & :: commenttttttttttt
  • Inside nested logic ( IF/ELSE , FOR loops, etc...) use REM because :: gives an error. 嵌套逻辑( IF/ELSEFOR循环等)内部使用REM因为::给出错误。
  • :: may fail within setlocal ENABLEDELAYEDEXPANSION ::可能在setlocal ENABLEDELAYEDEXPANSION失败

#5楼

The :: instead of REM was preferably used in the days that computers weren't very fast. 在计算机不是很快的时候,优选使用::而不是REM。 REM'ed line are read and then ingnored. 读取REM行,然后进行刻录。 ::'ed line are ignored all the way. ::'ed行一直被忽略。 This could speed up your code in "the old days". 这可以加速“旧时代”的代码。 Further more after a REM you need a space, after :: you don't. 在REM之后你需要一个空间,在你不需要之后。

And as said in the first comment: you can add info to any line you feel the need to 正如第一条评论中所述:您可以将信息添加到您认为需要的任何行

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

As for the skipping of parts. 至于零件的跳过。 Putting REM in front of every line can be rather time consuming. 将REM放在每条线的前面可能相当耗时。 As mentioned using GOTO to skip parts is an easy way to skip large pieces of code. 如前所述,使用GOTO跳过部分是一种简单的方法来跳过大量的代码。 Be sure to set a :LABEL at the point you want the code to continue. 确保在希望代码继续的位置设置: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

#6楼

Multi line comments 多行评论

If there are large number of lines you want to comment out then it will be better if you can make multi line comments rather than commenting out every line. 如果您想要注释掉大量的行,那么如果您可以创建多行注释而不是注释掉每一行,那将会更好。

See this post by Rob van der Woude on comment blocks : 请参阅Rob van der Woude关于评论块的这篇文章

The batch language doesn't have comment blocks, though there are ways to accomplish the effect. 批处理语言没有注释块,但有办法实现这种效果。

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

You can use GOTO Label and :Label for making block comments. 您可以使用GOTO Label和:Label来进行块注释。

Or, If the comment block appears at the end of the batch file, you can write EXIT at end of code and then any number of comments for your understanding. 或者,如果注释块出现在批处理文件的末尾,则可以在代码末尾写入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... 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值