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

Anonymous
2009-11-07T14:52:57+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?

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.

0 comments No comments
Answer accepted by question author
Anonymous
2010-01-22T18:45:14+00:00

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 :-)

Was this answer helpful?

0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-10-10T20:12:21+00:00

    Unfortunately that program is suffering the same problem as Robocopy.  It sees the junctions to itself over and over until it reaches the max directory limit and errors out.  I just tested it by running a junction backup on my user profile and it took a very long time.  When it finished, looking at the backup file I see the multiple links to itself caused by the looping junction points:

    [\Documents and Settings\USERNAME\AppData\Local\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data]

    I found this old post of mine because I replaced my C: partition with an SSD and I want to move the User profiles back to C: (except Documents, Music, etc.), and was searching to see if it was possible.  It looks like moving an existing profile will require changing hundreds of registry entries and re-creating dozens of junctions per user profile.  It will probably be easier just to re-install Windows.  I'm not a programmer, but I've done some UNIX shell scripting and it seems like it shouldn't be hard to come up with a program that can automate the process of copying files, permissions, recreating junction points and modifying the registry entries to point to the new location.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-04-03T18:13:03+00:00

    Found a great tool that would do just that.

    Provides a means to backup, transfer or restore the special filesystem 'junctions' within Windows Vista and Windows 7 user-profiles. Also has the capability to repair a Windows installation from which the junctions have been accidentally deleted.

    http://sourceforge.net/projects/junctionbox/

    Was this answer helpful?

    0 comments No comments
  3. 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

    ...

    Was this answer helpful?

    0 comments No comments