批处理文件.IF语句? [英] Batch Files. IF statements?

查看:74
本文介绍了批处理文件.IF语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件,需要将attrib + h命令应用到文件,然后输出到txt文件并在屏幕上显示内容.如果尚未提供文件或找不到文件,也应执行此操作.到目前为止,我有这个功能,但无法正常工作:

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:

: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.

推荐答案

此代码存在一些问题.首先,批处理文件需要使用其 IF / ELSE 语句的特定语法.

There are a few issues with this code. Firstly, batch files require specific syntax with their IF / ELSE statements.

类似这样的东西

IF EXIST "%1" (
    echo "it's here!"
) ELSE (
    echo "it isn't here!"
)

正常工作,而类似

IF EXIST "%1" 
(
    echo "it's here!"
) 

ELSE 
(
    echo "it isn't here!"
)

没有.括号定界了该块,因此,如果您的 IF 命令计算为true,则将执行()之间的所有操作.

does not. The parenthesis delimit the block, so your IF command will execute everything in between ( and ) if it evaluates to true.

第二,您实际上不需要任何 ELSE 语句.由于您在 ELSE 命令之前使用 GOTO 命令,因此,如果第一个 IF ,您将永远无法到达第二个 GOTO 命令.code>计算为true.

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.

最后,使用您当前显示的代码,您不需要的:TOP 标记.

Finally, with the code that you currently show, the :TOP tag that you have is unnecessary.

所有这些之后,应该剩下类似以下的内容:

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

这篇关于批处理文件.IF语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆