Windows 10

Batch File To Create a Folder

In this tutorial, you’ll learn how to create a folder in Windows using a batch file, but only if the folder does not already exist. This method uses the IF conditional statement along with the mkdir (make directory) command.

Batch files are powerful tools for automating system tasks, and checking for the existence of a folder before creating it is a common and useful practice.
 

✅ Batch File to Create a Folder If It Doesn’t Exist

The following example checks whether the folder C:\Folder exists and creates it if it doesn’t:

@echo off
if not exist "C:\Folder\" mkdir C:\Folder

🔍 Explanation:

   @echo off ➤ Hides the commands from being displayed in the Command Prompt window for a cleaner output.

   if not exist "C:\Folder\" ➤ This checks whether the folder C:\Folder\ exists.

   mkdir C:\Folder ➤ Creates the folder if it doesn’t already exist.

📝 Note: If the folder does exist, the script does nothing.

 

📂 Customizing the Folder Path

You can change the path to create a folder in a different location. For example:

@if not exist "D:\Projects\Logs" mkdir "D:\Projects\Logs"

This command ensures the Logs folder under D:\Projects is created only if it’s missing.
 

🧠 Use Case

This approach is useful in:

  • Scheduled tasks that set up log or backup folders
  • Installer scripts
  • Development environments that require specific folder structures

 

🧾 Summary

Creating a folder conditionally in a batch file is a simple yet essential trick for anyone automating tasks in Windows. Using if not exist with mkdir ensures that your scripts don’t throw errors or overwrite existing directories unnecessarily.
 

🚀 Boost your productivity with the best AI toolsTry them

 

Leave a Reply

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