Copy utilities: ROBOCOPY, XCOPY

ROBOCOPY [Best option]

Mirror (will delete extra files from destination)

/MIR

# typical mirror example -- /XJ helps with appdata*
robocopy /MIR /NDL /XJ C:\path\to\source c:\path\to\destination

Copy (keeps extra files at destination)

/E

Depending on other switches it can OVERWRITE same filename on destination.

Move

/MOV

To delete a folder

To delete a folder that contains files you can't delete via the GUI use:
robocopy /MIRROR C:\path\to\some-empty-folder c:\path\to\folder\you\want\to\delete

Other Useful Switches

/R:0 don't retry a copy if it fails (useful because in most cases you can just re-run the same robocopy command again in the end)

/B Use if you're getting access denied errors.You need to be running in an Administrator context or otherwise have backup rights to use this flag (B stands from Backup mode).

/LOG+:C:\log.txt keep a log in a text file

/L do nothing but list the files you would copy/move/delete

/SEC will keep security settings intact (users access rights)

Advanced options

When copying very large files over a potentially unstable and relatively slow connection use Restartable mode (/Z) With this option, should the copy be interrupted while any particular file is partially copied, the next execution of robocopy can pick up where it left off rather than re-copying the entire file.That option could be useful when copying very large files over a potentially unstable and relatively slow connection. [But it might be slower over very fast connections because it] writes a header block to the file to track how far it has gotten in the copy. It has to update this header after every write, which is slow. Without restarts the header doesn't have to be written, thus the copy is much faster. – Corey Mar 22 '19 at 5:18

Examples and more options at https://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx

Troubleshooting

Destination folder disapears

If you mirror a Disk (e.g. D:\) to a folder (e.g. C:\folder), the folder will be created with hiden and system attributes and will disapear from windows explorer. Fix it like this:

attrib -h -s -a "C:\folder"

When copying AppData you see the same folder/subfolder/folder/subfolder/...

TLDR: Add this option to ROBOCOPY: /XJ

This recursive file structure is indeed caused by the implementation of junction points in the NTFS file structure by Microsoft. Essentially, some "folders" are not actual folders at all, but junction or reparse points, which essentially reroute a directory name to another directory. This is much like mapping a drive letter to a directory or another drive, and the Windows system is designed to handle this NTFS structure situation behind the scenes.

In Vista and 7, Microsoft decided to standardize some of the typical Windows settings for users, documents, etc., which had varied over time. To maintain backward compatibility, the old standards were also retained BUT WITH JUNCTIONS REPARSING THEM TO REDIRECT INQUIRIES INTO THE NEW ACTUAL DIRECTORIES. Most programs work right through these junctions without even noticing. For example, C:\Documents and Settings points to C:\Users. Even though the former directory does not actually exist, it APPEARS to have the files and directories under the latter, and a change in the contents of either directory will be reflected in the other, since they are in fact, a single directory.

The difficulty comes from certain Reparse Points which essentially redirect to a parent directory. This causes a recursive cascade for unwary programs or users who are unaware of the proper handling of the junctions (even though the junctions are designed to be transparent to software looking for either directory). The usual "subdirectory explosion" situation comes from the fact that the \Documents and Settings\$USER$\Application Data folder (consistent with XP convention) is a junction point in Vista to \Users\$USER$\AppData\Roaming, which in turn can contain the Application Data junction as a subdirectory...

Under normal Windows permissions, the junctions and their directories are inaccessible and hidden. If permissions are changed or programs working outside Windows are not prepared to encounter these junctions, duplication and recursion is destined to occur. For those using powerful low-level command interfaces such as Repair Console, it is best to check the capabilities and switches on the commands by using the /? help switch (for example, Robocopy /?, xcopy /?, rd /?, etc.). If you are not sure what you are doing, be sure to backup ahead of time with a reliable Windows program, with proper use of Restore Points.

Another useful discussion is here: http://www.vistax64.com/vista-general/58035-nested-application-data-directory.html

XCOPY (for VERY old PCs without robocopy)

xcopy c:\users\ e:\backup\ /K/R/E/H/Y/C/I/D

Will copy all files and their attributes from c:\ to e: You can stop it and run the same command again to continue

Will not copy owner information (security permissions)

Will not delete files in destination that aren't found in source (so it's not a complete MIROR of source to destination). If you want to create a miror use ROBOCOPY /MIROR

What to copy

/S Copies directories and subdirectories except empty ones.*
/E Copies directories and *subdirectories, INCLUDING empty ones. Same as /S /E

/A Copies files with the archive attribute set, doesn't change the attribute.
/M Copies files with the archive attribute set,turns off the archive attribute.
/D :date Copies files changed on or after the specified date . If no date is given, copies only those files whose source time is newer than the destination time.
/U Only copy files that already exist in destination (Update)

/H Copies hidden and system files also.
/K Copies attributes . Normal xcopy will reset read-only attributes.

Overwrites and errors

/Y Overwritesexisting files without prompting.
/R Overwrites read-only files also.
/C Continues copying even if errors occur.
/I Assumes that destination is a directoryif it does not exist and copying more than one file.

Display settings

/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.

Other

/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes empty directories and subdirectories.
/L DOESN’T COPY – Just Listthe files that would be copied.
/N Copy using the generated short names.

/-Y Prompts you before overwriting existing files.
/P Prompts you before creating each destination file.
/W Prompts you to press a key before copying.
Topic revision: r7 - 29 Jul 2022, NickDemou
Copyright © enLogic