Create Empty File

Maintained on

Batch files in Windows can make PC operations more efficient once you get the hang of them. However, if you’re not a specialist in this area, batch files can be difficult to work with, especially if you’re used to other programming languages. In this article, we’ll show you how to create an empty file using batch files, with copy-and-paste code that you can use.

Using type

The type function is originally used to display the contents of a file specified in the argument.

type C:\Users\Public\Documents\sample.txt

If you execute the above command, the contents of sample.txt will be displayed on the command line.

If you specify a destination as an argument, the contents of the source file will be reflected in the destination file.

type C:\Users\Public\Documents\sample.txt > C:\Users\Public\Documents\sample2.txt

If you execute the above command, the contents of sample.txt saved in Public Documents will be copied to sample2.txt.

To create a file using the type command, specify nul as the first argument of the type command.

type nul > <new_file_name>

By doing this, an empty file with nothing written in it will be created with the file name specified in the second argument.

type nul > %file_name%

The following is test data for copy and paste. Please change the variables FOLDER and FILE to the folder and file name you want to output, respectively.

Image (can be copied)

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>type nul > %userprofile%/Documents/empty.file

In case of a batch file:

@echo off
setlocal

set FOLDER=%userprofile%\Documents\
set FILE=test.out

type nul > %FOLDER%%FILE%

endlocal
exit

Using Copy command

The copy command is a function that copies data using two arguments.

copy A B

If you execute the above command, you can copy a file named A to a file named B.

To create an empty file using the copy command, you can specify nul as the first argument, similar to the type command mentioned earlier.

copy nul <new_file_name>

By doing this, an empty file with nothing written in it will be created with the file name specified in the second argument.

copy nul %file_name%

The following is test data for copy and paste. Please change the variables FOLDER and FILE to the folder and file name you want to output, respectively.

Image (can be copied)

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>copy nul %userprofile%/Documents/empty.file

For batch files:

@echo off
setlocal

set FOLDER=%userprofile%\Documents\
set FILE=test.out

copy nul %FOLDER%%FILE%

endlocal
exit

For more information about the copy command, please refer to the following page:

Creating an empty file using the echo command

The echo command, which is familiar to many, can also be used to create an empty file.

However, please note that a completely empty file is not created. Two bytes of data are created.

But if you only use it for duplicate checks, it shouldn’t be a big problem.

By specifying the destination with echo, you can write the argument to a file.

At this point, you may want to use echo nul>%file_name%, but it doesn’t work because nul is treated as the string “nul”.

Therefore, by outputting a line break with echo; and combining it as follows, you can create an empty file:

echo;>%file_name%

You may have noticed that this process does not create a completely empty file, but rather a file containing only a line break.

This is because the file size becomes 2 bytes.

echo;>%file_name%

The following is test data for copy and paste. Please change the variables FOLDER and FILE to the folder and file name you want to output, respectively.

Image (can be copied)

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>echo;>%userprofile%/Documents/empty.file

For batch files:

@echo off
setlocal

set FOLDER=%userprofile%\Documents\
set FILE=test.out

echo;>%FOLDER%%FILE%

endlocal
exit

For more information about the echo command, please refer to the following page:

Confirmation Test

Quiz

Which of the following commands creates a sample.txt file that is not 0 bytes in size?

#PowerShell