Creating Empty Files from the Command Line

Creating Empty Files from the Command Line

In many operating systems, like Linux and macOS, the touch command is a convenient way to create empty files or update the timestamp of existing files. Windows doesn’t natively include this command, but several alternative methods allow you to achieve the same result from the command line. This tutorial will explore the common approaches for creating empty files in Windows.

Using type nul

A straightforward method for creating an empty file is using the type nul command redirected with the greater-than symbol (>). nul is a special device in Windows that discards any output sent to it. By redirecting the output of type nul to a new file, you effectively create an empty file.

type nul > your_file.txt

This command creates a file named your_file.txt in the current directory. If the file already exists, its content will be overwritten.

Using echo

The echo command, typically used to display text, can also create empty files when redirected.

echo. > your_file.txt

Note the period (.) after echo. This ensures that an empty line is written to the file before redirection, resulting in a zero-byte file. Without the period, some versions of the command interpreter might create a file containing a carriage return and line feed.

Preserving Existing File Content

If you want to update the timestamp of an existing file without overwriting its content, you can use the >> (append) operator. However, using >> with type nul or echo will add a new line to the end of the file rather than simply updating the timestamp.

For updating the timestamp of an existing file without altering its content, more complex solutions involving file copying are required, or using a custom command (see below).

Using copy

The copy command, when used with specific parameters, offers another solution.

copy /b filename.ext +,,

This command copies the file itself to itself, effectively updating the timestamp without altering the content. If the file doesn’t exist, this command won’t create it.

Using PowerShell

If you’re working in PowerShell, you can utilize the New-Item cmdlet:

New-Item -type file your_file.txt

Or, using its alias:

ni your_file.txt

This command creates an empty file named your_file.txt.

Creating a Custom Command

For a more touch-like experience, you can create a batch file (e.g., touch.cmd) and place it in a directory that’s included in your system’s PATH environment variable. This will allow you to call touch from any command line window. Here’s an example of the batch file contents:

@echo off
setlocal enableextensions disabledelayedexpansion

(for %%a in (%*) do if exist "%%~a" (
    pushd "%%~dpa" && ( copy /b "%%~nxa"+,, & popd )
) else (
    type nul > "%%~fa"
)) >nul 2>&1

This script iterates through the arguments passed to it. If a file exists, it updates the timestamp using copy. If it doesn’t exist, it creates the file using type nul.

Choosing the Right Method

The best method for creating empty files depends on your specific needs:

  • For simple, quick file creation, type nul > filename.txt or echo. > filename.txt are often sufficient.
  • If you need to preserve existing file content and update the timestamp, the custom batch file solution or the copy command is recommended.
  • If you’re already working in PowerShell, New-Item is the most natural choice.

Leave a Reply

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