• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19260
  • Last Modified:

Creating an empty Text File from a Batch File

How can I create an empty text file through a Batch file? What would the command or commands be to create an empty text file?
1 Solution
the following command in a batch file should do that:

cls >>file.txt
For a really empty file (0 bytes), use
echo. 2>EmptyFile.txt
When opened with notepad, the command with cls put a square in the .txt file.

The next command create a new empty .txt file
echo|find /I /V "echo">file.txt
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

The simplest way is:

echo.> c:\empty.txt

However you will have a return, otherwise use the one from Longbow.
unlike "echo.> c:\empty.txt", "echo. 2>EmptyFile.txt" will *not* create a file containing a CR LF, and unlike "echo|find /I /V "echo">file.txt", it doesn't call an external program.
echo. 2>EmptyFile.txt will redirect echo's stderr output, which (for the obvious reasons) is empty; as a result, as I said, a completely empty file will be created.
The line feed it produces can be suppressed, if necessary, by redirecting the standard output as well:
echo. >NUL 2>EmptyFile.txt
ben1211Author Commented:
obda, could you explain what echo.2> does? Why the .2?

Thank You.

Here you will find a list and some explanations :
In the NT command shell (often and incorrectly referred to as "DOS-Window"), you have two kinds of console output: stdout and stderr. By default, you'll see both in the console.
stdout is the standard output, stderr can be used by the program when printing error output.
Output to stdout can be redirected by the usual ">NUL" or ">SomeFile.txt" (or 1>NUL).
Output to stderr can be redirected by using a 2 in front of the redirection: "2>NUL".
For example, open a command prompt and enter (do not replace the xxx with a valid name), for example
net use X: \\xxxxx\xxxx >NUL
Even though you might expect the output to be redirected into the NUL device (">NUL"), you still see the error message--because it's written to stdout.
Now enter
net use X: \\xxxxx\xxxx 2>NUL
and you won't receive the error message anymore, because you redirected stderr ("2>NUL") this time.
Now to the empty file:
Any output you produce with "echo" will have a CR LF at the end; if you redirect this output into a file, you'll have the CR LF there as well.
echo doesn't write anything to stderr, so if you redirect the stderr output of an echo command, you have the completely empty output you need to create a really empty file. At the same moment, you can redirect the regular output (the CR LF) as well, as mentioned above:
echo. >NUL 2>EmptyFile.txt
Or you could use the echo to write something:
echo Creating an empty file ... 2>EmptyFile.txt
(Note that you have to leave a space in front of the "2>EmptyFile.txt").
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now