Wednesday, April 6, 2011

How to create an empty file at the command line?

How to create an empty file at the DOS/Windows command-line?

I tried:

copy nul > file.txt

but it always displays that a file was copied.

Is there any other method in the standard cmd?

It should be a method that does not require the touch command from Cygwin or any other nonstandard commands. The command needs to run from a script so keystrokes cannot be used.

From stackoverflow
  • echo "" > filename
    

    I believe this works on Windows/DOS, but my last hands-on experience with either is quite a while ago. I do know for a fact that it works on basically any POSIX compliant OS.

    Kris : Apperantly, VonC's answer is better than mine, so please upvote that instead.
    Grendler : Unfortunately: echo "" displays double quotes and they are written to the file when stream is redirected to it. The same happens with just: echo > filename because it writes ECHO is off/on to the file as well.
    Kris : maybe you could put "@echo off" on the line before creating the file to circumvent that?
  • I'm using curly { } brackets for enter and Ctrl+Z keyboard

    copy con SomeFile.txt {enter}

    {Ctrl+Z} {enter}

    Grendler : I precised the question that the command will run from script so unfortunately any keyboard interaction does not work. Thank you anyway.
  • echo.>filename
    

    (echo "" would actually put "" in the file! And echo without the '.' would put "Command ECHO activated" in the file...)

    Note: the resulting file is not empty but includes a return line sequence: 2 bytes.


    This discussion points to a true batch solution for a real empty file:

     <nul (set/p z=) >filename
    
     dir filename
     11/09/2009  19:45                 0 filename
     1 file(s)                         0 bytes
    

    The "<nul" pipes a nul response to the set/p command, which will cause the variable used to remain unchanged. As usual with set/p, the string to the right of the equal sign is displayed as a prompt with no CRLF.

    Since here the "string to the right of the equal sign" is empty... the result is an empty file.


    The difference with cd. > filename (which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the <nul... trick) can be used to echo lines without any CR:

    <nul (set/p z=hello) >out.txt
    <nul (set/p z= world!) >>out.txt
    dir out.txt
    

    The dir command should indicate the file size as 12 bytes: "hello world!".

    Agent_9191 : you'd actually want `echo.>filename` because it will include the space as well as the newline character.
    Greg Hewgill : Using the `rem` command avoids creating a file with an empty line in it.
    VonC : @Agent_9191: true, I have updated my answer. @Greg: not sure what you mean: `rem>filename` produces the same result (2 bytes)
    Greg Hewgill : I recall the `rem` trick worked in the past (maybe only DOS?), but apparently not today!
    Grendler : @VonC filename trick works exactly as I needed. Thanks :-)
    Noufal Ibrahim : +1 but one can see why scripting with DOS is not popular. :)
    Joey : Noufal Ibrahim: don't let this fool you; just see the next answer which has a much easier and equally working solution. What's done here is partially wrong in the first case (not empty but contains a line break) and way overcomplicated in the second one.
  • You can write your own touch.

    //touch.cpp
    #include <fstream>
    #include <iostream>
    
    int main(int argc, char ** argv;)
    {
      if(argc !=2)
      {
        std::cerr << "Must supply a filename as argument" << endl;
        return 1;
      }
      std::ofstream foo(argv[1]);
      foo.close();
      return 0;
    }
    
  • Reading comments on my post, I have to admit I didn't read the question right.

    On the Windows command-line, one way would be to use fsutil:

    fsutil file createnew <filename> <size>
    

    An example:

    fsutil file createnew myEmptyFile.txt 0
    

    Below is for *nix command-line.

    touch filename
    

    This command changes your modified date of a file or creates it if file is not found.

    qid : Unfortunately, the question specifically states, "Without the touch command from Cygwin."
    Greg Hewgill : There exist non-Cygwin implementations of the touch command: http://unxutils.sourceforge.net/ is good.
    Frank Farmer : In *nix, I'm personally partial to a simple `> filename`, which can also be used to truncate an existing file.
    Joey : `fsutil` needs administrative privileges. That's a bit much to ask for simply creating an empty file ...
  • If you really want a totally empty file, without any output to stdout, you can cheat a little:

    copy nul file.txt > nul
    

    Just redirect stdout to nul, and the output from copy disappears.

    Grendler : It fails if the file.txt exists.
    wallyk : Failing if the file exists is good behavior as I understand the question.
    Joey : +1. It's amazing how the accepted answer is something half-wrong and half convoluted while the obviously correct one gets nearly no credit. To add to this: `type nul>file` would be another way.
  • Here's another way:

    cd. > filename
    
    VonC : It seems to work as well. +1
  • You could also use

    echo. 2>foo

    The debug output for echo. will almost definitely be empty :)

    HTH, -Craig

0 comments:

Post a Comment