How to Create a Directory Junction in Batch Script
A Directory Junction is a powerful feature of the NTFS file system that acts as a pointer, or a "wormhole," from one folder to another. When a program or a user accesses the junction point, the file system transparently redirects them to the target directory. This is incredibly useful for moving large folders (like game installations or user data) from a small SSD to a larger hard drive without breaking the original application's path.
This guide will teach you how to use the built-in mklink /J command to create Directory Junctions, explain the crucial differences between junctions, symbolic links, and shortcuts, and cover the essential requirements, such as running your script as an Administrator.
The Core Command: mklink /J
The mklink (Make Link) command is the standard Windows utility for creating various types of links. To create a Directory Junction, you must use the /J switch.
Critically, mklink requires elevated privileges. You must run your batch script from a command prompt that has been "Run as Administrator."
The syntax is:
mklink /J "LinkName" "TargetFolder"
"LinkName": The path for the junction you are creating. This path must not already exist."TargetFolder": The path to the existing, original folder that the link will point to.
Junction vs. Symbolic Link vs. Shortcut
These terms are often confused, but they are very different.
| Feature | Directory Junction (/J) | Directory Symbolic Link (/D) | Shortcut (.lnk) |
|---|---|---|---|
| Nature | A file-system-level pointer. Transparent to most applications. | A more modern file-system-level pointer. Transparent to applications. | A separate .lnk file that is not transparent. cd will not work on it. |
| Target Location | Local drives only. Can point from C: to D:, but cannot point to a network share (\\server\share). | Local or Network. Can point to a local folder or a network share. | Can point to anything, anywhere. |
| Creation Command | mklink /J | mklink /D | Requires VBScript/PowerShell. |
For redirecting a folder on a local machine, a Directory Junction is often the most compatible and straightforward choice.
Basic Example: Creating a Directory Junction
Let's create a simpler path, C:\GameData, that points to a deeply nested folder where a game stores its files.
The game's data at initial state is in: C:\Users\Admin\AppData\LocalLow\MyCoolGame\SaveData
@ECHO OFF
REM This script must be run as Administrator.
SET "TARGET=C:\Users\Admin\AppData\LocalLow\MyCoolGame\SaveData"
SET "LINK=C:\GameData"
ECHO Creating Directory Junction...
mklink /J "%LINK%" "%TARGET%"
Output:
Creating Directory Junction...
Junction created for C:\GameData <<===>> C:\Users\Admin\AppData\LocalLow\MyCoolGame\SaveData
Now, if you run DIR C:\GameData, Windows will show you the contents of the SaveData folder. If you CD C:\GameData, your prompt will change, and you will be operating inside the target folder transparently.
Key mklink parameters explained
/J: (Required for Junctions) Creates a Directory Junction.Link: The path for the new junction point. This directory should not already exist.Target: The existing directory that the junction will point to.
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
This is the most common failure. mklink is a protected command.
You do not have sufficient privilege to perform this operation.
Solution: Run as Administrator
Your script must be executed from a command prompt with administrative privileges. Right-click your .bat file or cmd.exe and select "Run as administrator."
Problem: The Link or Target Already Exists
- If the Link path already exists as a file or folder,
mklinkwill fail. - If the Target path does not exist,
mklinkwill also fail.
Let's see the error if Link exists:
Cannot create a file when that file already exists.
Let's see the error if Target doesn't exist:
The system cannot find the path specified.
Solution: Check Before Creating
A robust script should verify the state of both paths before attempting to create the link.
IF EXIST "%LINK%\" (ECHO [ERROR] Link path already exists. & GOTO :EOF)
IF NOT EXIST "%TARGET%\" (ECHO [ERROR] Target path does not exist. & GOTO :EOF)
mklink /J "%LINK%" "%TARGET%"
Problem: Linking to Network Shares (UNC Paths)
A Directory Junction cannot point to a network location.
The Error in Action
REM This will FAIL.
mklink /J "C:\NetworkShare" "\\Server\Share"
Output:
Local volumes are required to complete the operation.
Solution: Use a Directory Symbolic Link (/D)
If you need to link to a network share, you must use a symbolic link instead of a junction.
mklink /D "C:\NetworkShare" "\\Server\Share"
Practical Example: Moving a User's Downloads Folder
This script automates the process of moving the Downloads folder from a user's C: drive to a D: drive to save space, then creates a junction so the system continues to function seamlessly.
@ECHO OFF
SETLOCAL
REM This script must be run as Administrator.
SET "SOURCE_FOLDER=%USERPROFILE%\Downloads"
SET "DEST_FOLDER=D:\UserData\Downloads"
ECHO --- Moving Downloads Folder ---
IF NOT EXIST "%SOURCE_FOLDER%\" (ECHO [ERROR] Source folder not found. & GOTO :End)
MKDIR "%DEST_FOLDER%" 2>NUL
ECHO Step 1: Moving existing files from C: to D:...
REM Use Robocopy to move files and folders reliably.
robocopy "%SOURCE_FOLDER%" "%DEST_FOLDER%" /E /MOVE /COPYALL
ECHO Step 2: Removing the original (now empty) Downloads folder...
RMDIR "%SOURCE_FOLDER%"
ECHO Step 3: Creating the junction point...
mklink /J "%SOURCE_FOLDER%" "%DEST_FOLDER%"
ECHO.
ECHO [SUCCESS] The Downloads folder has been moved and linked.
:End
ENDLOCAL
Conclusion
The mklink /J command is an invaluable tool for transparently redirecting folders on local drives. It is perfect for managing large data folders and saving space on your primary drive without reconfiguring applications.
To successfully create a Directory Junction, always remember:
- You must run the script as an Administrator.
- The link name must not exist, but the target folder must exist.
- Junctions (
/J) are for local volumes only. For network shares, use a symbolic link (/D).
By mastering mklink, you gain a powerful technique for flexible and efficient file system management.