Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19253
  • 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?
0
ben1211
Asked:
ben1211
1 Solution
 
rindiCommented:
the following command in a batch file should do that:

cls >>file.txt
0
 
oBdACommented:
For a really empty file (0 bytes), use
echo. 2>EmptyFile.txt
0
 
LongbowCommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DaGo21Commented:
The simplest way is:

echo.> c:\empty.txt

However you will have a return, otherwise use the one from Longbow.
0
 
oBdACommented:
DaGo21,
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
0
 
ben1211Author Commented:
obda, could you explain what echo.2> does? Why the .2?


Thank You.

Ben
0
 
LongbowCommented:
Here you will find a list and some explanations :
http://www.robvanderwoude.com/redirection.html
0
 
oBdACommented:
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").
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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