How to Create a Zero-Byte File (Touch) in Batch Script
In scripting, you sometimes need to create an empty file, not for its content, but for its presence. These zero-byte files can act as markers to signal that a process has completed, as simple "lock" files to prevent a script from running multiple times, or simply to create a placeholder. In Unix-like systems, this is done with the touch command. While Windows doesn't have a touch command, it has several simple, built-in methods to achieve the same result.
This guide will show you the most common and reliable methods for creating a zero-byte file, explain the important distinction between creating a file and updating its timestamp (the other function of touch), and provide a practical example of a lock file to control script execution.
The Primary Method: TYPE NUL >
The most common and arguably best method for creating an empty file is by redirecting the output of the special NUL device.
NUL: This is a virtual "null device" in Windows. It's a black hole; it contains nothing and discards anything sent to it.TYPE NUL: This command attempts to display the contents ofNUL, which is nothing.>: The redirection operator takes that "nothing" and sends it to a file.
If the file doesn't exist, it's created. If it does exist, it's overwritten with nothing, resulting in an empty, 0-byte file.
@ECHO OFF
ECHO Creating an empty marker file...
TYPE NUL > process_complete.marker
ECHO Verifying the file...
DIR process_complete.marker
Output (The DIR command shows the file was created with a size of 0):
Creating an empty marker file...
Verifying the file...
Volume in drive C is Windows
Volume Serial Number is 1234-ABCD
Directory of C:\Scripts
10/27/2023 11:30 AM 0 process_complete.marker
1 File(s) 0 bytes
An Alternative Method: COPY NUL
Another popular method is using the COPY command with the NUL device as the source.
@ECHO OFF
ECHO Creating a lock file...
COPY NUL my_app.lock
This also creates a 0-byte file named my_app.lock as result. However, there is a key difference in behavior:
TYPE NUL > my_app.lock: Silently overwritesmy_app.lockif it already exists. This is ideal for automation where you always want to reset the file.COPY NUL my_app.lock: Prompts for confirmation ifmy_app.lockalready exists. This will halt your script, waiting for user input. You can bypass this withCOPY /Y NUL my_app.lock, butTYPE NUL >is generally simpler and more direct for unattended scripts.
The "Touch" Concept: How to Update a Timestamp
In the Unix world, touch does two things: it creates a zero-byte file if it doesn't exist, and it updates the last modified timestamp of the file if it does exist. The methods above only handle the creation part.
To update a timestamp on an existing file without changing its contents, you can use a peculiar-looking COPY command.
The following script uses this command that will "touch" an existing file, updating its last modified time to the current time.
@ECHO OFF
REM Assume data.log already exists.
ECHO The timestamp of data.log before:
DIR data.log | FIND "data.log"
ECHO Updating the timestamp...
COPY /B data.log +,, > NUL
ECHO The timestamp of data.log after:
DIR data.log | FIND "data.log"
The magic here is +,,. This tells the COPY command to concatenate the file with nothing, and the side effect of this operation is that the file's metadata (including its timestamp) is updated.
Common Pitfalls and How to Solve Them
Problem: The File Already Exists
The most significant "pitfall" is unintentionally overwriting a file that has important content because you used TYPE NUL >.
Solution: Check with IF NOT EXIST
If your goal is to create a marker file only when one isn't already present, you should always perform a check first.
@ECHO OFF
SET "MARKER_FILE=installation.complete"
IF NOT EXIST "%MARKER_FILE%" (
ECHO Marker file does not exist. Creating it now.
TYPE NUL > "%MARKER_FILE%"
) ELSE (
ECHO Marker file already exists. Skipping creation.
)
This logic prevents accidental data loss and allows your script to make decisions based on the file's presence.
Practical Example: Using a Zero-Byte File as a Lock File
A lock file is a classic way to ensure only one instance of a script runs at a time. The logic is simple: if the lock file exists, exit; otherwise, create it, do the work, and then delete it.
@ECHO OFF
SETLOCAL
SET "LOCK_FILE=%TEMP%\my_script.lock"
ECHO --- My Critical Process Script ---
REM Check if the lock file exists.
IF EXIST "%LOCK_FILE%" (
ECHO [ERROR] Another instance of the script is already running.
ECHO Lock file found: %LOCK_FILE%
GOTO :End
)
ECHO No other instance found. Creating lock file...
TYPE NUL > "%LOCK_FILE%"
ECHO.
ECHO Starting important work...
REM (Simulate a long process)
TIMEOUT /T 5 > NUL
ECHO Work finished.
ECHO.
ECHO Removing lock file...
DEL "%LOCK_FILE%"
:End
ECHO Script complete.
ENDLOCAL
This script is safe to run multiple times. The second instance will detect the lock file created by the first and exit immediately.
Conclusion
While Windows lacks a dedicated touch command, its functionality is easily replicated with simple, built-in tools.
- To create a new or overwrite an existing zero-byte file, the command
TYPE NUL > filename.txtis the most direct and reliable method for automated scripts. - To update the last modified timestamp of an existing file without changing its content, use the
COPY /B filename.txt +,,command. - To avoid accidentally overwriting important data, use an
IF NOT EXISTcheck before creating a file.
These techniques provide all the power you need to manage marker files, lock files, and timestamps in your batch scripts effectively.