Checking File Existence
Checking whether a file or folder exists is a common way to verify preconditions in a batch file.
Adding an existence check before copying or deleting makes the cause of errors much easier to find.
In short, use if exist "path" to check for existence, and if not exist "path" when the condition should be that the target is missing.
if exist "C:\work\sample.txt" echo It exists
if not exist "C:\work\sample.txt" echo It does not exist
Basic Command
The if exist Command
Command Prompt has no dedicated command just for checking whether a file exists. Use the if exist syntax instead.
It checks whether the specified file or folder exists and runs a process if it does.
if exist "path" (
rem Processing when the file exists
)
A path may contain spaces, so it is safer to write if exist "%file_path%" with double quotes.
The following code checks whether sample.txt exists in the user’s Documents folder.
set "file_path=%USERPROFILE%\Documents\sample.txt"
if exist "%file_path%" (
echo The file exists.
) else (
echo The file does not exist.
)
The next code stores the result of the check in a variable.
set "file_path=%USERPROFILE%\Documents\sample.txt"
if exist "%file_path%" (
set file_exist=true
) else (
set file_exist=false
)
if %file_exist%==true (
echo The file exists.
) else (
echo The file does not exist.
)
Storing the result in a variable lets you separate the existence check from the processing that follows.
Always write ) else ( on one line. Putting a line break between ) and else causes a syntax error.
Checking Folders and Telling Them Apart from Files
if exist works for folders as well as files. Adding a trailing \ to the path makes the check true only for a folder.
if exist "%USERPROFILE%\Documents\" (
echo The Documents folder exists.
)
Conversely, to confirm that the target exists as a file and not as a folder of the same name, combine the two checks.
if exist "%file_path%" if not exist "%file_path%\" (
echo It exists as a file.
)
if not exist "output\" mkdir "output" creates the output folder only when it does not exist yet. See
for details.
Checking with Wildcards
With * or ?, you can check whether at least one matching file exists.
if exist "%USERPROFILE%\Documents\*.log" (
echo There are log files.
)
When the File Does Not Exist (if not exist)
Use if not exist to stop processing when a file is missing, or to create it.
if not exist "%file_path%" (
type nul > "%file_path%"
)
If a path contains ), as in C:\Program Files (x86), writing echo %file_path% inside an if (...) block can cause a syntax error. Quote the path as in echo "%file_path%", or avoid the parenthesized block and write if exist ... goto instead.
Practical Examples
Using It in a Batch File
The following batch file runs different processing depending on whether the file exists.
@echo off
setlocal
set "file_path=%USERPROFILE%\Documents\sample.txt"
if exist "%file_path%" (
echo "%file_path%" exists.
rem Processing when the file exists
) else (
echo "%file_path%" does not exist.
rem Processing when the file does not exist
)
pause
endlocal
For more about @echo off, see the following page.
Validation
When your processing assumes that certain files exist, validate them first.
The following code checks two files and continues only if both exist.
@echo off
setlocal
set "file1=%USERPROFILE%\Documents\file1.txt"
set "file2=%USERPROFILE%\Documents\file2.txt"
if not exist "%file1%" (
echo "%file1%" does not exist.
exit /b 1
)
if not exist "%file2%" (
echo "%file2%" does not exist.
exit /b 1
)
rem Processing when both files exist
pause
endlocal
If either file is missing, this script prints a message and ends with exit code 1. Exit codes are explained in
.Summary
- Use
if exist "path"to check whether a file or folder exists - Use
if not exist "path"when the condition is that it is missing - Wrap the path in double quotes to allow for spaces
- A trailing
\limits the check to folders - Write
) else (on one line