Last active
March 10, 2025 08:06
-
-
Save xobs/e83d19e0f2c81edc6db77611bd37b852 to your computer and use it in GitHub Desktop.
Relink a directory after extracting it with 7zip. Replace all symlinks with `mklink /d`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extract the archive with 7zip, for example `&"C:\Program Files\7-Zip\7z.exe" x .\file.tar`, | |
# and then run this script on the resulting directory. This will replace all symlinks with | |
# directory links created with `mklink /d`. | |
# | |
# This script requires you to run it in Administrator mode, or have Developer Mode enabled. | |
param ( | |
[string]$rootDirectory | |
) | |
if (-not (Test-Path $rootDirectory -PathType Container)) { | |
Write-Host "Invalid directory: $rootDirectory" | |
exit 1 | |
} | |
Get-ChildItem -Path $rootDirectory -Recurse | ForEach-Object { | |
$target_path = $_ | Select-Object -ExpandProperty Target | |
if ($target_path -and $_.DirectoryName -and -not $_.Parent.FullName) { | |
$target_path_full = $_.DirectoryName + "\" + $target_path | |
# If the item is a symbolic link and a directory, then it is in an invalid form | |
# that Windows can't understand. We need to recreate it using `mklink /d` | |
if ($target_path_full -and (Test-Path $target_path_full -PathType Container)) { | |
# Remove the symlink | |
Remove-Item -Force $_.FullName | |
# Create a new symlink using mklink /d in the directory pointed to by $_.DirectoryName | |
cmd /c "mklink /d `"$($_.FullName)`" `"$target_path`"" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment