# Solved: Conditional Statements Question for Batch Files



## etheodos (May 5, 2009)

Hello, 

I am writing a batch file in Windows Vista to back up certain files. I have struggled through most of the code, this being my first time ever writing a batch file longer than about 5 lines, but have come to a grinding halt when I attempt to have multiple conditions in an if statement. I am not entirely sure that it is even possible, since this is its first appearance in my code, or if I just have the formatting wrong. I am simply trying to get my code to exit if the statement meets both requirements, which is if the two source directories do not exist.

The faulty line of code looks like this...

IF NOT EXIST "%SOURCE_1%" & "%SOURCE_2%" EXIT

Everything else in the code works, so hopefully this is just a noob mistake. Any help would be very gratefully met.

Thank You


----------



## Squashman (Apr 4, 2003)

Correct! You cannot do conditional statement like that. So what I do is set flag variables.

```
IF NOT EXIST "%SOURCE_1%" set _Flag1=Y
IF NOT EXIST "%SOURCE_2%" set _Flag2=Y
IF %_Flag1%%_Flag2%=="YY" GOTO :EOF
```


----------



## etheodos (May 5, 2009)

Wow! Thank you very much, that did the trick nicely.


----------



## etheodos (May 5, 2009)

Wait, no, sorry, I reinstated the sources after it seemed to work, and now the code just terminates after the third line of code that you have written here. I copied the code over exactly, and although the first two lines work (the two variables are initiated fine) the code terminates on the third line. I don't understand, everything seems to match up, and your technique makes perfect sense. I have a pause directly after the third line to check though, and I don't know why, but the code never passes that point if either or both of the sources exist.


----------



## etheodos (May 5, 2009)

I just turned echo on and checked. That line outputs...

IF Y == YY GOTO :EOF

which does not make any sense to me, is there something I am missing?


----------



## etheodos (May 5, 2009)

Alright, sorry to keep rambling, but it seems to be working with everything except when both sources exist. Apparently that is when the third line of code seems to terminate the script, as well as when it is supposed to.


----------



## TheOutcaste (Aug 8, 2007)

Couple of missing quotes:


```
IF NOT EXIST "%SOURCE_1%" set _Flag1=Y
IF NOT EXIST "%SOURCE_2%" set _Flag2=Y
IF [COLOR=Red][B]"[/B][/COLOR]%_Flag1%%_Flag2%[COLOR=Red][B]"[/B][/COLOR]=="YY" GOTO :EOF
```
Without those quotes, if both folders exist, it will cause an error and the script will stop.

That will exit only if BOTH source folders do NOT exist. If either folder exists, one of the _FlagX variables will not get set, so you'll end up with this on line three:
If "Y"=="YY" Goto :EOF which will not exit, but continue on to line 4.
You also need to make sure that the two _FlagX variables are cleared, and not using a value from a previous run.
So this would be even better:

```
Set _Flag1=
Set _Flag2=
IF NOT EXIST "%SOURCE_1%" set _Flag1=Y
IF NOT EXIST "%SOURCE_2%" set _Flag2=Y
IF [COLOR=Red][B]"[/B][/COLOR]%_Flag1%%_Flag2%[COLOR=Red][B]"[/B][/COLOR]=="YY" GOTO :EOF
```
HTH

Jerry


----------



## etheodos (May 5, 2009)

Okay, that definitely fixed it, everything is working perfectly now. Thank you for the help, both of you. This forum is a life saver.


----------



## Squashman (Apr 4, 2003)

Dang quotes get me everytime. Had them every where else.
I shouldn't ever assume that people need to initialize there variables.
Thanks for catching that!


----------

