How to copy a directory with all permissions and junctions intact?

Is there any way to copy a directory with junctions in it and have it copy them as junctions, not the contents of the junction?

I'll try to make a long story short, but I just spent a few hours cleaning up after robocopy and Windows 7's hidden junctions.  I have Windows 7 installed on a spare HD so I can play around with it, get used to it, test things and figure out how I want to set it up before taking the plunge and replacing XP full time.

Just as an experiment, I wanted to create a new user account, but put its user directory on another drive and use a symlink to point to it from C:\Users.  I created a new user account called Test, logged in so that its home directory was created, and then logged out.

I discovered that moving or copying the files, either in explorer or a command prompt didn't move or copy the directory junctions.  I then tried robocopy since it has options to copy all security, owner and auditing info.  There is also an option to "copy symbolic links versus the target."

The exact command line I used was:
     robocopy C:\Users\Test D:\Users /E /COPYALL /SL /R:0

First, it didn't even copy the permissions, the few files it did copy inherited the destination parent directory's permissions, and it added a strange permission which I'll get to in a minute.  It didn't take long before it stopped with an error.  I don't remember the error because I've rebooted several times since then, and I don't care to try again just to get the exact error message to post here.

What happened is that there is a hidden directory junction in C:\Users\Test\AppData\Local.  The junctioin name is "Application Data" and it links to C:\Users\Test\AppData\Local (the same directory it's in).  This caused it to keep copying the "Application Data" folder into itself until the max folder name length was exceded and it stopped.

When I tried deleting the directory, I kept getting an error that it couldn't delete the directory because it wasn't empty.  I had taken full control of the directory, but I still got the error and the directory (from what I could see) was empty.

Here's where the strange permission comes in.  For some reason, the copies of the "Application Data" directory were set to hidden/system, and had the Everyone group's "List folder / read data" permission set to deny, which is why I couldn't see them, even in administrator command prompt.

Once I got the permissions sorted out, I still couldn't delete the directory because the name was too long.  Windows kept telling me to rename it so it was shorter and try again.  I finally managed to delete it by renaming a few "Application Data" directories to "a" and then moving part of it to another directory.

Question Info

Last updated April 14, 2020 Views 5,886 Applies to:
Not for the faint of heart, but this is doable if you combine a couple of tools in a Powershell script

I think the simplest approach would be to use
the Powershell Community Extensions for finding source junctions and creating new ones at the target. Then, use SetACL to save the ACL from the source, then restore them to the newly created junction.

Should not be too many lines of code :-)

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.