【问题标题】:How to run multiple commands in if condition in batch file如何在批处理文件中的if条件下运行多个命令
【发布时间】:2019-08-12 15:48:24
【问题描述】:

我想在批处理文件中的 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..)

【问题讨论】:

标签: batch-file


【解决方案1】:

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

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

【讨论】:

  • 我建议%directoryPath% 有一个尾部反斜杠,以确保它检查的是目录是否存在,而不是文件。
猜你喜欢
  • 2016-04-27
  • 1970-01-01
  • 2015-11-15
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2018-02-14
相关资源
最近更新 更多