可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a batch file that runs several python scripts that do table modifications.
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!)
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
. 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:
The rem
command is indeed for comments. 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. Since rem
commands don't do anything, it's safe to print them without side effects. To avoid printing a command, prefix it with @
, or, to apply that setting throughout the program, run @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.)
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
回答2:
::
or 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.
::
may fail within setlocal ENABLEDELAYEDEXPANSION
回答3:
No, plain old batch files use REM
as a comment. ECHO
is the command that prints something on the screen.
To "comment out" sections of the file you could use 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.
回答4:
The :: instead of REM was preferably used in the days that computers weren't very fast.
REM'ed line are read and then ingnored. ::'ed line are ignored all the way. This could speed up your code in "the old days". Further more after a REM you need a space, after :: you don't.
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.
As mentioned using GOTO to skip parts is an easy way to skip large pieces of code. Be sure to set a :LABEL at the point you want the code to continue.
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
回答5:
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.
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.
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.
@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
回答6:
Putting comments on the same line with commands: use & :: comment
color C & :: set red font color
echo IMPORTANT INFORMATION
color & :: reset the color to default
Explanation:
&
separates two commands, so in this case color C
is the first command and :: set red font color
is the second one.
Important:
This statement with comment looks intuitively correct:
goto error1 :: handling the error
but it is not a valid use of the comment. It works only because goto
ignores all arguments past the first one. The proof is easy, this goto
will not fail either:
goto error1 handling the error
But similar attempt
color 17 :: grey on blue
fails executing the command due to 4 arguments unknown to the color
command: ::
, grey
, on
, blue
.
It will only work as:
color 17 & :: grey on blue
So the ampersand is inevitable.
回答7:
You can comment something out using ::
or REM
:
your commands here
:: commenttttttttttt
or
your commands here
REM commenttttttttttt
To do it on the same line as a command, you must add an ampersand:
your commands here & :: commenttttttttttt
or
your commands here & REM commenttttttttttt
Note:
- Using
::
in nested logic (IF-ELSE
, FOR
loops, etc...) will cause an error. In those cases, use REM
instead.