Windows 10

Batch File To Check If Folder Exists

In this tutorial, you will learn how to check if a folder exists in a Windows batch file using the IF EXIST condition. A batch file contains a series of DOS (Disk Operating System) commands that can be executed automatically.

This check is useful when you want your script to behave differently depending on whether a specific folder is present or not.
 

🧪 Batch File to Check If a Folder Exists

The following example checks if the folder C:\Users\StackHowTo\myFolders exists:

@echo off
if exist "C:\Users\StackHowTo\myFolders" (
  echo Exist 
) else (
  echo Does not exist
)

📤 Output:

Exist

If the folder exists, it will output “Exist”, otherwise it will display “Does not exist”.
 

✅ Use Case Examples

Create folder only if it doesn’t exist:

if not exist "C:\MyFolder" mkdir "C:\MyFolder"

Delete folder only if it exists:

if exist "C:\MyFolder" rmdir /S /Q "C:\MyFolder"

 

🧠 Quick Tip
  • Use IF EXIST for files and folders
  • Use double quotes around the path if it contains spaces

 

📘 Summary

Checking for folder existence in a batch file allows you to create flexible, error-proof, and efficient scripts for automation and file management on Windows.
 

🚀 Boost your productivity with the best AI tools → Try them

 

Leave a Reply

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