0

AFAIK the working solution was kindly shared by TripeHound .... If you want to test both files exist, you want IF EXIST D:\ann.PDF IF EXIST D:\file.html ECHO ... The conditional command of the first IF is the second IF and if that is also true, the ECHO is executed. – TripeHound

The answers accepted here How to run two commands in one line in Windows CMD? are not working for me.

When I have only one 'IF EXIST ..' there is no problem. When I try having two on the same line using &, (or && in .bat) the batch file crashes or the CLI post an error "" & was unexpected at this time ""

This is the code I tried and the (very consistent) results

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

response is "this is good" because both of the test files (ann.PDF and file.html) exist

  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.

I haven't got a clue why the 'unexpected' error is given.. any productive suggestions are welcome ... using Windows 7 x64 but solution has to also work on XP sp3 .... TVMIA !

Community
  • 1
  • 1
  • 1
    possible duplicate of [Using an OR in an IF statement WinXP Batch Script](http://stackoverflow.com/questions/7273433/using-an-or-in-an-if-statement-winxp-batch-script) – Dawa Law Jun 09 '15 at 00:37
  • @Dawa Law @trigger Thanks !. I looked at that and dont think it applies ... try experiment: put the following in standard windows cmd console `IF EXIST D:\ann.PDF & IF EXIST D:\file.html (ECHO passes) ELSE (ECHO fails)` I anticipate you will see a response **fails** bcz you dont have the files in your D partition, and I expect **passes** bcz files exist. The error ` & was unexpected at this time` . I think I would see if there was an open bracket anywhere before ` & ` but that is **not** the case. `IF EXIST D:\ann.PDF (ECHO passes) ELSE (ECHO fails)` by itself **passes** – user3345612 Jun 09 '15 at 12:27
  • If you want to test both files exist, you want `IF EXIST D:\ann.PDF IF EXIST D:\file.html ECHO ...` The conditional command of the first `IF` is the second `IF` and if that is also true, the `ECHO` is executed. – TripeHound Jun 09 '15 at 13:46
  • @TripeHound That WORKS, and I am amazed that it does considering the answered question in the link included in my question copy. I am pleased that it works, and can you post a link where the info was located (if available), to help out anyone that gets directed to this thread?! – user3345612 Jun 09 '15 at 18:44
  • I told you the same thing 19 hours ago. You seem stupid. –  Jun 09 '15 at 20:10

1 Answers1

0
If exist <filename> <command>

You don't have a command on the one's that don't work. &, &&, and || work on error levels from a command. If Exist doesn't.

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

found

and

C:\Users\David Candy>type %windir%\winhelp1.ini  && (echo. & echo found) || echo
 not found
The system cannot find the file specified.
not found
  • **I am sorry.** Had far too many examples and confused the question. I have simplified the question content to minimum needed, to avoid confusion. – user3345612 Jun 09 '15 at 12:56
  • `If exist c:\somefile if exist someotherfile`. `If` is a command so can appear in the command part of another `if`. –  Jun 09 '15 at 14:08