Not for the faint of heart, but this is doable if you combine a couple of tools in a Powershell scriptI think the simplest approach would be to use the Powershell Community Extensions http://pscx.codeplex.com/ for finding source junctions and creating new ones at the target. Then, use SetACL http://setacl.sf.net/ to save the ACL from the source, then restore them to the newly created junction.Should not be too many lines of code :-)
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.
Windows for home | Previous Windows versions | Files, folders, and storage
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
-
Anonymous
2010-01-22T18:45:14+00:00
7 additional answers
Sort by: Most helpful
-
Anonymous
2010-02-17T09:02:01+00:00 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?...
Unfortunately I also do not know of a way - although I wish there was. If there was sufficient need, I suppose I could write one but for my use-case (moving the \Users\ folder to another drive), a simple batch file worked fine.
Sample:
set userRoot=C:\Users\Media
set oldFolder=%userRoot%\AppData\Roaming
set newFolder=%userRoot%\Application Data
mklink /J "%newFolder%" "%oldFolder%"
attrib +H +S "%newFolder%" /L
Do the above 4 lines for each link you need to reproduce. You can get a list of these via using: dir "%UserProfile%" /AL (see below).
On my Win7x64 machine, there were about 15 Junctions in %UserProfile%, which obviously went missing after I did a robocopy ... /MIR /XL command to move my \Users\ root folder.
---- ---- ---- ---- Example listing of default UserProfile Junctions (Win7x64)
C:\Users>dir "C:\Users\Media" /AL /s
Volume in drive C is SysWin
Volume Serial Number is ...
Directory of C:\Users\Media
2009-10-12 17:21 <JUNCTION> Application Data [C:\Users\Media\AppData\Roaming]
2009-10-12 17:21 <JUNCTION> Cookies [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Cookies]
2009-10-12 17:21 <JUNCTION> Local Settings [C:\Users\Media\AppData\Local]
2009-10-12 17:21 <JUNCTION> My Documents [C:\Users\Media\Documents]
2009-10-12 17:21 <JUNCTION> NetHood [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Network Shortcuts]
2009-10-12 17:21 <JUNCTION> PrintHood [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Printer Shortcuts]
2009-10-12 17:21 <JUNCTION> Recent [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Recent]
2009-10-12 17:21 <JUNCTION> SendTo [C:\Users\Media\AppData\Roaming\Microsoft\Windows\SendTo]
2009-10-12 17:21 <JUNCTION> Start Menu [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Start Menu]
2009-10-12 17:21 <JUNCTION> Templates [C:\Users\Media\AppData\Roaming\Microsoft\Windows\Templates]
0 File(s) 0 bytes
Directory of C:\Users\Media\AppData\Local
2009-10-12 17:21 <JUNCTION> Application Data [C:\Users\Media\AppData\Local]
2009-10-12 17:21 <JUNCTION> History [C:\Users\Media\AppData\Local\Microsoft\Windows\History]
2009-10-12 17:21 <JUNCTION> Temporary Internet Files [C:\Users\Media\AppData\Local\Microsoft\Windows\Temporary I
nternet Files]
0 File(s) 0 bytes
Directory of C:\Users\Media\Documents
2009-10-12 17:21 <JUNCTION> My Music [C:\Users\Media\Music]
2009-10-12 17:21 <JUNCTION> My Pictures [C:\Users\Media\Pictures]
2009-10-12 17:21 <JUNCTION> My Videos [C:\Users\Media\Videos]
0 File(s) 0 bytes
...
-
Anonymous
2010-01-08T15:20:31+00:00 For clarification for others, the option you need to use to resolve the 'looping' issue is /XJ (exclude junction points). However as Jimnms has said, it will not actually make a copy of the junction point itself. I'm not sure how / if you can do this.
-
Anonymous
2009-11-10T08:20:41+00:00 From reading the link you provided, it is just a more detailed version of doing a "robocopy /?". According to the robocopy help page, the /sec command would only copy Data, Attributes and Time stamps where the /copyall copies all of that that plus NTFS access control list (ACL), Owner information and Auditing information. I figured out though, you have to add the /b switch, then it works.
I still haven't been able to get it to copy the directory junctions as junctions rather than the contents. I don't think it is possible. The only options concerning junctions are to exclude file junctions only, directory junctions only or all junctions.
Even after sorting out the copy permissions, every time I try to make a copy of a user's directory with the /SL command, since there is a directory junction that points to itself, it copies that junction over and over. Even if I manage to hit CTRL-C before it gets too long, it sets the hidden and system flag on the directories and adds a deny everyone permission to the directories, which makes it a time consuming pain to remove them all.
-
Anonymous
2009-11-08T14:26:19+00:00 Hi jimnms,
Welcome to Microsoft Answers.
I would suggest you to use the /sec in place of /copyall in the robocopy command and check if you can get the directories with permissions and junction points intact.
Also refer to the below mentioned link for further guidance with the command -
http://technet.microsoft.com/en-us/library/cc733145%28WS.10%29.aspx
Thanks,
Hope this helps. Let us know the results.
Meghmala – Microsoft Support
Visit our Microsoft Answers Feedback Forum and let us know what you think