I need to run a utility only if a certain file exists. How do I do this in Windows batch?
Asked
Active
Viewed 1.4M times
3 Answers
884
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
-
10If 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
-
3If 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 :( – mez.pahlan 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
-
1if 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
-
4One **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
97
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,899
- 9
- 48
- 67

Sheng Jiang 蒋晟
- 15,125
- 2
- 28
- 46
67
Try something like the following example, quoted from the output of IF /?
on Windows XP:
IF EXIST filename.txt ( del filename.txt ) ELSE ( echo filename.txt 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.
-
12
-
7I 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