use*_*044 816 comments cmd commenting batch-file comment-conventions
我有一个批处理文件,它运行几个进行表修改的python脚本.
我想让用户注释掉他们不想运行的1-2个python脚本,而不是从批处理文件中删除它们(所以下一个用户知道这些脚本作为选项存在!)
我还想添加注释,以便特别注意它们在运行之前需要在批处理文件中更新的变量.我明白我可以用了REM.但看起来更像是在用户运行后更新用户的进度.
是否有更适当添加评论的语法?
T.T*_*dua 883
:: 要么 REM:: commenttttttttttt
REM commenttttttttttt
Run Code Online (Sandbox Code Playgroud)
&字符:your commands here & :: commentttttttttttIF/ELSE,FOR循环等)内部使用REM因为::给出错误.:: 可能会失败 setlocal ENABLEDELAYEDEXPANSION Rob*_*edy 811
该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
Run Code Online (Sandbox Code Playgroud)
fvu*_*fvu 43
不,普通的旧批处理文件REM用作注释. ECHO是在屏幕上打印内容的命令.
要"注释掉"您可以使用的文件部分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
Run Code Online (Sandbox Code Playgroud)
我能说什么?批处理文件是很久以来的遗留物,它们笨重而丑陋.
编辑:稍微修改了一下示例,让它包含您正在寻找的元素.
Kee*_*ees 29
在计算机不是很快的时候,优选使用::而不是REM.读取REM行,然后进行刻录.::'ed line一直被忽略.这可以加速"旧时代"的代码.在REM之后你需要一个空间,在你不需要之后.
正如第一条评论中所述:您可以将信息添加到您认为需要的任何行
SET DATETIME=%DTS:~0,8%-%DTS:~8,6% ::Makes YYYYMMDD-HHMMSS
Run Code Online (Sandbox Code Playgroud)
至于零件的跳过.将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
Run Code Online (Sandbox Code Playgroud)
Som*_*luk 22
如果您想要注释掉大量的行,那么如果您可以创建多行注释而不是注释掉每一行,那将会更好.
批处理语言没有注释块,但有办法完成效果.
GOTO EndComment1
This line is comment.
And so is this line.
And this one...
:EndComment1
Run Code Online (Sandbox Code Playgroud)
您可以使用GOTOLabel和: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...
Run Code Online (Sandbox Code Playgroud)
mir*_*lav 12
& :: commentcolor C & :: set red font color
echo IMPORTANT INFORMATION
color & :: reset the color to default
Run Code Online (Sandbox Code Playgroud)
说明:
&分隔两个命令,所以在这种情况下color C是第一个命令,:: set red font color是第二个命令.
带注释的此声明看起来直观正确:
goto error1 :: handling the error
Run Code Online (Sandbox Code Playgroud)
但它不是评论的有效用途.它的工作原理只是因为goto忽略了第一个之后的所有参数.证明很简单,这goto也不会失败:
goto error1 handling the error
Run Code Online (Sandbox Code Playgroud)
但类似的尝试
color 17 :: grey on blue
Run Code Online (Sandbox Code Playgroud)
失败执行命令由于4个参数未知的color命令:::,grey,on,blue.
它只能用作:
color 17 & :: grey on blue
Run Code Online (Sandbox Code Playgroud)
所以&符号是不可避免的.
您可以使用::或注释掉某些内容REM:
your commands here
:: commenttttttttttt
Run Code Online (Sandbox Code Playgroud)
或者
your commands here
REM commenttttttttttt
Run Code Online (Sandbox Code Playgroud)
要在与命令相同的行上执行此操作,您必须添加一个&符号:
your commands here & :: commenttttttttttt
Run Code Online (Sandbox Code Playgroud)
或者
your commands here & REM commenttttttttttt
Run Code Online (Sandbox Code Playgroud)
:: 嵌套逻辑(IF-ELSE,FOR循环等)会导致错误。在这些情况下,请REM改用。