views:

13962

answers:

5

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

+14  A: 
echo. 2>EmptyFile.txt
DannySmurf
m_pGladiator, if you're satisfied with DannySmurf's answer, perhaps you should mark it as "accepted".
Sandman
This echoes a newline to stdout, though... my answer doesn't.
ephemient
That's a fairly tangential concern, I think. Not really part of the problem.
DannySmurf
Sometimes it's relevant; I used to have touch lying around until I got the idea of just copying NUL (or type NUL>file) for the purpose of getting 0-byte files. :-)
Joey
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
cmptrgeekken
+22  A: 
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.

ephemient
+1 - the question does state an *empty* file, so the accepted answer is wrong.
Joe
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
ephemient
+7  A: 

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.

+1  A: 

You can use a TYPE command instead of COPY.

Try this: TYPE File1.txt>File2.txt

Where File1.txt is empty.

A: 

REM. > empty.file

Johannes