How to create empty text file from a batch file?

593,815

Solution 1

echo. 2>EmptyFile.txt

Solution 2

copy NUL EmptyFile.txt

DOS has a few special files (devices, actually) that exist in every directory, NUL being the equivalent of UNIX's /dev/null: it's a magic file that's always empty and throws away anything you write to it. Here's a list of some others; CON is occasionally useful as well.

To avoid having any output at all, you can use

copy /y NUL EmptyFile.txt >NUL

/y prevents copy from asking a question you can't see when output goes to NUL.

Solution 3

type NUL > EmptyFile.txt

After reading the previous two posts, this blend of the two is what I came up with. It seems a little cleaner. There is no need to worry about redirecting the "1 file(s) copied." message to NUL, like the previous post does, and it looks nice next to the ECHO OutputLineFromLoop >> Emptyfile.txt that will usually follow in a batch file.

Solution 4

Techniques I gathered from other answers:

Makes a 0 byte file a very clear, backward-compatible way:

type nul >EmptyFile.txt

idea via: anonymous, Danny Backett, possibly others, myself inspired by JdeBP's work

A 0 byte file another way, it's backward-compatible-looking:

REM. >EmptyFile.txt

idea via: Johannes

A 0 byte file 3rd way backward-compatible-looking, too:

echo. 2>EmptyFile.txt

idea via: TheSmurf

A 0 byte file the systematic way probably available since Windows 2000:

fsutil file createnew EmptyFile.txt 0

idea via: Emm

A 0 bytes file overwriting readonly files

ATTRIB -R filename.ext>NUL
(CD.>filename.ext)2>NUL

idea via: copyitright

A single newline (2 bytes: 0x0D 0x0A in hex notation, alternatively written as \r\n):

echo.>AlmostEmptyFile.txt

Note: no space between echo, . and >.

idea via: How can you echo a newline in batch files?


edit It seems that any invalid command redirected to a file would create an empty file. heh, a feature! compatibility: uknown

TheInvisibleFeature <nul >EmptyFile.txt

A 0 bytes file: invalid command/ with a random name (compatibility: uknown):

%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt

via: great source for random by Hung Huynh

edit 2 Andriy M points out the probably most amusing/provoking way to achieve this via invalid command

A 0 bytes file: invalid command/ the funky way (compatibility: unknown)

*>EmptyFile.txt

idea via: Andriy M

A 0 bytes file 4th-coming way:

break > file.txt

idea via: foxidrive thanks to comment of Double Gras!

Solution 5

REM. > empty.file

Share:
593,815

Related videos on Youtube

m_pGladiator
Author by

m_pGladiator

I don't know what to say here :) Maybe I should ask a question in StackOverflow about this field :)

Updated on May 08, 2020

Comments

  • m_pGladiator
    m_pGladiator about 4 years

    Can somebody remember what was the command to create an empty file in MSDOS using BAT file?

    • Peter Mortensen
      Peter Mortensen over 14 years
      Also at stackoverflow.com/questions/1702762, "How to create an empty file at the command line?".
    • user unknown
      user unknown about 12 years
      You aren't confusing DOS and cmd.exe, are you?
    • Wolfpack'08
      Wolfpack'08 over 4 years
      READ ME: Set-Content "your_file.txt" .gitignore -Encoding utf8 this is case-sensitive and forces utf8 encoding! (I also posted this as an answer).
  • ephemient
    ephemient over 15 years
    This echoes a newline to stdout, though... my answer doesn't.
  • TheSmurf
    TheSmurf over 15 years
    That's a fairly tangential concern, I think. Not really part of the problem.
  • Joe
    Joe over 15 years
    +1 - the question does state an empty file, so the accepted answer is wrong.
  • ephemient
    ephemient over 15 years
    DannySmurf's solution actually does create an empty file -- a newline goes to stdout, nothing goes to stderr (directed into the new file). But thanks for the +1 anyways
  • cmptrgeekken
    cmptrgeekken over 14 years
    To merge ephemient's answer and this one, you could do: "echo. >NUL 2>EmptyFile.txt" to achieve the same results without outputting a newline
  • Amit Naidu
    Amit Naidu almost 13 years
    +1 this is the natural one that first comes to mind, not the contrivances with stderr etc.
  • Danny Beckett
    Danny Beckett over 11 years
    You can also do type NUL>File2.txt
  • djangofan
    djangofan about 11 years
    You are correct. This method avoides the /y flag on the copy command.
  • TomeeNS
    TomeeNS over 10 years
    BTW, how to get File1.txt?
  • Ninh Pham
    Ninh Pham over 10 years
    Why is . in echo.
  • Andriy M
    Andriy M about 10 years
    This will create a file with a space character followed by a carriage return character followed by a new line character – not an empty file.
  • Andriy M
    Andriy M about 10 years
    It's type nul ..., not type <nul ..., actually.
  • n611x007
    n611x007 about 10 years
    @AndriyM thanks you're right I made the edit! Didn't notice because interestingly enough it works with the wrong one. which may mean that any invalid command redirected to a filename would create an empty file! just tried with NonExistentCommand <nul >EmptyFile.txt and it worked
  • n611x007
    n611x007 about 10 years
    since it would be unsafe to rely on a hardcoded command name expecting it to be "invalid", I added a randomized command name option
  • Andriy M
    Andriy M about 10 years
    That's interesting. As for random command name generation, that might be unnecessary, you could just use *>EmptyFile.txt or anything where the "command" contains a character highly unlikely to be part of the name. I admit, though, that there's always a tiny risk it becomes a valid command in the future.
  • n611x007
    n611x007 about 10 years
    @AndriyM now thats fun. You could even make it self-documenting with ?>EmptyFile.txt ;) - ? as in "what?!?!". Well, you could actually do ?!?!>EmptyFile.txt or ?????>EmptyFile.txt, to make it clear. :P
  • OneManBand
    OneManBand over 9 years
    @Reegan If you used echo 2 without the ., the console would read "ECHO is off." Using echo. 2 effectively silences console output by only displaying a newline.
  • Gras Double
    Gras Double over 9 years
    Yet another one: break > file.txt, props foxidrive
  • TPAKTOPA
    TPAKTOPA over 9 years
    type nul > EmptyFile.txt is shortest solution. Still your answer is better then accepted solution, cause your file will be really empty. +1
  • Prashant
    Prashant about 9 years
    Warning: yes the question was on how to create an empty file, however usually you want to "make sure the file exists". When using the copy /Y NUL command it might erase existing content. If you are used to Unix' touch command, this might not at all be what you expect .) Using type NUL >> emptyfile.txt is safer in this regard.
  • Prashant
    Prashant about 9 years
    And most of the commands can be used with >> if you need "must exist but should not be truncated if exist" (like Unix touch does)
  • Nam G VU
    Nam G VU almost 9 years
    I guess we woyld skip the quotes
  • foxidrive
    foxidrive almost 9 years
    @NamGVU The quotes are used in the target filename, if you choose to use long filename elements such as space or & etc. They are not part of the file contents.
  • Nam G VU
    Nam G VU almost 9 years
    Yeah I agree ^^ Permission to update yours as discussed.
  • foxidrive
    foxidrive almost 9 years
    @NamGVU The quotes are able to be left in place with or without a long filename. I guess if it's typed from the command line with a short filename, then you wouldn't want unnecessary typing.
  • Christian St.
    Christian St. over 7 years
    Short and easy. A few other solutions here do not create a new file of zero bytes!
  • AntonyG
    AntonyG about 7 years
    I'm stuck on Windows CE, and this is the only answer that creates and empty file! The other approaches either add a blank line or simply don't work...
  • riderBill
    riderBill over 6 years
    Sorry @ephemient. I didn't see your answer before the comment editing time limit ran out. I should have given you props in my first comment. I'll upvote your answer to make amends. ;^J
  • riderBill
    riderBill over 6 years
    Sorry for the downvote--I was in too much of a hurry. SO won't let me undo it. I'll come back later and try again. I still don't like your answer, though--the new line to stdout is annoying. Anyway, there are better solutions.
  • T.Teser
    T.Teser over 6 years
    it would be great if you could explain more in your answers or at least give a reference
  • Sinjai
    Sinjai over 6 years
    I couldn't find any information on CON, aside from that linked in this answer. Could someone share some light reading?
  • ephemient
    ephemient over 6 years
  • Sinjai
    Sinjai over 6 years
    @ephemient Awesome, do you have anything on practical uses?
  • ephemient
    ephemient over 6 years
    @Sinjai copy /y CON file.txt lets you type several lines, ending with ^Z, to create a file without opening an editor. If a command normally prints to a file, using CON will cause its output to go to the screen. Stuff like that.
  • DodgyCodeException
    DodgyCodeException over 6 years
    The only problem with this solution is that if there is a file called REM in the current directory, and you type REM., cmd will respond with "'rem.' is not recognized as an internal or external command, operable program or batch file."
  • DodgyCodeException
    DodgyCodeException over 6 years
    The FSUTIL utility requires that you have administrative privileges.
  • DodgyCodeException
    DodgyCodeException over 6 years
    The only problem with this solution is that if there is a file called CD (no extension) in the current directory, and you type CD., cmd will respond with "'CD.' is not recognized as an internal or external command, operable program or batch file." If you instead type CD . (with a space before the dot) it will work.
  • Bernhard Eriksson
    Bernhard Eriksson almost 6 years
    fsutil is not a standard utility in MS-DOS.
  • kayleeFrye_onDeck
    kayleeFrye_onDeck almost 6 years
    I suggest removing the second line from this answer. IMO this is the best answer on the question so far, but the second line doesn't really add value unless the person trying to use it doesn't know the basics of white-space handling for command-line.
  • aschipfl
    aschipfl over 5 years
    @DodgyCodeException, using rem/ instead of rem. solves that problem...
  • DodgyCodeException
    DodgyCodeException over 5 years
    @aschipfl thanks, that's interesting. It's also interesting how this answer from 2010 has comments spaced from each other by several years. A slowwwww conversation!
  • Wolfpack'08
    Wolfpack'08 over 4 years
    Can you change the default encoding?
  • Wolfpack'08
    Wolfpack'08 over 4 years
    Worth looking at my answer (below) because it changes the encoding.
  • jeb
    jeb over 4 years
    How is the encoding relevant for creating an empty file? A null byte file doesn't need any encoding and it's tricky to mark it with a BOM (0 bytes)
  • Wolfpack'08
    Wolfpack'08 over 4 years
    @jeb because people fill empty files, and when filled files are used as settings files, they create errors when they are not UTF8. In fact, I would say people almost always fill files and don't leave them blank, and I wouldn't use any other method to create a blank file that I later intended to alter because I nearly always want to be certain that my files are UTF8. For example, the .git tutorial on github asks the user to make a blank file which is filled later, and the other method makes a file with weird Windows encoding.
  • aschipfl
    aschipfl over 3 years
    @Wolfpack'08, the file encoding is not stored in a special attribute or in some other magical secret area, it is just reflected by the data it contains; if there is no data, there cannot be an encoding…
  • cbmeeks
    cbmeeks over 3 years
    This should have been the accepted answer. About as clean and simple as it gets.
  • Wolfpack'08
    Wolfpack'08 over 3 years
    @aschipfl A blank file must contain some data, then. If you create a blank file and explicitly flag utf8, the file works after it is later populated; on the other hand, if you create a blank file and ostensibly allow default encoding (ASCII), when you later fill the file it will break whatever program requires UT8 encoding. I have first-hand experience of this, and anybody can test it. Regardless of your incredulity, this is demonstrably true and tolerates empirical testing: I think the negative feedback is totally unwarranted and damages the accessibility of this important info. Prove it.
  • aschipfl
    aschipfl over 3 years
    A blank file is a blank file, there is no data inside; otherwise, it would not be blank (empty, 0 bytes), would it? You seem to work with a versioning tool (git), which stores some meta-data for each file, apparently including its encoding. But the blank file itself definitely does not hold any encoding setting, neither in the data area (0 bytes obviously hold no information), nor in the file table of the file system (FAT, NTFS, or whatever you may use)…
  • Nikolai Ehrhardt
    Nikolai Ehrhardt over 3 years
    For me, win10, this causes syntax-error? So filepath is not valid?