You can try something link this:
function createSymbolicLinks ($source, $destination, [switch]$recurse) {
Get-ChildItem $source -Recurse:$recurse | ? { !$_.PSISContainer } | % {
$destpath = $_.Fullname -replace [regex]::Escape($source), $destination
if(!(Test-Path (Split-Path $destpath))) {
#Create missing subfolders
New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
}
cmd /c mklink $destpath $($_.FullName) | Out-Null
}
}
#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse
I believe this will fail if the same filename already exists in c:\folderc
. So if you need to replace a file in c:\folderc
with a symbolic link from d:\folderd
, you need to extend it to remove the existing file.
UPDATE: This will only go down one level with the recurse
option. It's not the prettiest solution, but it should work.
function createSymbolicLinks ($source, $destination, [switch]$recurse) {
Get-ChildItem $source | % {
if($_.PSIsContainer -and $recurse) {
Get-ChildItem $_.FullName
} else {
$_
}
} | ? { !$_.PSIsContainer } | % {
$destpath = $_.Fullname -replace [regex]::Escape($source), $destination
if(!(Test-Path (Split-Path $destpath))) {
#Create missing subfolders
New-Item (Split-Path $destpath) -ItemType Directory -Force | Out-Null
}
cmd /c mklink $destpath $($_.FullName) | Out-Null
}
}
#Create symbolic links in c:\folderC for all files in d:\folderD(with recursive search)
createSymbolicLinks -source d:\folderD -destination c:\folderC -recurse