Windows 10

Batch File To Check If Multiple Files Exist

In this tutorial, you’ll learn how to check the existence of multiple files in a Windows environment using a batch file. This is useful for verifying required files before proceeding with installation, backup, or deployment scripts.

Batch files allow you to run a sequence of DOS (Disk Operating System) commands in automation scripts.
 
 

✅ Solution 1: Use Multiple IF EXIST Statements

This method checks each file individually using nested IF EXIST conditions.

@echo off
if exist "file1.txt" if exist "file2.txt" echo "file1.txt" and "file2.txt" exist!
if not exist "file1.txt" echo "file1.txt" is missing
if not exist "file2.txt" echo "file2.txt" is missing

🧪 Output:


📌 Explanation:

  • The first line checks if both files exist and echoes a success message.
  • The following lines check each file individually and echo which one is missing if needed.

 
 

🔁 Solution 2: Use a FOR Loop for Scalability

This method is cleaner and easily extendable to many files.

@echo off
for %%i in ("file1.txt" "file2.txt") do (
    if not exist "%%i" (
        echo %%i is missing
        goto :EOF
    )
)
echo All files exist!

🧪 Output:


📌 Explanation:

  • Loops over the list of files.
  • If any file is missing, it prints the missing file and exits the script.
  • If none are missing, it confirms that all files exist.

 

Leave a Reply

Your email address will not be published. Required fields are marked *