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
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.
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.
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.
echo '' > %1
. (name the file as touch.bat)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
Run CMD in admistrator mode and type this:
NUL > file_name.extention
or you type this
echo .> file_name.extention
Source: Stackoverflow.com