How to create empty files from the command prompt (cmd)

How to create empty files from the command prompt (cmd): Well, sometimes you just need to create empty files in Windows for applications to work in a portable environment or to take advantage of the null files in another process. Whatever the reason might be, knowing how to create empty files from the command prompt will only be useful for you and will help you in understanding the system better.

Now PSIX-compatible systems have touch command which creates empty files but in Windows, there is no such command that’s why it’s more important to learn how to create one. You must be thinking why not create an empty file from notepad and save it, well it doesn’t actually an empty file that’s why this task is accomplished by using the command prompt(cmd).

How to create empty files from the command prompt (cmd)

1.Press Windows Key + X then select Command Prompt (Admin).

2.Type the following command and hit Enter: cd “C:\Your Directory”
Note: Replace your directory with the actual directory you need to work with.

3.To create empty file just type this command & hit Enter: copy nul “emptyfile.txt”
Note: Replace emptyfile.txt with the name of the file you need.

4.If the above command fails to create an empty file then try this one: copy /b NUL EmptyFile.txt

5.Now the problem with above command is that it will always display that the file was copied and in order to avoid that you can also try the following command: type NUL > 1.txt

6.If you really want a totally empty file, without any output to stdout then you can redirect stdout to nul:
copy nul file.txt > nul

7.Another alternative is that run aaa> empty_file which will create an empty in the current directory and then it will try to run the command aaa which is not a valid command & in this way you will create an empty file.

C:\Users\Aditya>cd Desktop

C:\Users\Aditya\Desktop>aaa >empty_file
'aaa' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Aditya\Desktop>dir
Direcotory of :\Users\Aditya\Desktop
09/06/2017 10:41 <DIR> .
09/06/2017 10:41 <DIR> ..
09/06/2017 10:41 0 empty_file

How to create empty files from the command prompt (cmd)

8.Also, you could write your own touch command:

#include <fstream>
#include <iostream>

int main(int argc, char ** argv;)
{
if(argc !=2)
{
std::cerr << "Must supply a filename" << endl;
return 1;
}
std::ofstream foo(argv[1]);
foo.close();
return 0;
}

7.Save the above file as touch.cpp and that’s it you have created a touch program.

Recommended for you:

That’s it you have successfully learned How to create empty files from the command prompt (cmd) but if you still have any queries regarding this guide then please feel free to ask them in the comment’s section.

Was this article helpful?

Related Articles

Leave A Comment?