Sometimes you have a bat script or you are just working in cmd and you need to create an empty file. Maybe as a placeholder for something or you want to create a file and append text to it later on. but how do you create a file in CMD?
There are multiple options to create an empty file or a file with text in command. In this article, we are going to look at the different options.
Create a File in CMD
Creating a file in command is basically done by redirecting some form of output to a file. Redirecting output is done with the
> operator. This means that we can, for example, redirect the text “test” to a new file with the following command:
echo test > filewithtext.txt
This creates a new text file, named filewithtext.txt with the word test in it.
Creating an Empty File in Command
It’s also possible to create an empty file in command. To do this we will be redirecting a NUL value to a new file. This way a new file is created with nothing in it. There are two ways to do this, we can copy the value of NUL to a new file. Or we can use the TYPE command, which will get the contents from a file, in this case, NUL, and redirect it to a new file.
copy NUL EmptyFile.txt
Note that we are not using the redirect operator here. The copy command file copy the contents from the NUL command to the file EmptyFile.txt. If the file doesn’t exist, it will be created.
The other option is to use the TYPE command:
type nul > emptyfile.txt
The command Type will open the contents of the given file in the command prompt, in this case,
NUL, and the redirect operator
> will redirect the results to the file emptyfile.txt.
There is even a shorter method to create a new empty file, but I don’t like to use it. The problem is that it will throw an error. The error can be ignored, the result is an empty file:
. > empty.txt
Create a File and open it
Besides just creating a new file, it’s also possible to create a new file and open it. This allows you to add text to it immediately. To do this we will be using the built-in notepad command:
This method also works for some other applications, for example, if you have Visual Studio installed, you can use the command
code followed by the file name to create and open a new file.
Creating a new file in command is pretty straightforward as you can see. To create an empty file we basically redirect a null output to a new file.
I hope this article helped you create a new file, if you have any questions, just drop a comment below.