Skip to content
Fix Code Error

How to create an empty file at the command line in Windows?

March 13, 2021 by Code Error
Posted By: Anonymous

How to create an empty file at the DOS/Windows command-line?

I tried:

copy nul > file.txt

but it always displays that a file was copied.

Is there any other method in the standard cmd?

It should be a method that does not require the touch command from Cygwin or any other nonstandard commands.
The command needs to run from a script so keystrokes cannot be used.

Solution

Without redirection, Luc Vu or Erik Konstantopoulos point out to:

copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt

"How to create empty text file from a batch file?" (2008) also points to:

type NUL > EmptyFile.txt
# also
echo. 2>EmptyFile.txt
copy nul file.txt > nul # also in qid's answer below
REM. > empty.file
fsutil file createnew file.cmd 0 # to create a file on a mapped drive

Nomad mentions an original one:

C:UsersVonCprogtests>aaaa > empty_file
'aaaa' is not recognized as an internal or external command, operable program or batch file.

C:UsersVonCprogtests>dir

 Folder C:UsersVonCprogtests

27/11/2013  10:40    <REP>          .
27/11/2013  10:40    <REP>          ..
27/11/2013  10:40                 0 empty_file

In the same spirit, Samuel suggests in the comments:

the shortest one I use is basically the one by Nomad:

.>out.txt

It does give an error:

'.' is not recognized as an internal or external command

But this error is on stderr. And > only redirects stdout, where nothing have been produced.
Hence the creation of an empty file.
The error message can be disregarded here. Or, as in Rain‘s answer, redirected to NUL:

.>out.txt 2>NUL

(Original answer, November 2009)

echo.>filename

(echo "" would actually put "" in the file! And echo without the ‘.’ would put "Command ECHO activated" in the file…)

Note: the resulting file is not empty but includes a return line sequence: 2 bytes.


This discussion points to a true batch solution for a real empty file:

 <nul (set/p z=) >filename

 dir filename
 11/09/2009  19:45                 0 filename
 1 file(s)                         0 bytes

The "<nul" pipes a nul response to the set/p command, which will cause the
variable used to remain unchanged. As usual with set/p, the string to the
right of the equal sign is displayed as a prompt with no CRLF.

Since here the "string to the right of the equal sign" is empty… the result is an empty file.


The difference with cd. > filename (which is mentioned in Patrick Cuff’s answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the <nul... trick) can be used to echo lines without any CR:

<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt

The dir command should indicate the file size as 11 bytes: "helloworld!".

Answered By: Anonymous

Related Articles

  • How to avoid "module not found" error while calling…
  • Is CSS Turing complete?
  • How many times the loss function is triggered from…
  • What are the undocumented features and limitations…
  • Obtain most recent value for based on index in a…
  • How can I change my Cygwin home folder after installation?
  • Does moment.js allow me to derive a timezone…
  • Simplest way to create Unix-like continuous pipeline…
  • How to properly do JSON API GET requests and assign…
  • List of Timezone IDs for use with FindTimeZoneById() in C#?
  • Best way to extract messy HTML tables using BeautifulSoup
  • What is your most productive shortcut with Vim?
  • How do I set up CLion to compile and run?
  • Memcached vs. Redis?
  • What is the difference between Cygwin and MinGW?
  • How to calculate Cohen's D across 50 points in R
  • SMTP error 554
  • How do I make a Windows batch script completely silent?
  • Azure Availability Zone ARM Config
  • How to use html template with vue.js
  • How to parse JSON with XE2 dbxJSON
  • Removing double quotes from variables in batch file…
  • Search match multiple values in single field in…
  • NullpointerException error while working with…
  • Avoid creating new session on each axios request laravel
  • Batch files : How to leave the console window open
  • The 'compilation' argument must be an instance of…
  • Flatten points into SVG polyline Polymer
  • How to check if running in Cygwin, Mac or Linux?
  • How can I determine whether a 2D Point is within a Polygon?
  • Get operating system info
  • Where can I download an offline installer of Cygwin?
  • Fix top buttons on scroll of list below
  • ./components/Avatar.tsx Error: Cannot find module…
  • How to convert CMD to JSON array correctly in Dockerfile?
  • HTML 5 Favicon - Support?
  • Root user/sudo equivalent in Cygwin?
  • "The import org.springframework cannot be resolved."
  • Event Snippet for Google only shows one event while…
  • Backbone View function throws TypeError: object is…
  • Ukkonen's suffix tree algorithm in plain English
  • The definitive guide to form-based website authentication
  • How do I echo and send console output to a file in a…
  • loop and eliminate unwanted lines with beautiful soup
  • Best way to do multi-row insert in Oracle?
  • Setting up and using Meld as your git difftool and mergetool
  • Could not autowire. No beans of…
  • How to add geojson points to the map
  • Why am I getting a nil pointer error depending on…
  • How do I use the nohup command without getting nohup.out?
  • Batch script: how to check for admin rights
  • Docker compose fails to start a service with an…
  • How do I limit the number of digits from 6 to 4 in…
  • Start redis-server with config file
  • Why does this Azure Resource Manager Template fail…
  • insert tables in dataframe with years from 2000 to…
  • Azure CLI - az deployment group create -…
  • Tensorflow: how to save/restore a model?
  • Persistent invalid graphics state error when using ggplot2
  • What is the reason for the error message "System…
  • Javascript Drag and drop for touch devices
  • Octave using 'for' statement to show two animations…
  • const commandFolders = readdirSync('./commands');…
  • CMD: Export all the screen content to a text file
  • Update points Redux React Native
  • How can I find the product GUID of an installed MSI setup?
  • How to check if ping responded or not in a batch file
  • What is the difference between VFAT and FAT32 file systems?
  • Difference between variable declaration syntaxes in…
  • Stored procedure or function expects parameter which…
  • mongodb group values by multiple fields
  • Angular: Can't find Promise, Map, Set and Iterator
  • Pandas pivot_table: filter on aggregate function
  • C++ template,typename and operator
  • Open Cygwin at a specific folder
  • Worker Script Failing to Load for Vue Webpack Built App
  • ComboBox.SelectedItem giving Null value
  • collision detection from picturebox in a list c#
  • Using Cygwin to Compile a C program; Execution error
  • Windows XP or later Windows: How can I run a batch…
  • Issues trying to install sylius/product-bundle to my…
  • Consider defining a bean of type…
  • How can I pass a wct test while rearranging children…
  • FFmpeg: How to split video efficiently?
  • How to query Json field so the keys are the column…
  • Most effective way to parse JSON Objects
  • Active tab issue on page load HTML
  • How to get device make and model on iOS?
  • How to get folder path from file path with CMD
  • Python scrape JS data
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • Smart way to truncate long strings
  • How to paste yanked text into the Vim command line
  • Can't upload files with Apollo-client GraphQL in…
  • How to traverse the complex nested Json in C# and…
  • C# Get YouTube videoId from Json
  • Something is wrong with my if and else statement
  • Bootstrap Carousel Full Screen
  • How to use 2 columns as "key" to get MAX value of…
  • Suppress command line output

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

What does enctype=’multipart/form-data’ mean?

Next Post:

Changing the color of an hr element

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error