3

Pseudo code:

if file exists:
do
    xxxx
done
else:
do
    xxxx
done
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Bin Chen
  • 54,865
  • 51
  • 136
  • 180
  • 1
    possible duplicate of [How to verify if a file exists in a Windows .BAT file?](http://stackoverflow.com/questions/3022176/how-to-verify-if-a-file-exists-in-a-windows-bat-file) and [How to check if a file exists from inside a batch file](http://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file) – Helen Jul 26 '11 at 14:55

2 Answers2

6

You can use exist:

if exist somefile.dat echo It exists

Depending on the "dialect", you can use else statements. At the lowest level, though, some rather ugly logic like this works:

if exist somefile.dat goto fileexists
echo file does not exist
goto alldone

:fileexists
echo file exists

:alldone
Mark Wilkins
  • 39,254
  • 5
  • 53
  • 106
4

Syntax is as follows:

IF [NOT] EXIST filename command

You can use the [NOT] option to execute code if a file doesn't exist as opposed to if the file does exist, however this can be done as an ELSE staement in a standard IF EXIST statement.

IF EXIST stuff.txt (
  ECHO It exists
) ELSE (
  ECHO It doesn't exist
)
Anthony Miller
  • 12,722
  • 27
  • 65
  • 96