Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何创建具有多个其他条件的批处理文件?_Batch File - Fatal编程技术网

Batch file 如何创建具有多个其他条件的批处理文件?

Batch file 如何创建具有多个其他条件的批处理文件?,batch-file,Batch File,我想创建一个包含多个其他条件的批处理文件,并且已经搜索了很多,但我的批处理文件不起作用。我希望该文件检查是否存在两个文件,如果存在,然后打开其中一个文件。如果两个文件中的一个不存在,则批处理应比较下两个文件。我的文件如下所示: IF EXIST "file1.txt" IF EXIST "file2.txt" Goto V1 IF EXIST "file3.txt" IF EXIST "file4.txt" Goto V2 IF EXIST "file5.txt" AND IF EXIST

我想创建一个包含多个其他条件的批处理文件,并且已经搜索了很多,但我的批处理文件不起作用。我希望该文件检查是否存在两个文件,如果存在,然后打开其中一个文件。如果两个文件中的一个不存在,则批处理应比较下两个文件。我的文件如下所示:

IF EXIST "file1.txt" IF EXIST "file2.txt" Goto V1

IF EXIST "file3.txt" IF EXIST "file4.txt" Goto V2

IF EXIST "file5.txt" AND IF EXIST "file6.txt" Goto V3

:V1
cd "folder"
start sample.exe
goto commonexit

:V2
cd "folder2"
start sample2.exe
goto commonexit

:V3
cd "folder3"
start sample3.exe
goto commonexit

:commonexit
像这样,cmd打开并立即关闭。当我注释掉“:commonexit”时,它会打开并在代码中运行,但它看起来像是在双IF条件中(IF…IF…),它只关心第二个IF。在它们之间放置AND运算符没有帮助

你们有没有人猜猜,会出什么问题

编辑:commonexit正在工作。我只是不知道:commonexit之后的特征线会使代码损坏。

这里有一种方法:

@echo  off
if exist "file1.txt" if exist "file2.txt" (
cd "folder"
start sample.exe
goto :EOF
)

if exist "file3.txt" if exist "file4.txt" (
cd "folder2"
start sample2.exe
goto :EOF
)

if exist "file5.txt" if exist "file6.txt" (
cd "folder3"
start sample3.exe
goto :EOF
)
逻辑如下:
检查
file1
是否存在,如果存在,检查
file2
是否存在,如果存在,
cd
,执行示例,然后转到文件结尾。但是,如果
file1
file2
不存在,它将跳过当前代码块并转到
的下一行,如果
的这里有一个替代的单行解决方案(拆分为3以保持最大80个字符的行长),它使用
Where
命令:

(Where/Q“file1.txt”&&&(Where/Q“file2.txt”&&Start/D“文件夹”“sample.exe”))||(
Where/Q“file3.txt”&&&(Where/Q“file4.txt”&&Start/D“folder2”“sample2.exe”
))||Where/Q“file5.txt”和&Where/Q“file6.txt”和&Start/D“folder3”“sample3.exe”

更简单,更接近原始代码:

IF EXIST "file1.txt" IF EXIST "file2.txt" cd "folder" & start sample.exe & goto commonexit

IF EXIST "file3.txt" IF EXIST "file4.txt" cd "folder2" & start sample2.exe & goto commonexit

IF EXIST "file5.txt" IF EXIST "file6.txt" cd "folder3" & start sample3.exe & goto commonexit

rem Put here code that will execute when none of the previous paths execute...

:commonexit

编辑:添加新方法

下面的新方法允许在几个“EXIST filename”测试上编写有效的“AND”操作,并以类似于几个“ELSE IF”命令的方式链接其中几个测试:

(for /F "skip=1" %%a in ('dir /B file1.txt file2.txt') do break) && (
   echo Both file1.txt and file2.txt exists
   echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file3.txt file4.txt') do break) && (
   echo Both file3.txt and file4.txt exists
   echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file5.txt file6.txt') do break) && (
   echo Both file5.txt and file6.txt exists
   echo Process they here
) || echo Last else
此方法基于由
for/F
命令返回的“ExitCode”值,如(以下退出代码管理部分)所述。只需插入额外的文件名并将“skip=1”值调整为文件数减1,即可轻松地将该方法扩展到更多文件。例如:

(for /F "skip=2" %%a in ('dir /B file1.txt file2.txt file3.txt') do break) && (
   echo All file1.txt, file2.txt and file3.txt exists
   echo Process they here
) || (for /F "skip=2" %%a in ('dir /B file4.txt file5.txt file6.txt') do break) && (
   echo All file4.txt, file5.txt and file6.txt exists
   echo Process they here
) || echo Last else

在批处理中没有什么比“AND运算符”更好的了。你是从双击开始的吗?如果是,请注意,工作文件夹可能不是您所期望的。运行
echo on
查看它的实际功能。(并在
:commonexit
之后放置
暂停
,以防止窗口关闭)嗨,Stephan,感谢您的回复。我不知道的是:commonexit之后出现特征线时,代码将无法工作。我将用Gerhard的代码再试一次,并发布我的结果。我建议使用例如
Start/D“folder2”sample2.exe
可能比使用
cd“folder2”
然后再使用
Start sample2.exe
要好。。。您正在谈论
else
子句,但我在您发布的代码中找不到任何
else
。;-)嗨,格哈德,谢谢你的回复。我尝试了您的方法,但即使文件1不存在,批处理仍希望打开sample1.exe。猜猜我该如何改变它?我使用了你的示例1,它打开了sample1.exe,文件为1.txt missing,我现在就开始工作了。它仍然是这种缺失和联系。因此,我将双IF条件(IF NOT…IF NOT…GOTO…)分为两行(IF NOT…GOTO…\LINEBREAK IF NOT…IF NOT…GOTO…)。它看起来不是很有条理,但它很管用。让我们来看看。这不管用。当“file1.txt”存在且“file2.txt”不存在时,执行第一个
ELSE
,但如果“file1.txt”不存在,则不执行其他代码。换句话说:第一个
ELSE
属于第二个
IF
命令;第一个
IF
命令没有任何
ELSE
部分。同样的观点也适用于“file3.txt”和“file5.txt”的“如果”。@Aacini,你是对的,这是一个懒惰的回答,我在发布之前没有正确地考虑这个问题。我已经完全修改了答案,使用了另一种方法。我邀请你回顾一下我在<代码>;)接受邀请!这很聪明,尽管它看起来比我自己的答案更奇怪,但我认为它比我自己的答案更容易扩展更多的文件和扩展更多的命令。它对外部流程的依赖也更少,而且应该更快!