How To Delete Files & Folders – From The Windows Command Line (CMD, DOS)
Easily delete files, folders & directories from the command prompt (cmd), with just a few simple commands in your current as well as different directories.
Welcome!
This guide is all about creating new files from the windows command line.
Lets get started!
Before we begin learning how to create files we must first learn how to change the location of our command line into to the location in which we want to create our new file.
For this example, let’s assume that we want to create our file in our desktop directory.
1) Open a command prompt without administrator privileges
By default, the command prompt is located at a folder within your users directory that’s named after your computers username (C:\Users\MyPC)
2) Use the “Dir” command to view every file and folder in the current directory.
Dir
The name of every file and folder as well some information about each of them will immediately appear.
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"
Your current working directory will immediately change.
And that’s it! You should now have navigated into the directory of your choice.
In case you made a mistake you can navigate to the previous directory 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 file.
Lets start with learning how to create files from our command line. To do so we will need to use the “Echo” command along with a redirector.
What exactly is echo and a redirector I hear you ask?
This might start to sound complicated, but don’t worry its actually pretty simple.
Do you see where I am going with this?
We can use the echo command to print a string and redirect its output into a new file by using a redirector, specifically the greater than redirector (>).
Lets see what that would look like:
Lets start by creating a new text file.
To do so all we have to do is type the echo command followed by the text we want our new file to have, the grater than redirector, and the name of your new file within quotes along with the .txt extension.
Echo Hello World > "File.txt"
You should see a new file in your current directory. Pretty simple right?
To view the contents of a file we just created directly from the command line we can use the “Type” command. Simply enter type followed by the name of the file we just created.
Type "File.txt"
The contents of the file will immediately be shown in the command prompt.
Apart from creating a new file we can add or append text to an existing text file. To do so we need to use the greater than symbol twice. Here is what that would look like:
Echo Goodbye World >> "File.txt"
Our file will now contain the following text:
Hello World
Goodbye World
In many cases you might want to crate a completely empty text file. The syntax for this command is very similar to our previous command however this time we need to add a dot directly after the echo command.
Echo. > "File.txt"
An empty file will immediately appear in your current directory.
In case you are wondering, the dot tells the echo command to not output anything.
While creating text files is nice, in may case you will want to create batch files. Doing so is very simple, all we have to do is replace the extension of the name of our new file from .txt to .bat.
Here is what that would look like:
Echo. > "Script.bat"
And a new batch file will immediately be created in your current directory.
In a similar way by using the extension of our choice we can create literally any type of file.
For example:
Awesome! Right?
You can embed the current date right into the name of the file you are creating by taking advantage of the %Date% automatic variable. Here is what our command would look like:
echo. > "%DATE:/=-%.txt"
This command will create a text file with the current date as its name in our current directory.
In my case the file will be named:
Mon 12-14-2020.txt
You might be wondering what /=- does.
By default, the date in the %Date% variable is stored in this format:
Mon 12/14/2020
However, to use this as the name of a file 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?
Using the windows command prompt you can create empty, dummy files with a specific size of your choice.
To do so we need to use the FSutil command instead of echo, which is what we have been using thus far.
FSutil stands for file system utility and as the name suggests it can be used to perform various operations on our file system.
But you don’t care about any of that, you just want to create a file.
To do so all you have to do is use the following syntax:
FSutil File CreateNew “Dummy File” 10485760
Confused? Lets break it down:
In this case a 10 MB file will be created.
Adjust the number of bytes to create a file with the size you want.
In case you want to convert gigabytes to bytes, use this online converter.
You’re welcome!
In all the above examples our new files will be created in the current directory.
In case you want to create your new file in a directory other than your current one, you need to enter your target directory just before the name of your new file.
So here is what our echo command would look like:
Echo. > "C:\Users\<Username>\Documents\File.txt"
This command will create an empty file in our documents directory.
To make it work for you replace <Username> with your computers username.
And here is what our FSutil command would look like:
FSutil File CreateNew “C:\Users\<Username>\Desktop\Dummy File” 10485760
This command will create a dummy file with a 10 MB size in our desktop directory.
Once again replace <Username> with your computers username.
In all the above examples we created only a single file. The beauty of the command line however is that you can complete complex, time consuming tasks in an instance, such as creating multiple files at once.
To do so, we need to use a for loop. For loops repeat an instruction a certain number of times.
Lets take a look at all the ways we can use a for loop to create multiple files:
Lets start with creating numbered files or, files for which only a number changes between their names.
Without further a do, here is what our command would look like:
FOR /L %A IN (1 1 10) DO (echo. > “File %A.txt”)
This command will create ten empty files in our current directory with the following names: File 1.txt, File 2.txt, File 3.txt, and so on…
This command might also look a little complicated, so let’s break it down:
Adjust the above command accordingly to suit your needs.
To create your files in a different directory, add the path of your choice, just before the name of your new files.
If you are unsure, here’s what that would look like:
FOR /L %A IN (1 1 10) DO (echo. > “C:\Users\\Desktop\File %A.txt”)
Replace with your computers username.
Also, 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.
Creating named files or, multiple files with different, specified names is quite similar.
Here is what that would look like:
FOR %A IN ("First", "Second", “Third”) DO (echo. > %A.txt)
The above command will create three files in your current directory with the following names: First.txt, Second.txt, Third.txt.
To make this command work for you, replace the names you want your files to have in the first parenthesis. And if necessary, replace, or adjust the second parenthesis with the command that will create your files the way you require.
The number of files that will be created depends entirely on how many file names you feed into the loop.
Once again to create your files in a different directory, add the target path just before the A parameter in the second parenthesis. Take a look at the above commands if you need a reference.
Finally, if you are planning on running this command from a batch script instead of the command line, you need to use two percentage signs instead of one so that your command is interpreted correctly.
You now know how to create many types of files 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.
This course has everything you need to start learning about the windows command line along with batch scripting.
Easily delete files, folders & directories from the command prompt (cmd), with just a few simple commands in your current as well as different directories.
A redirector or pipe is a special symbol that is used to redirect the input or output of a command or combine two or more commands in various ways.
You can use the find command to search for a file based on the contents inside it or to find where a string is located within a single or multiple files.
Learn the Windows command line And Become an Expert!