windows 批处理文件。IF 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22334327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch Files. IF statements?
提问by SmkErrl
i have a batch file that needs to apply the attrib +h command to a file, then output to a txt file and display contents on screen. This should also be done if file has not been provided or can not be found. I have this so far but can not get it to work:
我有一个批处理文件,需要将 attrib +h 命令应用于文件,然后输出到 txt 文件并在屏幕上显示内容。如果未提供文件或找不到文件,也应执行此操作。到目前为止我有这个,但无法让它工作:
:TOP
IF EXIST "%1" GOTO COMMAND
) ELSE
(
GOTO ERROR1
:COMMAND
attrib +h %1
SHIFT
GOTO TOP
GOTO END
:ERROR1
IF "%1"=="" GOTO ERROR2
) ELSE
(
GOTO ERROR3
:ERROR2
ECHO.
ECHO No file(s) provided. Please re run the batch file.
GOTO END
:ERROR3
ECHO.
ECHO The file was not found. Please re run the batch file.
GOTO END
:END
This is my first computer course and any help will be greatly appreciated. Thank you.
这是我的第一门计算机课程,任何帮助将不胜感激。谢谢你。
回答by Nate Mara
There are a few issues with this code. Firstly, batch files require specific syntax with their IF
/ ELSE
statements.
这段代码有几个问题。首先,批处理文件需要带有IF
/ELSE
语句的特定语法。
Something like this
像这样的东西
IF EXIST "%1" (
echo "it's here!"
) ELSE (
echo "it isn't here!"
)
works correctly, while something like this
工作正常,而像这样的
IF EXIST "%1"
(
echo "it's here!"
)
ELSE
(
echo "it isn't here!"
)
does not. The parenthesis delimit the block, so your IF
command will execute everything in between (
and )
if it evaluates to true.
才不是。括号分隔块,因此您的IF
命令将执行其间的所有内容,(
并且)
如果它的值为真。
Secondly, you don't actually need any ELSE
statements. Because you are using GOTO
commands just before your ELSE
commands, you will never reach the second GOTO
command if the first IF
evaluates to true.
其次,您实际上不需要任何ELSE
语句。因为您在GOTO
命令之前使用命令ELSE
,所以GOTO
如果第一个命令的IF
计算结果为真,您将永远不会到达第二个命令。
Finally, with the code that you currently show, the :TOP
tag that you have is unnecessary.
最后,对于您当前显示的:TOP
代码,您拥有的标签是不必要的。
After all of that, you should be left with something that looks like this:
毕竟,你应该得到如下所示的东西:
@ECHO off
IF EXIST "%1" (
GOTO COMMAND
)
GOTO ERROR1
:COMMAND
echo "You entered a file correctly, and it exists!"
GOTO END
:ERROR1
IF "%1"=="" (
GOTO ERROR2
)
GOTO ERROR3
:ERROR2
ECHO.
ECHO No file(s) provided. Please re run the batch file.
GOTO END
:ERROR3
ECHO.
ECHO The file was not found. Please re run the batch file.
GOTO END
:END
回答by Justin
I am not to familiar with Batch, but it looks like your If statement is formatted wrong.
我对 Batch 不太熟悉,但看起来您的 If 语句格式错误。
IF EXIST "%1" (
GOTO COMMAND
) ELSE
(
GOTO ERROR1
)
回答by MC ND
Just some problems with parenthesis and flow logic
只是括号和流程逻辑的一些问题
@ECHO OFF
IF "%~1"=="" GOTO ERROR1
:TOP
IF NOT EXIST "%~1" GOTO ERROR2
attrib +h "%~1"
IF "%~2"=="" GOTO END
SHIFT
GOTO TOP
:ERROR1
ECHO.
ECHO No file(s) provided. Please re run the batch file.
GOTO END
:ERROR2
ECHO.
ECHO The file "%~1" was not found. Please re run the batch file.
GOTO END
:END