Windows 10

Batch File To Check If File Exists

In this tutorial, we are going to see how to check if file exists in a batch file by using IF EXIST condition. Batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

Batch File To Check If File Exists

The following example check if “filename.txt” exists:

@echo off 

IF EXIST "filename.txt" (
    echo 'File EXIST!'
) ELSE (
    echo 'File missing!'
)
 
Output:

 

Example 2:

The following example EXIT if the required file “filename.txt” is missing:

@Echo Off
IF NOT EXIST filename.txt EXIT /b
Echo "File Exists!"

 

5 thoughts on “Batch File To Check If File Exists

  • Tim Gillis

    Wow, this “tutorial” is so very inadequate. The command checks to see if the file exists. But where? Anywhere on the computer? Only the current drive? Only the current directory? Don’t you think that bit of information would be helpful?

    Reply
    • Yep, we are waiting for your solution then

      Reply
  • just replace filename.txt with drive\folder\subfolder\filename.ext

    e.g the file you’re checking for should be something like c:\tmp\folder1\subfolder1\something.txt
    then the code would be :-

    @echo off 
    IF EXIST "c:\tmp\folder1\subfolder1\something.txt" (
        echo 'File EXIST!'
    ) ELSE (
        echo 'File missing!'
    )
    Reply
  • chandru

    Hi,

    I have files with names

    “windows10.0-kb123456-v2-x64_fa90a4bdc1da0f5758cdfa53c58187d9fc894fa0.msu”
    “windows10.0-kb987654-v2-x64_fa90a4bdc1da0f5758cdfa53c58187d9fc894fa0.msu”
    “windows10.0-kb167890-v2-x64_fa90a4bdc1da0f5758cdfa53c58187d9fc894fa0.msu”
    “windows10.0-kb235684-v2-x64_fa90a4bdc1da0f5758cdfa53c58187d9fc894fa0.msu”
    “windows10.0-kb654321-v2-x64_fa90a4bdc1da0f5758cdfa53c58187d9fc894fa0.msu”

    I want to filter a file which is having “kb123456” in the above list and want to display it as an output, may I know how to achieve this using batch script

    Reply
  • So how do I check if the file name is in a variable to test if the file exists?

    Reply

Leave a Reply

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