Creating a blank file from a batch file

28,959

Solution 1

You just want to create a completely empty file? This will do it:

copy /y nul donald.txt

If you're looking to write something to first line, you want something like this:

echo Hello, World > donald.txt

Solution 2

One of the options is

@echo off
rem Don't destroy an existing file
if exist testfile goto _nocreate
:: Create the zero-byte file
type nul>testfile
:_nocreate

Originally from http://www.netikka.net/tsneti/info/tscmd041.htm which contains some additional methods.

Solution 3

In old MS-DOS days I used this method to create an empty file:

rem > empty.txt

In new Windows this no longer works, but rem may be replaced by any command that show nothing. For example:

cd . > empty.txt
Share:
28,959
Admin
Author by

Admin

Updated on July 19, 2020

Comments

  • Admin
    Admin almost 4 years

    I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial blank line. What else I can use to a file starting from the first line?

  • Rich
    Rich over 13 years
    Urgh, that's an evil way of defining a touch command. Remember that touch is not only used for creating empty files, but also for updating the timestamp of an existing one. Better not use a name that has multiple uses already ;-). In keeping with PowerShell terminology, this should probably be called New-Item. But wait ... that already exists. New-Item foo.txt -Type File (or ni -t f foo.txt if you like it shorter). So, this is probably a non-solution to something that can be done easier with the tools already there.
  • Rich
    Rich over 13 years
    If they want an empty file, then they certainly don't want the file to read "Test string" (even including the quotation marks).
  • Cody Gray
    Cody Gray over 13 years
    @Joey: I interpreted "What else I can use to a file starting from the first line?" as meaning they wanted to write text into the file, but they wanted it to appear on the first line of the file, rather than the second with a blank line at the top.
  • Rich
    Rich over 13 years
    Ah, ok. The question was indeed a little murky.