[batch-file] How to create empty text file from a batch file?

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

This question is related to batch-file cmd dos

The answer is


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!


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.


If there's a possibility that the to be written file already exists and is read only, use the following code:

ATTRIB -R filename.ext
CD .>filename.ext

If no file exists, simply do:

CD .>filename.ext

(updated/changed code according to DodgyCodeException's comment)

To supress any errors that may arise:

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

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.


fsutil file createnew file.cmd 0

There are infinite approaches.

Commands that output nothing:

break
cls
color
goto
pushd
popd
prompt
title

Weird Commands:

CD.
REM.
@echo off
cmd /c
START >FILE

The outdated print command produces a blank file:

print /d:EMPTY_TEXT_FILE nul

fsutil file createnew file.cmd 0

The easiest way is:

echo. > Filename.txt


IMPORTANT:

If you don't set the encoding, many softwares can break. git is a very popular example.

Set-Content "your_ignore_file.txt" .gitignore -Encoding utf8 this is case-sensitive and forces utf8 encoding!


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.


One more to add to the books - short and sweet to type.

break>file.txt
break>"file with spaces in name.txt"

If there's a possibility that the to be written file already exists and is read only, use the following code:

ATTRIB -R filename.ext
CD .>filename.ext

If no file exists, simply do:

CD .>filename.ext

(updated/changed code according to DodgyCodeException's comment)

To supress any errors that may arise:

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

You can use a TYPE command instead of COPY. Try this:

TYPE File1.txt>File2.txt

Where File1.txt is empty.


REM. > empty.file


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.


You can also use SET to create a null byte file as follows

set x=x > EmptyFile.txt

Or if you don't want to create an extra variable reassign an existing variable like

set PROMPT=%PROMPT% > EmptyFile.txt

or like this:

set "PROMPT=%PROMPT%" > EmptyFile.txt

The easiest way is:

echo. > Filename.txt


You can use a TYPE command instead of COPY. Try this:

TYPE File1.txt>File2.txt

Where File1.txt is empty.


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.


You can also use SET to create a null byte file as follows

set x=x > EmptyFile.txt

Or if you don't want to create an extra variable reassign an existing variable like

set PROMPT=%PROMPT% > EmptyFile.txt

or like this:

set "PROMPT=%PROMPT%" > EmptyFile.txt

One more to add to the books - short and sweet to type.

break>file.txt
break>"file with spaces in name.txt"

IMPORTANT:

If you don't set the encoding, many softwares can break. git is a very popular example.

Set-Content "your_ignore_file.txt" .gitignore -Encoding utf8 this is case-sensitive and forces utf8 encoding!


REM. > empty.file


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.


Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)

Examples related to cmd

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH VSCode Change Default Terminal How to install pandas from pip on windows cmd? 'ls' in CMD on Windows is not recognized Command to run a .bat file VMware Workstation and Device/Credential Guard are not compatible How do I kill the process currently using a port on localhost in Windows? how to run python files in windows command prompt?

Examples related to dos

How to increment variable under DOS? MS-DOS Batch file pause with enter key How to list files using dos commands? Commenting multiple lines in DOS batch file copy all files and folders from one drive to another drive using DOS (command prompt) In MS DOS copying several files to one file How to get a list of sub-folders and their files, ordered by folder-names How do I increment a DOS variable in a FOR /F loop? Recursive directory listing in DOS DOS: find a string, if found then run another script