How To Create Files & Folders – Using The Windows Command Line (CMD)
Navigating The Command Prompt
Before we begin learning how to create files or folders we must first learn how to change the location of our command line into to the location in which we want to create our file or folder. For example lets create a folder in our desktop directory. By default the command prompt is located at a folder within your users directory that’s named after your computers username (C:\Users\MyPC).
The directory in which the cmd is located its called the current working directory.
From here we can use the “Dir” command to view every file and folder in this directory.
The name of every file and folder as well some information about each of them will immediately appear. In the first two lines the single dot represents the current directory and the double dot, the parent directory. In between all the files we can see our desktop folder. To navigate there lets use the “CD” command. Which stands for “Change Directory”. Simply type “CD” followed by the name of the folder you want to navigate into within quotes.
Your current working directory will immediately change.
To navigate back to our parent directory type “CD” followed by two dots.
Like we said previously the double dots represent the parent directory.
Use the “Dir” and “CD” commands to navigate to any folder in your computer from the command line.
Now that we are located in the desktop lets create our folder.
Create a Folder Using Cmd
The command we are going to use is “Mkdir” witch stands for make directory.
Simply type “Mkdir” followed by the name you would like your folder to have.
Use “Dir” to verify that your folder was created correctly.
If the name of the folder you want to create contains a space you need to surround the name of your file in quotes. If you don’t the command prompt will only use the word before the space as the name of the file.
This happens because the command prompt needs to be able to differentiate between names and multiple parameters. The quotation marks tell the command line that the spaces separating the words do not constitute an additional parameter and are just a continuation of our folders name.
So the command would look like this:
Mkdir "My Pictures"
Create a File Using Cmd
We can use the command line to create any type of file. To do so we will need to use the “Echo” command.
Echo is primarily used to print variables or strings to the command line, similar to “print” or “Console.WriteLine” in other programming languages.
You can create multiple types of files using echo, however for this example we are going to create a text file because its the easiest to grasp.
Along with the “Echo” command we will need to use a redirector.
Redirectors are special symbols that can be used complete many function such as combine two commands or redirect the output of one command into input for another.
To create our file we need to use the Greater Than redirector. Which will redirect the output of the echo command, into a text file.
So to create a file type echo, followed by the text you want your file to have, the greater than symbol, and the name of your file along with its extension.
Echo Hello World > File.txt
A text file will immediately be created.
To view the contents of a file directly from the command line use the Type command. Simply enter type followed by the name of the file we just created.
The contents of the file will immediately be shown in the command prompt.
Using the greater than redirector will only create a new file.
To add text to an existing file we need to type the greater than symbol twice.
Echo Goodbye World >> File.txt
Our file will now contain the following text:
In some instances you might want to create an empty file. For that we are going to type echo, directly followed by a dot, the greater than symbol and the name of our file.
Echo. > File2.txt
An empty file will immediatelly be created.
Using the “Mkdir” and echo commands we can create folders or files in our current directory.
But what if we want to create a folder or file in a different directory without having to navigate there?
You would have to enter the location of the target directory followed by a backward slash and the name we want our folder to have, instead of typing the name of our folder or file directly.
So to create a folder in a different directory type the following:
mkdir "C:\Users\Julian\Documents\My Pictures".
As you can see we are once again using quotation marks, because the name of the folder we want to create contains a space.
In the same way you can replace “Mkdir” with any command to complete any task from any directory.
- Navigate to the directory you want to create a file into using the “CD” and “Dir” commands.
- Use “Mkdir” to create folders. Don’t forget to surround the name of your folder in quotes if it contains a space.
- Use echo along with the greater than redirector to create files.