Checking File Existence

Maintained on

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.

Basic_usage_of_exist
if exist "path" (
  rem Processing when the file exists
)
Wrap the path in double quotes

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.

Example_of_if_exist
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.

Storing_the_result_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.

Keep else on the same line as the closing parenthesis

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.

Checking_that_a_folder_exists
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.

Confirming_that_the_target_is_a_file
if exist "%file_path%" if not exist "%file_path%\" (
    echo It exists as a file.
)
Create the folder if it is missing

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.

Checking_for_log_files
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.

Create_the_file_if_it_does_not_exist
if not exist "%file_path%" (
    type nul > "%file_path%"
)
Echoing a path that contains parentheses

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

Practice Questions

Practice Problem

Which command prints a message when the file does not exist?

Choose one answer.

  1. if exist "%file%" echo missing
  2. if not exist "%file%" echo missing
  3. if exists "%file%" echo missing

Practice Problem

Which is the most appropriate way to check only whether a folder exists?

Choose one answer.

  1. if exist "%dir%"
  2. if exist "%dir%\"
  3. if folder "%dir%"
#Command Prompt #Batch File #Conditionals #if exist #File Operations