498

I need to run a utility only if a certain file exists. How do I do this in Windows batch?

sorin
  • 137,198
  • 150
  • 472
  • 707
sab
  • 8,917
  • 11
  • 38
  • 48

3 Answers3

797
if exist <insert file name here> (
    rem file exists
) else (
    rem file doesn't exist
)

Or on a single line (if only a single action needs to occur):

if exist <insert file name here> <action>

for example, this opens notepad on autoexec.bat, if the file exists:

if exist c:\autoexec.bat notepad c:\autoexec.bat
waghso
  • 613
  • 7
  • 20
Chris J
  • 28,355
  • 4
  • 62
  • 105
  • 10
    If you are dealing with paths with spaces: http://stackoverflow.com/questions/138981/how-do-i-test-if-a-file-is-a-directory-in-a-batch-script – Nick Dec 24 '13 at 23:17
  • 5
    @loopkin - `else` is valid, see "if /?" ... "The ELSE clause must occur on the same line as the command after the IF. For example: [...numerous examples of use...]" – Chris J Apr 15 '14 at 18:21
  • 4
    @chris-j Thanks Chris, you're correct, it seems like the parenthesis have to be on the same line as the else. That's what I was doing wrong. I think I'll never get used to the batch syntax :( – scharette May 24 '14 at 15:44
  • 3
    If you're a n00b like me and forget to replace the squiggly brackets too then this won't work. So be sure to remove {} when you {insert file name here}!! Spent an embarrassing 5 minutes realising this :( – piratemurray Jun 06 '14 at 09:10
  • Is this new? I wasn't aware of this syntax until now. However, it works fine on my Windows 10 machine. – Martin Hansen Nov 27 '15 at 20:48
  • @Martin - the extended 'if' syntax has been available since XP I believe. – Chris J Nov 30 '15 at 09:51
  • 1
    if you are running as an administrator your current directory can be reset to system32.. use these lines; \@setlocal enableextensions \@cd /d "%~dp0" – PodTech.io Jan 15 '16 at 11:09
  • 3
    One **caveat** of `IF EXIST` construct: It cannot detect Hidden files (files with Hidden attribute). – Explorer09 Aug 13 '17 at 16:03
  • I have Win 10 and first if with parentheses does not work. – Hrvoje Batrnek Nov 22 '18 at 02:54
  • This tells you if the target exists, so it could be a file or it could be a directory. It doesn't tell you if the file exists (i.e. if it's a directory, it's not a file) – John Rocha May 06 '21 at 22:54
90
C:\>help if

Performs conditional processing in batch programs.

IF [NOT] ERRORLEVEL number command

IF [NOT] string1==string2 command

IF [NOT] EXIST filename command

Sk8erPeter
  • 6,392
  • 9
  • 44
  • 67
Sheng Jiang 蒋晟
  • 14,859
  • 2
  • 26
  • 44
46

Try something like the following example, quoted from the output of IF /? on Windows XP:

IF EXIST filename. (
    del filename.
) ELSE (
    echo filename. missing.
)

You can also check for a missing file with IF NOT EXIST.

The IF command is quite powerful. The output of IF /? will reward careful reading. For that matter, try the /? option on many of the other built-in commands for lots of hidden gems.  

Alex Weitz
  • 2,451
  • 2
  • 24
  • 41
RBerteig
  • 38,361
  • 7
  • 80
  • 122
  • 12
    Why the '.' at the end of filename? Is it a typo? – Everyone Jun 04 '13 at 17:30
  • 7
    I quoted the help text from the actual `IF` command built in to CMD.EXE, which had those dots. I don't know why they included them, it does seem inconsistent. Of course, what the `EXIST` keyword actually needs is a valid file name, which may be fully qualified. Other commands use the idiom `[drive:][path]filename` in place of the text `filename.` use here, which is obviously clearer. – RBerteig Jun 04 '13 at 17:36