Batch Script to Create Folders: A Reusable Windows Template

A short batch file can create a whole set of folders in one double-click. Here is a reusable template and how to change its folder list.

Windows Command Prompt showing the HELP command list beside a PowerShell window listing get-command output on a blue desktop.
In this article

Yes. A batch file can create a whole set of folders in one double-click, so you can stop right-clicking, choosing New, then Folder, and typing a name every time. A batch script is a plain text file of commands that Windows runs one after another. [1] For more detail, see Batch File vs PowerShell to Create Folders.

That makes it a good fit for repeatable setups, such as the same project folders for every new client or school term. You write the list of names once, then reuse the file whenever you need that structure. The folder itself is made with the md (make directory) command. [1]

This article gives you a reusable template and shows how to adapt its folder list. It covers Windows 11 and notes where Windows 10 differs.

Key takeaways

  • A batch file is a text file of commands run in sequence.

  • One script can replace many manual folder-creation clicks.

  • Edit the folder list to reuse the same template for any project.

What do you need before you write the script?

You need only Notepad and a place where the folders should go. No extra software, downloads or admin tools are required.

What a batch script is

Batch scripts are stored in simple text files, with lines of commands that run in sequence, one after the other. [1] That is why Notepad is enough: you type the commands, save the file, and Windows runs them in order. The file extension is .bat or .cmd. [1]

The one command that matters

The md (make directory) command creates a folder. [1] It does the same job as mkdir, so you can use either one. [2]

If a folder name contains a space, wrap it in quotes. For example:

md "Project Files"

The quotes keep Project Files together as one name, so you get a single folder instead of two.

Pick your target location

Decide where the new folders should appear, such as a folder on your Documents drive or a project folder on an external drive. The script will use this path later as its base location, so choose one you can find easily.

VERSIONS

Windows 11 and Windows 10 use the same commands. Only the File Explorer menus for showing file extensions differ slightly, and that matters when you rename the file to end in .bat.

A laptop showing lines of code sits on a wooden desk beside a spiral notebook with handwriting, a pen, a cup of tea and flowers.

How do you create the batch file step by step?

You create the batch file in five short steps: make a text file, rename it to .bat, paste in the template, save, and double-click it. You may also find this useful: How to Create a Folder Inside Multiple Folders in Windows.

  1. Open the folder where you want to keep the script. Right-click an empty space, choose New, then Text Document. [1]

  2. Rename the file so it ends in .bat, for example MakeFolders.bat. Batch scripts are plain text files whose commands run one after another. [1]

  3. Open the file in Notepad and add the two variables at the top. BaseDir is where the folders will be created, and NewDirs is the list of names, each in quotes and separated by commas. [3]

set BaseDir=H:
set NewDirs="folder 1","folder 2","folder 3"
  1. Below those lines, add the part that does the work. The md command makes each folder, and the echo lines print Created: or *ERROR*: for each path. Then save the file. [3]

for /D %%A in (%BaseDir%\*.*) do (
md "%%~A\%%~B" >NUL 2>&1 && (
echo Created: "%%~A\%%~B"
) || (
echo *ERROR*: "%%~A\%%~B"
)
  1. Double-click the file. A command window opens and prints one line per folder, and the new folders appear in your base location.

Safety: test first

The script writes to whatever path BaseDir names. Run it first in a test location, such as a scratch folder you can delete, before pointing it at real data. Keep backups of anything important.

How do you avoid errors when a folder already exists?

Add an existence check before each md call, and the script will no longer fail when a folder is already there. The source shows the pattern in one line:

if not exist %folder% md %folder%

The line reads in plain English: if the folder does not exist, make it. [2]

Why this makes the script safe to rerun

The md command is the same as mkdir, and it creates a folder from the path you give it. [2] Without a check, running it against a name that already exists produces an error. With if not exist in front, the command only runs when the folder is missing, so existing folders are skipped quietly.

That means you can run the same file after adding a new name to your list. Only the new folder gets created, and the older ones are left as they are.

Use quotes when names contain spaces

Wrap any name that contains a space in double quotes, in both the check and the md call. Otherwise the command treats each word as a separate item. For example, with a folder named Project Files:

if not exist "Project Files" md "Project Files"

If you store the name in a variable such as %folder%, the same rule applies. Put the quotes around the variable too, like "%folder%", so names with spaces still work.

How do you change the folder list for your own projects?

You only need to change two lines to reuse the template: BaseDir (where the folders go) and NewDirs (what they are called).

Edit the NewDirs line

NewDirs holds a comma-separated list, and each name sits in its own quotes. Add, remove or rename entries, and keep the commas between them with no spaces. The template’s list looks like this:

set NewDirs="folder 1","folder 2","folder 3"

[3]

Change BaseDir

BaseDir is the parent location. The template points it at a drive, and you can swap in any drive or path:

set BaseDir=H:

[3]

Example: Office folders under a year folder

Set the two lines like this:

set BaseDir=H:\Temp Folder\2013
set NewDirs="Excel","Powerpoint","Word"

[3]

The path contains a space, so the example version of the loop wraps %BaseDir% in quotes:

for /D %%A in ("%BaseDir%\*.*") do (

[3]

Add the same list inside every existing subfolder

The for /D loop visits each subfolder of BaseDir, and the md line inside it builds the path from the subfolder (%%A) and each name from your list (%%B). That puts your whole list inside every folder that already exists there:

for /D %%A in (%BaseDir%\*.*) do (
md "%%~A\%%~B" >NUL 2>&1 && (
echo Created: "%%~A\%%~B"
) || (
echo *ERROR*: "%%~A\%%~B"

[3]

Each folder reports Created: on success or *ERROR*: on failure, so you can see what happened for every path. [3]

Can you run the script in any folder with one click?

Yes, with one caveat: a script placed in a folder can build its folders right next to itself, and a shortcut makes that a one-click job. Where the folders land depends on which folder the script is working in.

Creating folders alongside the script

If you save the batch file in a folder and run it there, it can create the folder structure in that same folder. This is the simplest setup, and it needs no path typed into the script. [4]

For example, drop the file into a new project folder, double-click it, and the subfolders appear beside it.

One-click use with a shortcut

A shortcut to the batch file lets you trigger it on demand, whether from the desktop or from a favorites link in File Explorer. [4] Right-click the .bat file, choose to create a shortcut, and move that shortcut wherever you want it.

Keep in mind where the result goes. A shortcut that launches the script created the structure only in the directory where the script itself lives, not in the folder you happened to be browsing. [4]

Sharing the script on a network

Placing one script on a shared drive is tempting, because users would not need their own copy. A person on a network might hope to browse to any directory on their machine, click a shortcut to the shared script, and get the structure in that directory. [4]

That is where the working directory causes trouble. Run through a shortcut, the script builds folders where it sits, so every user’s folders would pile up on the shared drive instead. [4]

If the script must serve several people, decide where the folders should go before you share it. Either give each person a copy kept in the target folder, or set an explicit target path in the script.

What mistakes break a folder-creating batch script?

Most broken folder scripts fail for one of four small reasons. Each is easy to fix once you know what to look for.

Saving as .bat.txt

When File Explorer hides extensions, a file you named folders.bat may really be folders.bat.txt. Double-clicking it then opens Notepad instead of running anything. Turn on file name extensions in File Explorer and rename the file so it ends in just .bat.

Forgetting quotes around names with spaces

Without quotes, md reads each word as a separate folder. Writing md Project Files makes two folders, not one. Write md "Project Files" instead, and keep quotes around each name in your list, as in "folder 1","folder 2".

Using a single % in loop variables

Inside a batch file, the for command needs double percent signs, so write %%a rather than %a. [2] A single percent sign works when you type the loop at the prompt, which is why a loop that worked there can fail once pasted into a .bat file. Loop variables are also case sensitive and must be a letter such as %%a, %%b or %%c. [2]

Building date-based names with %date%

A name built from slices of %date% depends on how your PC formats dates. One example builds a folder name with set folder=%date:~10,4%%date:~7,2%%date:~4,2%. [5] That slices fixed character positions, so it only produces a sensible year, month and day if the date string has the layout the author expected. On a PC with a different regional format, the same line can produce a scrambled or wrong name. Test any date-based line on the computer that will actually run it.

Your next step with a folder batch script

A folder batch script is a small template: set the variables, loop or use md lines, then run it. Once the file works, you can reuse it for every new project instead of clicking through New > Folder each time.

The only part you edit is the list. Change the set NewDirs= line, such as set NewDirs="Excel","Powerpoint","Word", and point set BaseDir= at the right path. [3]

For your next step, save a copy of the working script and adapt it to your next project. If your lists grow long, reading folder names from a text file is a related approach for larger jobs, and it is covered in its own article.

Sources

  1. Steps to Create a Batch File to Run Python Script Step 1: C

  2. Batch Script Create Folder If Not Exist With Code Examples

  3. Batch folder creation?

  4. Currently the way I’m doing it the shortcut makes the batch script create the directory structure…

  5. I am kind of new to Batch scripting, I am trying to write a script to create a folder which…