1

I wanted to make a batch file with multiple else conditions and already searched a lot but mine is not working. I want the file to check if two files exists and if they do then open one of that files. If one of the two files do not exist the batch should compare the next two files. My file looks like that:

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

Like this the cmd opens and immediately closes. When I comment out the ":commonexit" it opens and works throug the code but it seems like in the double IF conditions (IF ... IF...) it only cares about the second IF. Putting an AND operator between them does not help.

Has anyone of you a guess, what could be wrong?

EDIT: the :commonexit is working. I just did not know that a breakline after :commonexit would make the code corrupt.

Loeli
  • 75
  • 1
  • 8
  • 1
    there is nothing like an "AND Operator" in batch. Do you start it with a doubleclick? If yes, be aware, the working folder might not be what you expect. Run with `echo on` to watch, what it really does. (and put a `pause` after `:commonexit` to prevent the window to close) – Stephan May 11 '18 at 06:48
  • Hi Stephan, thanks for your response. What I did not know is that the code will not work when there is a breakline after :commonexit. I will try it again with the code of Gerhard and post my results. – Loeli May 11 '18 at 06:53
  • 1
    I would suggest that using e.g. `Start /D "folder2" sample2.exe` may be better than using `cd "folder2"` followed by `start sample2.exe`. – Compo May 11 '18 at 07:15
  • Hmm... you are talking about `else` clauses but I cannot find a single `else` in the code you posted... ;-) – aschipfl May 11 '18 at 11:46

3 Answers3

1

Here is one way:

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

Here is the logic: file1 is checked for existence, if exist check if file2 exists, if it does, cd, execute sample and then goto end of file. If however file1 or file2 does not exist, it will skip the current code block and goto the next line of if's

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Hi Gerhard, thanks for your response. I tried it your way but even though the file1 does not exists the Batch wants to open sample1.exe. Any guess how I can change that? – Loeli May 11 '18 at 06:56
  • I used your example 1 and it opened sample1.exe with file1.txt missing – Loeli May 11 '18 at 06:58
  • I got it working now. It is still this missing AND connection. So i put the double IF condition (IF NOT ... IF NOT ... GOTO ...) in two lines (IF NOT ... GOTO ... \LINEBREAK IF NOT ... IF NOT ... GOTO ...). It doesn't seem very organized but it works – Loeli May 11 '18 at 07:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170828/discussion-between-loeli-and-gerhard-barnard). – Loeli May 11 '18 at 07:05
1

Simpler and closer to the original code:

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

EDIT: New method added

The new method below allows to write an efficient "AND" operation over several "EXIST filename" tests, and to chain several of these tests in a way similar to several "ELSE IF" commands:

(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

This method is based on the "ExitCode" value returned by for /F command as explained at this answer (below Exit Code management section). The method can be easily extended to more files by just inserting the additional file names and adjusting the "skip=1" value to the number of files minus one. For example:

(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
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • _Invitation accepted!_ This is clever, although it looks even more odd than my own answer, I think that it would be easier to extend for more files and expand for additional commands than my own. It also has less reliance on external processes, and should be faster too! – Compo May 13 '18 at 11:23
0

Here's an alternative one line solution, (split over 3 to maintain max 80 character line lengths), which uses the Where command:

(Where/Q "file1.txt"&&(Where/Q "file2.txt"&&Start /D "folder" "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"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • This does not work. The first `ELSE` is executed when "file1.txt" exists _and_ "file2.txt" does not exists, but if "file1.txt" does not exists, then no other code is executed. In other words: the first `ELSE` belongs to the _second_ `IF` command; the first `IF` command have not any `ELSE` part. The same point apply to the "IF's" of "file3.txt" and "file5.txt". – Aacini May 12 '18 at 02:17
  • @Aacini, you are correct, it was a lazy answer, _I didn't look at the problem properly before posting_. I have completely revised the answer to use an alternative method. – Compo May 12 '18 at 12:56
  • I invite you to review the new method I posted at [my answer](https://stackoverflow.com/a/50302343/778560)... **`;)`** – Aacini May 12 '18 at 21:15