Create Empty File
Sometimes you need an empty file in a batch file or at the command prompt, for example as a marker file that signals a job has finished, or to initialize a log file.
This page shows three ways to create an empty file, with copy-and-paste code.
Summary: Which Method Should You Use?
| Method | File size | If the file already exists |
|---|---|---|
type nul > file | 0 bytes | Overwritten (contents are lost) |
copy nul file | 0 bytes | Prompts for confirmation at the command prompt; overwrites without asking inside a batch file |
echo;>file | 2 bytes (line break only) | Overwritten (contents are lost) |
If you need a true 0-byte file, use type nul or copy nul.
Each of these methods can wipe out the contents of an existing file with the same name. If you want to keep existing files, see “Avoiding Overwriting an Existing File” below.
Using type
The type command normally displays the contents of the file given as its argument.
type C:\Users\Public\Documents\sample.txt
If you add a redirection target with >, the output is written to that file instead.
type C:\Users\Public\Documents\sample.txt > C:\Users\Public\Documents\sample2.txt
The command above copies the contents of sample.txt into sample2.txt.
Using this mechanism, passing nul to type creates a file with nothing in it. nul is a special device name that outputs nothing and reads as empty.
type nul > <new_file_name>
type nul > %file_name%
The following is a copy-and-paste sample. Change the variables FOLDER and FILE to the folder and file name you want.
Command Prompt (copyable):
In a batch file:
@echo off
setlocal
set FOLDER=%userprofile%\Documents\
set FILE=test.out
type nul > "%FOLDER%%FILE%"
endlocal
exit /b
Using copy
The copy command copies files.
copy A B
The command above copies file A to file B.
You can also create an empty file with copy by specifying nul as the source.
copy nul <new_file_name>
The command prints “1 file(s) copied.” To suppress the message, append >nul.
copy nul %file_name%
Command Prompt (copyable):

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
In a batch file:
@echo off
setlocal
set FOLDER=%userprofile%\Documents\
set FILE=test.out
copy nul "%FOLDER%%FILE%" >nul
endlocal
exit /b
For more information about the copy command, see the following page:
Using echo
The familiar echo command can also create a file. However, it does not create a completely empty file; 2 bytes of data are written.
You might be tempted to write echo nul>file_name, but nul is treated as the literal string “nul”, so the word “nul” would be written into the file.
Instead, use echo;, which outputs only a line break:
echo;>file_name
The resulting file contains only a line break (CRLF), so its size is 2 bytes. That is fine when you only need the file to exist, for example as an existence check.
echo;>%file_name%
Command Prompt (copyable):
In a batch file:
@echo off
setlocal
set FOLDER=%userprofile%\Documents\
set FILE=test.out
echo;>"%FOLDER%%FILE%"
endlocal
exit /b
For more information about the echo command, see the following page:
Avoiding Overwriting an Existing File
Redirection with > erases the contents of an existing file. To create the file only if it does not exist, and leave it untouched otherwise, use >> (append):
type nul >> sample.txt
Because nothing is appended, existing contents are preserved, and an empty file is created if the file does not exist.
You can also combine this with if not exist. For checking whether a file exists, see the following page:
Note: Using PowerShell
In PowerShell, use the New-Item cmdlet to create an empty file.
New-Item sample.txt -ItemType File