How to Create a Hard Link to a File in Batch Script
In file management, you sometimes need a file to exist in two places at once without creating a copy. While a shortcut (.lnk file) simply points to a file, a hard link is a more powerful construct. It acts as a second, independent name for the exact same data on the disk. Any changes made via one name are instantly reflected in the other because they are the same file.
This guide will teach you how to use the built-in mklink command to create hard links, explain the critical differences between a hard link and a shortcut, and cover the main limitations you'll encounter, such as requiring administrator rights and being restricted to a single disk volume.
The Core Command: mklink /H
The mklink (Make Link) command is the standard Windows utility for creating both symbolic and hard links. To create a hard link, you must use the /H switch.
Crucially, mklink requires elevated privileges. You must run your batch script from a command prompt that has been "Run as Administrator."
The syntax is:
mklink /H "LinkName" "TargetFile"
"LinkName": The new file path you are creating."TargetFile": The path to the existing, original file.
Understanding Hard Links vs. Shortcuts
It is essential to understand that a hard link is not a shortcut.
| Feature | Shortcut (.lnk) | Hard Link |
|---|---|---|
| Nature | A small, separate file that points to the target's path. | A second directory entry pointing to the same data on the disk. |
| Deletion | Deleting the shortcut has no effect on the original file. | Deleting one link does not delete the file data. The data is only removed when the last link to it is deleted. |
| Identity | Is a separate entity. An application opening it sees a .lnk file. | Is indistinguishable from the original file. An application sees it as a normal file. |
| Location | Can point to files on different drives (e.g., C: to D:). | Must be on the same disk volume (e.g., both on C:). |
Think of it this way: a shortcut is a signpost pointing to a house. A hard link is a second front door to the exact same house.
Basic Example: Creating a Hard Link
Let's say we have a master configuration file and we want an application to access it using a different name in another folder (on the same drive).
We have a file: C:\SharedConfig\master.ini
@ECHO OFF
REM This script must be run as Administrator.
SET "TARGET=C:\SharedConfig\master.ini"
SET "LINK=C:\Program Files\MyApp\config.ini"
ECHO Creating hard link...
mklink /H "%LINK%" "%TARGET%"
Output:
Creating hard link...
Hardlink created for C:\Program Files\MyApp\config.ini <<===>> C:\SharedConfig\master.ini
Now, C:\Program Files\MyApp\config.ini exists. If you edit it, you are actually editing master.ini, and vice versa. They are two names for one file.
Key mklink Parameters Explained
/H: (Required for Hard Links) Creates a hard link. If omitted,mklinkcreates a symbolic link.Link: The new link being created. This file should not already exist.Target: The existing file that the link will point to. This file must exist.
mklink can also create symbolic links for files (the default) and directories (/D).
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
This is the most common reason mklink fails. It is a protected command that requires elevation.
You do not have sufficient privilege to perform this operation.
Solution: Run as Administrator
Your script must be run from a command prompt with administrative privileges.
Right-click your .bat file or cmd.exe and select "Run as administrator."
Problem: "The system cannot move the file to a different disk drive."
This error message is slightly misleading, but its meaning is clear: you are trying to create a hard link on a different disk volume than the target file.
Let'see an example of error:
REM This will FAIL. Target is on C:, Link is on D:.
mklink /H "D:\Data\config.ini" "C:\Shared\master.ini"
Output:
The system cannot move the file to a different disk drive.
Solution: Use the Same Volume (or Use a Symbolic Link)
Hard links are a feature of the NTFS file system that operates within a single volume. You cannot create a hard link from C:\ to D:\. If you need to link across drives, you must use a symbolic link (mklink "Link" "Target" without the /H).
Problem: The Source File Does Not Exist
You can not create a hard link to a file that doesn't exist.
The error message will be:
The system cannot find the file specified.
Solution: Use IF EXIST First
Before creating a link, always verify that the target file exists to prevent errors.
IF NOT EXIST "%TARGET%" (
ECHO [ERROR] Target file does not exist. Cannot create link.
GOTO :EOF
)
mklink /H "%LINK%" "%TARGET%"
Practical Example: Centralizing a Configuration File
This script ensures two different applications use the exact same configuration file, which is managed in a central, shared location. This saves space and prevents the configuration from becoming out of sync.
@ECHO OFF
SETLOCAL
REM Run as Administrator.
SET "MASTER_CONFIG=C:\SharedData\global_settings.xml"
SET "APP1_CONFIG=C:\Program Files\AppOne\settings.xml"
SET "APP2_CONFIG=C:\Program Files\AppTwo\config.xml"
ECHO --- Configuration Linker ---
IF NOT EXIST "%MASTER_CONFIG%" (ECHO [ERROR] Master config is missing! & GOTO :End)
ECHO.
ECHO Linking AppOne...
IF EXIST "%APP1_CONFIG%" DEL "%APP1_CONFIG%"
mklink /H "%APP1_CONFIG%" "%MASTER_CONFIG%"
ECHO.
ECHO Linking AppTwo...
IF EXIST "%APP2_CONFIG%" DEL "%APP2_CONFIG%"
mklink /H "%APP2_CONFIG%" "%MASTER_CONFIG%"
:End
ECHO Script finished.
ENDLOCAL
Now, both AppOne and AppTwo will use the same configuration file, and any changes made by one app (if it writes to the config) will be instantly seen by the other.
Conclusion
The mklink /H command is a powerful tool for advanced file management, allowing you to create multiple references to a single file without duplicating data.
For success with hard links, remember the three fundamental rules:
- You must run the script as an Administrator.
- The link and the target must be on the same disk volume (e.g., both on
C:\). - The target file must exist before you can create a link to it.
By using hard links, you can write more efficient scripts that save disk space and reduce the complexity of synchronizing identical files.