How To Create Folders/Directories – From The Command Line (The Easy Way!) – Windows, CMD

What We'll Learn:

Welcome!
This guide is all about creating new folders or directories from the windows command line.

  • We’ll start by learning, how to navigate to the location in which we want to create our new folder directly from the command prompt.
  • How to use a simple if statement to check whether a folder/directory with the same name already exists.
  • How the mkdir command works, and how we can use it to create many types of folders/directories.
  • How to embed the current date into the name of our new folders.
  • And finally, we’ll learn how to use the mkdir command or a for loop to create multiple folders at once, with a single command.

Lets get started!

Navigating to where we want to create our folder:

Before we begin learning how to create folders we must first learn how to change the current working directory, or in other words, the location of our command prompt, into to the location in which we want to create our new folder.

If you want to create your folder in our current directory, simply skip this step.

For this example, let’s assume that we want to create a new directory in your desktop.

 

1) Open your command line (without administrator privileges)

By default, it should be located at your users directory, usually: “C:\Users\<Username>“. In your <Username> case will be your computers username.

Cmd Create Files - Corrent Working Directory

2) Use the “Dir” command to view every file and folder in the current directory.

Dir

From all the files and folders that appear locate the “Desktop” directory.

Cmd Create Files - Dir Command Output

3) Navigate to the directory of your choice using the “CD” command.

“CD” stands for “Change Directory”. Remember to surround the name of the directory you want to navigate into within quotes “”.

Cd "Desktop"

And that’s it! You should now have navigated into the directory of your choice.

 

In case you made a mistake you can navigate one directory backwards by typing Cd followed by two dots.

Cd ..

The double dots represent the parent directory.

Now that we are located in the correct directory lets create our new folder/directory.

Checking if a folder/directory already exists:

Before creating a new directory its always a clever idea to check if a folder with the same name already exists in your target directory. This is especially important with folders that contain files within them, because you might accidentally overwrite all of them and lose important data.

You don’t need to worry however, all we have to do is use a simple if statement that checks whether a folder with a specified name already exists, and prints an appropriate message.

Here is what that would look like:

If Exist “My New Folder” (echo Already Exists) else (echo Doesn’t exist)

To make this command work for you replace “My New Folder” with the name of the directory you want to create within quotes.

 

If you want to learn how it works, lets break it down:

  • If statements are used to execute one out of two commands, depending on a specific condition.
  • “Exist” is a parameter, and as the name implies it returns true when a specified file/folder is found and false when it is not.
  • Afterwards we have the name of the folder we want to create within quotes. Quotes are especially important if the name of the folder/directory you want t create contains spaces within it.
  • Then, within parentheses, we have the first echo command which simply prints “Already Exists” into our console only when our condition is true.
  • And finally, if our condition is false, the code within our else statement is executed and the echo command prints “Doesn’t Exist” into our console.

Pretty simple, isn’t it?

Create Folder/Directory – From CMD:

After all that we are finally ready to create a new folder!

Thankfully, doing so is extremely simple. Thanks to the mkdir command, which stands for make directory.

If you’ve ever used the command line before, you might know an alias for this command, “MD”. Both MD and MkDir are exactly the same thing and work in exactly the same way.

Without further a do, lets create our first folder. Here is the command we are going to use:

Mkdir "Cats"

This command will create a folder named “Cats” in your current directory.

To make the above command work for you change “Cats” with the name of the folder you want to create.

 

Create folders with spaces within their names:

You might be wondering why we surrounded the name of our new folder within quotes.

Surrounding paths and names in quotes is generally the recommended practice, however in this case they are not strictly necessary.

That’s because, quotes are used to tell the command prompt that spaces separating the names of our folders do not constitute an additional parameter or an additional value and are just a continuation of a name or path.

So, if the name of the folder you want to create contains a space within it, i.e “My Folder”, you need to surround it in quotes. Otherwise, the command prompt might misinterpret your command or display an error.

Mkdir "My Cat Pics"

Creating Folders With The Current Date - Using CMD:

You can embed the current date right into the name of the folder you are creating by taking advantage of the %Date% automatic variable. As the name suggests, this variable simply stores the current date.

Here is what our command would look like:

mkdir "%DATE:/=-%"

This command will create a folder with the current date as the name, in our current directory.

In my case the new directory will be named:

Sun 01-24-2021

You might be wondering what /=- does. By default, the date in the %Date% variable is stored in this format:

Sun 01/24/2021

However, to use this as the name of a folder we need to replace the backward slashes with any other symbol, so that the command line doesn’t misinterpret our date as a directory. That is exactly what /=-  does, you can replace the dash with any symbol of your choice.

Awesome! Right?

Create multiple folders at once - Using CMD:

In all the above examples we created only a single folder. However, the beauty of the command line is that you can complete complex, time consuming tasks in only an instance, such as creating multiple directories at once.

Lets take a look at all the ways we can use the command prompt (CMD) to create multiple folders:

 

Named Folders (First, Second, Third):

Creating named folders or, in other words, multiple folders with different, specified names, is very easy.

This function is built right into the mkdir command. So, all we have to do is type the names of the folders we want to create one after the other.

Here is what that would look like:

Mkdir "First" "Second" "Third"

The above command will create three folders in your current directory with the following names: First, Second, and Third.

To make this command work for you, simply replace “First” “Second” and “Third” with the names you want your folders to have. The names of the folders do not need to be separated with comas or semicolons, a single space will do just fine!

You can create as many folders you want simply by specifying their names.

The sky is the limit!


To create your folders in a different directory, add the target path just before the name of each folder.

If you are unsure, here’s what that would look like:

Mkdir " C:\Users\<Username>\Desktop\First Folder" "C:\Users\<Username>\Documents\Second Folder"

Replace <Username> with your computers username.

The above command will create a folder named “First Folder” in the desktop directory and a second folder named “Second Folder” in the documents directory!

Easy as pie!

 

Numbered Folders (File1, File2, File3):

Apart from crating named folders, we can use the command line to create numbered files or in other words, files for which only a number changes within their names.

To do so, we need to use a For Loop. For loops simply repeat a specified instruction a certain number of times.

Without further a do, here is what our command would look like:

FOR /L %A IN (1 1 10) DO (Mkdir “Folder (%A)”)

This command will create 10 new folders in our current directory with the following names: Folder (1), Folder (2), Folder (3), and so on…

This command might also look a little complicated, so let’s break it down:

  • The /L parameter instructs the for command to iterate through the numbers in our parentheses, instead of using them directly as the names of our new folders.
  • Our A parameter specifies a single letter replaceable parameter. You can replace A with any letter of your choice. This is somewhat similar to a regular variable, in the sense that its value changes.
  • After that we have our condition, the first number specifies the starting number, the second number is the step, and the third number is the ending number.
  • And finally, in parenthesis we have the command that will be executed every time the loop is executed. In this case the mkdir command is executed and it creates our folders.

Adjust the above command accordingly to suit your needs.


Once again, if you want to create your new folders in a different directory, add the path of your choice, just before their names.

If you are unsure, here’s what that would look like:

FOR /L %A IN (1 1 10) DO (Mkdir “C:\Users\<Username>\Desktop\Folder (%A)”)

Replace <Username> with your computers username.

Finally, if you are planning on running any of these commands from a batch script you need to use two percentage signs instead of one, so that they are interpreted correctly.

Summary:

  • Navigate into the directory you want to create your new folder, using the “CD” and “Dir” commands.
  • Check whether a folder with the same name already exists, before creating it, so that you do not accidentally overwrite it.
  • Use the mkdir command to create your new folder. Don’t forget to surround its name within quotes!
  • Take advantage of the %Date% automatic variable and embed the current date into the names of your files.
  • Enter a path before the name of your new folder to create it in any directory.
  • Use the mkdir command to create multiple named folders or a simple For loop to create multiple numbered folders.

That's It!

You now know how to create many types of folders directly from the command prompt.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.

Dont Stop Learning!

Windows Command Line Course!

This course has everything you need to start learning about the windows command line along with batch scripting.

Read More:

Any Questions? Ask them Below:

Leave a Reply

Your email address will not be published.

Take Your Skills to the next level!
Become one of our beloved students by enrolling into one of our courses. Get started now!

FREE Command Line Course!

Learn the Windows command line And Become an Expert!