I have looked at this question: Recursively copy a directory entirely as symlinks, preserving current symlinks and if I'm reading it right I want to do the same thing but in Windows.

So, I have this:


I want that directory structure to be the exact same on the target directory minus the Dir1 but all the files to be symlinks pointing to the source files. So, I end up with this:

Target_Dir\Dir_A\File.ext <- file is symlink, folders are created
Target_Dir\Dir_A\Dir_A_A\file2.ext <- file is symlink, folders are created
Target_Dir\Dir_B\File3.ext <- file is symlink, folders are created

How can I achieve this? I'm also looking for a batch script or something that can be executed on a schedule to copy the new files every so often skipping the already created symlinks, kinda like a synchronization job minus the real copying of the files but instead (sym)linking them.

  • @PimpJuiceIT Thank you! That is exactly what I was requesting. It works well except for just one thing, I have a folder which name has a "!" character in the end and the batch has copied the folder without the "!" character and also has created a file with the folders name. To be more precise the name is like "AAAA AAAA! (1234)" and the batch has created a folder named "AAAA AAAA (1234)" and a file named "AAAA AAAA.mkv" (without the numbers on the filename), how can I fix this? The file points to a file with the same name in the source dir but that file doesn't exist.
    – churchill
    Jun 22 '20 at 6:13
  • I have also noticed that all the files that had a "!" in their filename has been copied without that character.
    – churchill
    Jun 22 '20 at 6:21
  • @DavidPostill Thanks! And sorry, I don't get it. What should I change in the pure batch script?
    – churchill
    Jun 22 '20 at 7:30
  • @PimpJuiceIT Yes, after some testing I also got the batch to copy some files but no others, and after a subsequent test it didn't work at all, I don't know why it worked that one time. Would be awesome if you could make a Powershell script to see if maybe this doesn't happen in there. Thanks!
    – churchill
    Jun 25 '20 at 23:17

PowerShell: Recursive folder copy with symbolic link files

You can use get-childitem to create an array variable and loop over the iterated object properties with some conditional if logic to help create a workable solution for your needs.

Essentially this...

  • creates a source-matching destination folder structure
  • creates a source-matching destination symbolic link file structure pointing to each source-matching file

You only need to set the $src value to point to the source root folder location and the $dest value to point to the root destination folder location—the other logic will do all the rest.

$src = "C:\Source\Folder\"
$dest = "C:\Destination\Folder\"
$src = $src.Replace("\","\\")

$i = Get-ChildItem -LiteralPath $src -Recurse
$i | % { Process {
    $apath = $_.FullName -Replace $src,""
    $cpath = $dest + $apath
    If(!(Test-Path (Split-Path -Parent $cpath))){New-Item -ItemType Directory -Force -Path (Split-Path -Parent $cpath)}
    If(!$_.PSIsContainer){If(!(Get-Item $cpath -ErrorAction SilentlyContinue)){New-Item -Path $cpath -ItemType SymbolicLink -Value ([WildcardPattern]::Escape($_.FullName)) -Force}}

Supporting Resources

  • 1
    @Churchill .... One thing I noticed testing from a non-admin account, in order to create symlinks, I had to elevate permission. Keep this in mind as you test just in case you have the same issue. Jun 29 '20 at 3:04
  • 1
    Run powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\AnyPScript.ps1" just like that from a batch or cmd to execute the logic within the Powershell script. I assume you are already familiar with that too but just in case I wanted to mention. Jun 30 '20 at 17:29
  • 1
    Thanks! I've tested this with all my files and it works great. Thanks for all your time helping me.
    – churchill
    Jul 2 '20 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.