使用多个IF在批处理文件语句 [英] Using multiple IF statements in a batch file

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

问题描述

在使用超过1 IF语句,有一个特殊的指导方针应遵循?他们应该进行分组?我应该使用括号来包装指令(S)?

使用一个例子是:

 是否存在somefile.txt是否存在someotherfile.txt SET VAR = somefile.txt,someotherfile.txt


解决方案

  

有一个特殊的指导方针应遵循


有没有标准的方式做批处理文件,因为绝大多数的作者和维护者的或者不理解编程概念,或者他们认为他们并不适用于批处理文件。

但我是一个程序员。我已经习惯了编译,我已经习惯了调试器。批处理文件没有编译,你可以不通过调试器中运行他们,所以他们让我紧张。我建议你​​要对你写什么额外的严格,所以你可以很肯定它会做什么,你认为它。

有一些编码的标准,说:如果你写了如果语句,则必须使用大括号,即使你没有一个其他条款。这样可以节省你从细微,难以调试的问题,而且是明确的可读性。我看不出有什么理由,你不能这样的推理适用于批处理文件。

让我们来看看你的code。

 是否存在somefile.txt是否存在someotherfile.txt SET VAR = somefile.txt,someotherfile.txt

以及如果语法,从命令,帮助,如果

  IF [NOT] ERRORLEVEL number命令
IF [NOT]字符串1 ==字符串2命令
IF [NOT] EXISTS filename命令...如果存在的文件名(
  命令
)ELSE(
  其他命令

所以,你要串联如果 s,因为命令。

如果您使用常见的编码标准规则我上面提到的,你总是想用小括号。这里是你将如何为您例如code这样做的:

 是否存在somefile.txt(
  如果不存在someotherfile.txt(
    SET VAR =somefile.txt,someotherfile.txt
  )

请确保您清晰格式,并做一些形式的压痕。你这样做在code,你应该做在你的批处理脚本。

此外,你也应该在经常引用您的文件名,并获得正确的报价的习惯。目前在帮助一些空话FOR HELP SET ,这将帮助你除去多余的报价再次引用字符串时。

修改

从您的意见,并重新阅读你原来的问题,好像你想建立一个逗号分隔存在的文件列表。对于这种情况,你可以简单地用一串,如果 / 其他语句,但是这将导致一堆重复的逻辑,而不会在所有干净的,如果你有两个以上的文件。

有一个更好的办法是写一个子程序,检查单个文件的存在,并追加到一个变量如果指定的文件存在。然后,只需调用子程序要检查每个文件:

  @ECHO OFF
SETLOCALREM TODO:在此处设置全局脚本变量
CALL:MainScript
GOTO:EOFREM MainScript()
:MainScript
  SETLOCAL  CALL:AddIfExistssomefile.txt%文件%,文件
  CALL:AddIfExistssomeotherfile.txt%文件%,文件  ECHO.Files:%文件%  ENDLOCAL
GOTO:EOFREM AddIfExists(文件名,existingFilenames,returnVariableName)
:AddIfExists
  SETLOCAL  如果不存在%〜1(
    SET结果=%〜1
  )ELSE(
    SET结果=
  )  (
    REM清理,并返回结果 - 串联如有必要,
    ENDLOCAL    IF%〜2==(
      SET%〜3%=%的结果
    )ELSE(
      SET%〜3 =%〜2,%结果%
    )
  )
GOTO:EOF

When using more than 1 IF statement, is there a special guideline that should be followed? Should they be grouped? Should I use parenthesis to wrap the command(s)?

An example to use would be:

IF EXIST somefile.txt IF EXIST someotherfile.txt SET var=somefile.txt,someotherfile.txt

解决方案

is there a special guideline that should be followed

There is no "standard" way to do batch files, because the vast majority of their authors and maintainers either don't understand programming concepts, or they think they don't apply to batch files.

But I am a programmer. I'm used to compiling, and I'm used to debuggers. Batch files aren't compiled, and you can't run them through a debugger, so they make me nervous. I suggest you be extra strict on what you write, so you can be very sure it will do what you think it does.

There are some coding standards that say: If you write an if statement, you must use braces, even if you don't have an else clause. This saves you from subtle, hard-to-debug problems, and is unambiguously readable. I see no reason you couldn't apply this reasoning to batch files.

Let's take a look at your code.

IF EXIST somefile.txt IF EXIST someotherfile.txt SET var=somefile.txt,someotherfile.txt

And the IF syntax, from the command, HELP IF:

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXISTS filename command

...

IF EXIST filename (
  command
) ELSE (
  other command
)

So you are chaining IF's as commands.

If you use the common coding-standard rule I mentioned above, you would always want to use parens. Here is how you would do so for your example code:

IF EXIST "somefile.txt" (
  IF EXIST "someotherfile.txt" (
    SET var="somefile.txt,someotherfile.txt"
  )
)

Make sure you cleanly format, and do some form of indentation. You do it in code, and you should do it in your batch scripts.

Also, you should also get in the habit of always quoting your file names, and getting the quoting right. There is some verbiage under HELP FOR and HELP SET that will help you with removing extra quotes when re-quoting strings.

Edit

From your comments, and re-reading your original question, it seems like you want to build a comma separated list of files that exist. For this case, you could simply use a bunch of if/else statements, but that would result in a bunch of duplicated logic, and would not be at all clean if you had more than two files.

A better way is to write a sub-routine that checks for a single file's existence, and appends to a variable if the file specified exists. Then just call that subroutine for each file you want to check for:

@ECHO OFF
SETLOCAL

REM Todo: Set global script variables here
CALL :MainScript
GOTO :EOF

REM MainScript()
:MainScript
  SETLOCAL

  CALL :AddIfExists "somefile.txt" "%files%" "files"
  CALL :AddIfExists "someotherfile.txt" "%files%" "files"

  ECHO.Files: %files%

  ENDLOCAL
GOTO :EOF

REM AddIfExists(filename, existingFilenames, returnVariableName)
:AddIfExists
  SETLOCAL

  IF EXIST "%~1" (
    SET "result=%~1"
  ) ELSE (
    SET "result="
  )

  (
    REM Cleanup, and return result - concatenate if necessary
    ENDLOCAL

    IF "%~2"=="" (
      SET "%~3=%result%"
    ) ELSE (
      SET "%~3=%~2,%result%"
    )
  )
GOTO :EOF

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

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