Create Empty file at Windows Command Line


👤 Diwas Poudel    🕒 Apr 23 2022    📁 Fix    📜 0 comment

Empty files are those with a file size of zero bytes and no data stored in them. You can make an empty text file, word document, or whatever you want.

With the "touch" command in Linux, we can easily create an empty file. In Windows OS, there is no direct equivalent command for touch. But don't worry, you can accomplish the same thing using a variety of methods in Windows, which I will go over in detail.

In general, we like to create a dummy file just for testing our application and then add the information as needed later. You can create an Empty File with the GUI, Command Prompt, or Powershell.

1 Using Echo Command in Command Prompt or Batch File

The echo command in Windows is used to display a string provided by the user. We can create an empty file using Echo and let's look at the syntax at first.

Here you have to understand follow points:

- No need for administrative privileges.
- In this method, if the file already exists then also it will overwrite the file and create the new one.

1a) Method 1

Syntax:

echo -n "" > filenamewithpath 

Where "filenamewithpath" indicates you create a filename with a path mentioned in it.

Example:

echo -n "" > D:\empty.txt

Inside D drive, I'm making an empty.txt file.

> is a redirection operator, and is used to send whatever is ever-present on the left to the right. Here echo is holding empty string "" and an empty string is written to the file empty.txt.This empty string will have 0 sizes.

You can replace D:\empty.txt with whatever you like.

Note that if you are going to write in C drive (or any drive where windows is installed) then you may get access denied.

1b) Method2

Combining echo on/off with & operator

echo off > D:\empty.txt & echo on
empty-text-file
fig. Empty Text File using Echo Command

1c) Method 3

 echo. 2>"D:\empty.txt"

2 Using Copy Command in Command Prompt or Batch File

The Copy command in Windows is typically used to copy one or more files from one location to another. They can be used to create files, empty files, and so on.

Also read: How do I navigate to a desktop in Windows CMD?

Notes:

  • No need for administrative privileges.
  • In this method, if the file already exists then also it will overwrite the file and create the new one.

Example:

COPY NUL  "D:/empty.txt"  > NUL 

If the file does not exist, the copy will create a new one; if it does exist, it will overwrite the previous one.

In this case, we're copying NUL to the empty.txt file, and the "> NUL" indicates that it will not display any success or failure messages like "1 file(s) copied." or "Overwrite asdddff.txt? (Yes/No/All):"

3 Using winit in Command Prompt or Batch File

wininit is a Microsoft Windows operating system software component. Using winit.exe, we can create an empty file.

Notes:

  • No need for administrative privileges.
  • In this method, if the file already exists then also it will overwrite the file and create the new one.

Example:

wininit >D:\empty.txt

4 Using REM in Command Prompt or Batch File

REM is an abbreviation for Remark. It supports the inclusion of comments within batch files. REM can be used to create the empty file shown below.

Also Read: How to find or check windows 10 / 11 user login history?

Notes:

  • No need for administrative privileges.
  • In this method, if the file already exists then also it will overwrite the file and create the new one.

Example:

REM. >D:\empty.txt

5 Using Powershell 

Microsoft Powershell is a set of task automation and configuration management tools. We can use this tool to create an empty file.Simply launch PowerShell and enter the following:

Example:

New-Item D:\empty.txt

Just replace empty.txt with whatever you want. 

6 Using Type Operator in Command Prompt or Batch File

type is an in-built command that displays the contents of a text file.

Notes:

  • No need for administrative privileges.
  • In this method, if the file already exists then also it will overwrite the file and create the new one.

Example:

type NUL > D:\empty.txt

7 Using fsutil Command in Command Prompt or Batch File

Notes:

  • Need for administrative privileges.
  • In this method, if the file already exists then it doesn't overwrite the file and will prompt you with an error like "Error:  The file exists." and if you include "< nul" in the command then it will not prompt a message.
Also read: How to list all environment variables?

Example:

fsutil file createnew D:\empty1.txt 0 > nul

8  Using Colon Operator in Command Prompt / Batch File

Example:

: > D:\empty.txt

FAQ:

How to create read-only empty files in Windows?

We can easily create read-only and empty files in Windows by combining two texts one creates an empty text file and then set read-only permission to the file."type Nul > abc.txt" creates an empty file and "attrib +r abc.txt" creates read-only permission for the same file. Here, attrib is used to set and remove file attributes like hidden(h), read-only(r), system(s) and archive(a).

 type Nul > abc.txt & attrib +r abc.txt

If you want to remove read-only from this you can use the "-r" parameter instead of "+r" 

 How to create a hidden empty file in Windows?

For this we use "+h" parameter as shown below.

type Nul > abc.txt & attrib +h abc.txt

 

How to create multiple empty files in Windows?

For creating multiple we can use for loop to generate the empty file. Here we are creating 10 empty files in the current location. The filename will be respectively:myemptyfile-1.txt,myemptyfile-2.txt,myemptyfile-3.txt and soon up to myemptyfile-10.txt

for /l %a in (1 1 10) do type nul > "myemptyfile-%a.txt"
creating-multiple-empty-files
fig. Create multiple empty file
Can we create Empty doc, xlsx files in Windows?

Yes, you can create empty docs, and xlsx files using any of the above methods. You can simply try like this:

type NUL > D:\emptyDocsFile.docx
empty-docs-file.
fig. Creating empty docs file 

Conclusion:

With the aforementioned methods, we can successfully create empty files such as txt, Docx, xlsx, or any text file.