Creating an empty Text File from a Batch File

Posted on 2005-05-12
Last Modified: 2011-08-18
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?
Question by:ben1211
    LVL 87

    Expert Comment

    the following command in a batch file should do that:

    cls >>file.txt
    LVL 82

    Accepted Solution

    For a really empty file (0 bytes), use
    echo. 2>EmptyFile.txt
    LVL 10

    Expert Comment

    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
    LVL 4

    Expert Comment

    The simplest way is:

    echo.> c:\empty.txt

    However you will have a return, otherwise use the one from Longbow.
    LVL 82

    Expert Comment

    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

    Author Comment

    obda, could you explain what echo.2> does? Why the .2?

    Thank You.

    LVL 10

    Expert Comment

    Here you will find a list and some explanations :
    LVL 82

    Expert Comment

    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").

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
    In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now