如何在批处理文件中的if条件中运行多个命令

时间:2019-08-12 15:48:24

标签: batch-file

我想在批处理文件中的if条件下运行多个命令。

我尝试了下面的代码,但对我不起作用。

IF not exist %directoryPath% (echo Invalid directory. goto :InvalidDirectory) ELSE (echo Sencha app build development started..)

代码:

:EnterDirectory
echo Enter your project directory
set /P directoryPath=
IF exist %directoryPath% (goto :init) ELSE (goto :InvalidDirectory)

:InvalidDirectory
echo This directory does not exists. 
(goto :EnterDirectory)

:init
IF not exist %directoryPath% (goto :InvalidDirectory) ELSE (echo Sencha app build development started..)

1 个答案:

答案 0 :(得分:2)

在使用批处理文件时,请勿尝试一行完成所有操作。当您对语法的关注不够时(很容易发生),这会使您的脚本不可读,甚至可能会给您带来错误/意外的结果。这种故障很难排除。将其分成多行(理想情况下,每行一个命令):

IF not exist %directoryPath% (
  echo Invalid directory.
  goto :InvalidDirectory
) ELSE (
  echo Sencha app build development started..
)

如果您坚持要一行执行,&是链接两个命令的正确方法:

IF not exist %directoryPath% (echo Invalid directory. & goto :InvalidDirectory) ELSE (echo Sencha app build development started..)