How to check if a file exists from inside a batch file

sab picture sab · Dec 2, 2010 · Viewed 1.1M times · Source

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

Answer

Chris J picture Chris J · Dec 2, 2010
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