batch-file - 如何在批处理文件中使用 if - else 结构?

标签 batch-file if-statement

我对批处理文件中的 if - else 结构有疑问。每个命令单独运行,但我无法安全地使用“if - else” block ,因此我的程序的这些部分不起作用。我怎样才能使这些部件运行?谢谢。

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )
ELSE IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

ELSE IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )
ELSE IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )

最佳答案

您的语法不正确。您不能使用ELSE IF。看来你其实并不需要它。只需使用多个 IF 语句即可:

IF %F%==1 IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )

IF %F%==1 IF %C%==0 (
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

IF %F%==0 IF %C%==1 (
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )

IF %F%==0 IF %C%==0 (
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )

很棒的批处理文件引用:http://ss64.com/nt/if.html

关于batch-file - 如何在批处理文件中使用 if - else 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081735/

相关文章:

batch-file - 批处理文件中的子程序

windows - 如何使用 WinRAR 创建 ZIP 文件,ZIP 文件的名称是当前文件夹的名称?

java - 在Python中运行包含java命令的批处理文件

javascript - 将 "tagName"属性与 "img"进行比较对于 <img> 元素不起作用

python - python3.6中if else的简化,具有多种情况

java - 为什么 .bat 文件会跳行并跳到末尾?

windows - 如何在启动时以管理员身份运行脚本

c - 程序未检测 if 语句

java - 如果语句不执行

mysql - 检查记录是否存在于不同的表中