views:

944

answers:

8

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.

A: 
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.
Kris
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.
Grendler
maybe you could put "@echo off" on the line before creating the file to circumvent that?
Kris
+1  A: 

I'm using curly { } brackets for enter and Ctrl+Z keyboard

copy con SomeFile.txt {enter}

{Ctrl+Z} {enter}

DRapp
I precised the question that the command will run from script so unfortunately any keyboard interaction does not work.Thank you anyway.
Grendler
+13  A: 
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!".

VonC
you'd actually want `echo.>filename` because it will include the space as well as the newline character.
Agent_9191
Using the `rem` command avoids creating a file with an empty line in it.
Greg Hewgill
@Agent_9191: true, I have updated my answer. @Greg: not sure what you mean: `rem>filename` produces the same result (2 bytes)
VonC
I recall the `rem` trick worked in the past (maybe only DOS?), but apparently not today!
Greg Hewgill
@VonC<nul (set/p z=) >filenametrick works exactly as I needed. Thanks :-)
Grendler
+1 but one can see why scripting with DOS is not popular. :)
Noufal Ibrahim
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.
Joey
+2  A: 

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;
}
Paul Nathan
+3  A: 

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.

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

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.

qid
It fails if the file.txt exists.
Grendler
Failing if the file exists is good behavior as I understand the question.
wallyk
+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.
Joey
+3  A: 

Here's another way:

cd. > filename
Patrick Cuff
It seems to work as well. +1
VonC
A: 

You could also use

echo. 2>foo

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

HTH, -Craig

Craig