如果存在..&如果存在......(ECHO ......)ELSE(ECHO ..)对我不起作用

时间:2015-06-09 00:33:58

标签: batch-file

AFAIK工作解决方案由TripeHound共享.... 如果要测试两个文件是否存在,则需要IF EXIST D:\ann.PDF IF EXIST D:\file.html ECHO ...第一个IF的条件命令是第二个IF和如果这也是真的,则执行ECHO。 - TripeHound

此处接受的答案How to run two commands in one line in Windows CMD?对我不起作用。

当我只有一个' IF EXIST ...'没有问题。当我尝试使用&,(或&& in .bat)在同一行上使用两个时,批处理文件崩溃或CLI发布错误"" &安培;此时出人意料 ""

这是我尝试的代码和(非常一致的)结果

 C:\Windows\system32> IF EXIST D:\file.html  (ECHO this is good) ELSE (ECHO this really sucks)
 this is good

响应是"这很好"因为两个测试文件(ann.PDF和file.html)都存在

  C:\Windows\system32> IF EXIST D:\ann.PDF & IF EXIST D:\file.html (ECHO this is good) ELSE (ECHO this really sucks)
    & was unexpected at this time.

我还没有弄清楚为什么会出现 '意外' 错误..欢迎提出任何有效的建议...... Windows 7 x64但解决方案也必须适用于XP sp3 .... TVMIA!

1 个答案:

答案 0 :(得分:0)

If exist <filename> <command>

你不能对那些不能工作的人发号施令。 &&&||处理命令的错误级别。 If Exist没有。

C:\Users\David Candy>type %windir%\winhelp.ini  && (echo. & echo found) || echo
not found
[Files]
Serenity.hlp=C:\Users\David Candy\Desktop

found

C:\Users\David Candy>type %windir%\winhelp1.ini  && (echo. & echo found) || echo
 not found
The system cannot find the file specified.
not found