[windows] How to create an empty file at the command line in Windows?

How to create an empty file at the DOS/Windows command-line?

I tried:

copy nul > file.txt

but it always displays that a file was copied.

Is there any other method in the standard cmd?

It should be a method that does not require the touch command from Cygwin or any other nonstandard commands. The command needs to run from a script so keystrokes cannot be used.

This question is related to windows file cmd command-line

The answer is


call>file.txt

this is the cleanest way I know.


Reading comments on my post, I have to admit I didn't read the question right.

On the Windows command-line, one way would be to use fsutil:

fsutil file createnew <filename> <size>

An example:

fsutil file createnew myEmptyFile.txt 0

Below is for *nix command-line.

touch filename

This command changes your modified date of a file or creates it if file is not found.


copy con SomeFile.txt Enter

Ctrl-Z Enter


Try this:

type NUL > 1.txt

this will definitely create an empty file.


For creating any type of file you can use the following code

type nul > (file_name).(file_type)

Eg. If you want to create a text file then

type nul > demo.txt

If you want to create a javascript file then

type nul > demo.js


Use copy > your_file_name.extension in command prompt like

P:\excecise> copy > Sample.txt

Open file :

type file.txt

New file :

Way 1 : type nul > file.txt
Way 2 : echo This is a sample text file > sample.txt
Way 3 : notepad myfile.txt <press enter>

Edit content:

notepad file.txt

Copy

copy file1.txt file1Copy.txt

Rename

rename file1.txt file1_rename.txt

Delete file :

del file.txt

Here is yet another way:

rem/ > file.ext

The slash / is mandatory; without it the redirection part was commented out by rem.


On Windows I tried doing this

echo off > fff1.txt

and it created a file named fff1.txt with file size of 0kb

I didn't find any commands other than this that could create a empty file.

Note: You have to be in the directory you wish to create the file.


.>>file.txt
  • >> append STDOUT into a file
  • . is just a wrong command to pass the empty STDOUT to >>

However, you'll see STDERR's output in the CMD:

'.' is not recognized as an internal or external command, operable program or batch file.

You can suppress this error message (if you want) by redirecting STDERR to NUL.

.>>file.txt 2>nul

type nul>filename will create a new empty file.

Sorry I'm late.

UPDATE: Also copy nul filename works without redirecting (more obvious solution).


You can write your own touch.

//touch.cpp
#include <fstream>
#include <iostream>

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

Yet another way:

copy nul 2>empty_file.txt

You can use the old command

copy con file_name.ext

don't type anything, just press F6 to save it, however it will print "File copied", but when you open the file, it will be empty


Here's another way:

cd. > filename

Just I have tried in windows

copy con file.txt

then Press Enter Key then Press Ctrl+Z Enter

And its worked for me.

For Ubuntu usually I am creating a file using VI command

vi file.txt

It will open the file then press ESC key then type :wp then press enter key. It will create a new file with empty data.


cd > filename.cfg 

worked when creating a file in C:/Program Files where you don't have the access to create files directly.


As a VIM user on windows, I failed to find the answer in this question thread.

As a solution from this thread, please install the gvim firstly.
During the installation, please remember to check the option "Create .bat files for command line use" as shown below.

enter image description here

Some bat files like vim.bat and gvim.bat will be installed under C:\Windows. Add C:\Windows to system Path if it's not done.

Relaunch the cmd.exe and type gvim empty_file.txt, you will launch the gvim to edit the empty file from scratch. If you don't want to leave your "Command Prompt" console, type vim empty_file.txt instead.

Hope this answer can help those who want to launch VIM from windows Command Prompt when creating an empty file.


Today I've discovered a new one :)

This will change the command line window title, but will also create a empty file.

title > file.txt

Yet another method that creates a zero byte file:

break > "file.txt"

Try this :abc > myFile.txt First, it will create a file with name myFile.txt in present working directory (in command prompt). Then it will run the command abc which is not a valid command. In this way, you have gotten a new empty file with the name myFile.txt.


First create your file so that it exists:

echo . > myfile.txt

Then overwrite the created file with an empty version using the copy command:

copy /y nul myfile.txt

You could also use:

echo. 2>foo

The debug output for echo. will almost definitely be empty.


  • create a bat file with content echo '' > %1. (name the file as touch.bat)
  • add the folder to PATH variable.
  • you can use touch to create files. (Ex: touch temp.txt creates temp.txt file)

check this article for more information.


If you really want a totally empty file, without any output to stdout, you can cheat a little:

copy nul file.txt > nul

Just redirect stdout to nul, and the output from copy disappears.


This worked for me,

echo > file.extension

Here's another way I found today, got ideas from other answers but it worked

sometext > filename.extension

Eg.

xyz > emptyfile.txt  //this would create an empty zero byte text file
abc > filename.mp4   //this would create an zero byte MP4 video media file

This would show an error message in the command prompt that ,

xyz is not as an internal or external command, operable program or batch file.

But the weird thing I found was the file is being created in the directory even if the command is not a standard windows command.


echo.|set /p=>file

echo. suppress the "Command ECHO activated"

|set /p= prevent newline (and file is now 0 byte)


so you can create an empty file with

'' > newfile.txt

navigate to the directory and type the above command in PowerShell window.

Note this will not work on windows command prompt.


echo "" > filename

I believe this works on Windows/DOS, but my last hands-on experience with either is quite a while ago. I do know for a fact that it works on basically any POSIX compliant OS.


I read many threads but it is not the shortest way.

Please use command:

>copy /b NUL empty_file.txt


Try this:

echo $null >> filename 

See: superUser


Run CMD in admistrator mode and type this:

NUL > file_name.extention

or you type this

echo .> file_name.extention

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to cmd

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH VSCode Change Default Terminal How to install pandas from pip on windows cmd? 'ls' in CMD on Windows is not recognized Command to run a .bat file VMware Workstation and Device/Credential Guard are not compatible How do I kill the process currently using a port on localhost in Windows? how to run python files in windows command prompt?

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)