In Windows 10, symbolic links (symlinks) break when the source file or directory is moved or renamed because they store the absolute or relative path to the target. To prevent this, you have a few options:
1. Use Absolute Paths for Symbolic Links
By default, Windows symlinks use absolute paths, which means they will break if the source is moved. However, if you ensure the symlink always points to the correct absolute path, it will work as long as the file exists at that location.
How to Create a Symlink with Absolute Path
-
Using Command Prompt (Admin):
mklink /D "C:\path\to\target\link" "C:\full\path\to\source\file"
- Replace
/D with /H for a hard link (if applicable) or /J for a directory junction.
-
Using PowerShell (Admin):
New-Item -ItemType SymbolicLink -Path "C:\path\to\target\link" -Target "C:\full\path\to\source\file"
Problem:
- If the source is moved, the symlink still points to the old path and breaks.
2. Use a Junction Instead of a Symlink (For Directories)
Junctions (/J) are similar to symlinks but are more resilient in some cases (though they still break if the source moves).
Create a Junction:
mklink /J "C:\path\to\target\link" "C:\path\to\source\directory"
Problem:
- Still breaks if the source is moved.
3. Use a Hard Link (For Files Only)
Hard links (/H) do not break when the source is moved because they reference the file by its file system ID rather than its path.
Create a Hard Link:
mklink /H "C:\path\to\target\link" "C:\path\to\source\file"
Limitations:
- Only works for files, not directories.
- Both the source and target must be on the same drive.
- If the source file is deleted, the hard link still works (but the data is lost if all links are deleted).