# Solved: Batch File If Statment



## computerman29642 (Dec 4, 2007)

I have the following code in a Batch File:


```
@echo off
set filepath=C:\

For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%test*.xls"`) do start /I excel.exe "%filepath%%%I"
```
Is there a way to add an If-Else-Statment that if the file does not exist, then open this file instead?


----------



## TheOutcaste (Aug 8, 2007)

So if there are no files named *test*.xls*, you want to open "*otherfile.xls*" ?

This should do it:

```
@echo off
set filepath=C:\

For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%test*.xls"`) do (
If /I "%%I"=="file not found" start /I excel.exe "%filepath%otherfile.xls" Else start /I excel.exe "%filepath%%%I"
)
```
HTH

Jerry


----------



## computerman29642 (Dec 4, 2007)

Thanks Jerry. I will test the code you provided.


----------



## computerman29642 (Dec 4, 2007)

Jerry, I tested you code. When I double-click the Batch File, the command prompt flashes on the screen but the excel file does not open.


----------



## TheOutcaste (Aug 8, 2007)

Guess I should have actually tested it. Seems there is something about the parentheses it doesn't like. Also discovered the *File not found* message is sent to STDERR so the loop variable never gets set.
This should do the trick though, at least it worked for me this time


```
@echo off
set filepath=C:\
For /f "usebackq tokens=*" %%I in (`dir /b /a-d "%filepath%test*.xls"^|^|Echo otherfile.xls`) do start /I excel.exe "%filepath%%%I"
```
This uses the conditional *||*
The Echo command will execute only if the dir command fails, setting the loop variable to otherfile.xls (you can use a variable here if you wish).
The carets are needed to escape the | characters because the command is actually being passed to a child process to execute.

Jerry


----------



## computerman29642 (Dec 4, 2007)

Jerry, The Batch File works now. Thank you very much.



> This uses the conditional ||
> The Echo command will execute only if the dir command fails, setting the loop variable to otherfile.xls (you can use a variable here if you wish).
> The carets are needed to escape the | characters because the command is actually being passed to a child process to execute.


I am a little confused on your explanation.


----------



## TheOutcaste (Aug 8, 2007)

There are several special symbols used with commands:
The Pipe *|*
Command1 *|* Command2
Sends the output from Command1 to Command2
The combine symbol *&*
Command1 *&* Command2
Execute Command1 then execute Command2
Conditional combine if successful
Command1 *&&* Command2
Execute Command1 then _only_ if Command1 was successful execute Command2
Conditional combine if fails
Command1 *||* Command2
Execute Command1 then _only_ if Command1 fails execute Command2

So the For loop is doing this command :
*dir /b /a-d "%filepath%test*.xls" || Echo otherfile.xls*

The Echo command is executed only if the dir command fails (File not found is considered a failure).

As far as the caret, it's just the way the For command works, any *|* symbols have to be written as *^|*

HTH

Jerry


----------



## computerman29642 (Dec 4, 2007)

Thanks Jerry for the explanation.


----------

