Xcopy command in Windows - Windows Basics


Wednesday, May 26, 2021

Xcopy command in Windows

The xcopy command is a Command Prompt command used to copy one or more files or folders from one location to another. With many options and the ability to copy entire directories, the xcopy command is similar to, but much more powerful, than the copy command. The robocopy command is similar, but with more options.

Availability of the Xcopy

This command is available from within Command Prompt in all Windows operating systems including Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows 98, and more. See How to open Command Prompt if you need help.

You can also access commands in MS-DOS as DOS commands.

Xcopy command syntax

Use the following syntax for the xcopy command:

xcopy source [destination] [/a] [/b] [/c] [/d [:date]] [/e] [/f] [/g] [/h] [/i] [/j] [/k] [/l] [/m] [/n] [/o] [/p] [/q] [/r] [/s] [/t] [/u] [/v] [/w] [/x] [/y] [/-y] [/z] [/exclude:file1[+file2][+file3]...] [/?]

Note: The availability of some xcopy command switches and command syntax may vary between operating systems.

Xcopy Command Options
sourceThis defines the files or top level folder that you want to copy from. The source is the only required parameter. Use quotes around source if it contains spaces.
destinationThis option specifies the location where the source files or folders should be copied to. If no destination is listed, the files or folders will be copied to the same folder you run the xcopy command from. Use quotes around destination if it contains spaces.
/aUsing this option will only copy archive files found in source. You can not use /a and /m together.
/bUse this option to copy the symbolic link itself instead of the link target. This option was first available in Windows Vista.
/cThis option forces xcopy to continue even if it encounters an error.
/d [ : date]Use the command with /d option and a specific date, in MM-DD-YYYY format, to copy files changed on or after that date. You can also use this option without specifying a specific date to copy only those files in source that are newer than the same files that already exist in destination. This is helpful when using xcopy to perform regular file backups.
/eWhen used alone or with /s, this option is the same as /s but will also create empty folders in destination that were also empty in source. The /e option can also be used together with the /t option to include empty directories and subdirectories found in source in the directory structure created in destination.
/fThis option will display the full path and file name of both the source and destination files being copied.
/gUsing xcopy with this option allows you to copy encrypted files in source to a destination that does not support encryption. This option will not work when copying files from an EFS encrypted drive to a non-EFS encrypted drive.
/hThe command doesn't copy hidden files or system files by default but will when using this option.
/iUse the /i option to force xcopy to assume that destination is a directory. If you don't use this option, and you're copying from source that is a directory or group of files and copying to destination that doesn't exist, the xcopy command will prompt you enter whether destination is a file or directory.
/jThis option copies files without buffering, a feature useful for very big files. This option was first available in Windows 7.
/kUse this option when copying read-only files to retain that file attribute in destination.
/lUse this option to show a list of the files and folders to be copied... but no copying is actually done. The /l option is useful if you're building a complicated command with several options and you'd like to see how it would function hypothetically.
/mThis option is identical to the /a option but xcopy will turn off the archive attribute after copying the file. You can not use /m and /a together.
/nThis option creates files and folders in destination using short file names. This option is only useful when you're using the command to copy files to a destination that exists on an drive formatted to an older file system like FAT that does not support long file names.
/oRetains ownership and Access Control List (ACL) information in the files written in destination.
/pWhen using this option, you'll be prompted before the creation of each file in destination.
/qA kind of opposite of the /f option, the /q switch will put xcopy into "quiet" mode, skipping the on-screen display of each file being copied.
/rUse this option to overwrite read-only files in destination. If you don't use this option when you want to overwrite a read-only file in destination, you'll be prompted with an "Access denied" message and the command will stop running.
/sUse this option to copy directories, subdirectories, and the files contained within them, in addition to the files in the root of source. Empty folders will not be recreated.
/tThis option forces the xcopy command to create a directory structure in destination but not to copy any of the files. In other words, the folders and subfolders found in source will be created but there we be no files. Empty folders will not be created.
/uThis option will only copy files in source that are already in destination.
/vThis option verifies each file as it's written, based on its size, to make sure they're identical. Verification was built in to the command beginning in Windows XP, so this option does nothing in later versions of Windows and is only included for compatibility with older MS-DOS files.
/wUse the /w option to present a "Press any key when ready to being copying file(s)" message. The command will begin copying files as instructed after you confirm with a key press. This option is not the same as the /p option which asks for verification before each file copy.
/xThis option copies file audit settings and System Access Control List (SACL) information. You imply /o when you use the /x option.
/yUse this option to stop the command from prompting you about overwriting files from source that already exist in destination.
/-yUse this option to force the command to prompt you about overwriting files. This might seem like a strange option to exist since this is the default behavior of xcopy but the /y option may be preset in the COPYCMD environment variable on some computers, making this option necessary.
/zThis option allows the xcopy command to safely stop copying files when a network connection is lost and then resume copying from where it left off once the connection is reestablished. This option also shows the percentage copied for each file during the copy process.
/exclude: file1+ file2][ + file3]...This option allows you to specify one or more file names containing a list of search strings you want the command to use to determine files and/or folders to skip when copying.
/?Use the help switch with xcopy to show detailed help about the command. Executing xcopy /? is the same as using the help command to execute help xcopy.

Xcopy command example

Here are some examples of how to use this command:

A. Copy files to new folder

xcopy C:\Files E:\Files /i

In the above example, the files contained in the source directory of C:\Files are copied to the destination, a new directory [/i] on drive E is called Files.

No subdirectories, nor any files contained within them, will be copied because the /s option is not used.

B. Xcopy backup script

xcopy "C:\Important Files" D:\Backup /c /d /e /h /i /k /q /r /s /x /y

In this example, xcopy is designed to act as a backup solution. Try this if you want to use xcopy instead of a software program to back up your files. Put the command as shown above in a script and schedule it to run nightly.

As shown above, the command is used to copy all files and directories [/s] newer than those already copied [/d], including empty directories [/e ] and the hidden file [/h], from the source C:\Important Files to the destination D:\Backup, which is the directory [/i].

For example, there are some read-only files that want to update in destination [/r] and want to keep that attribute after being copied [/k]. The example also wants to ensure that any ownership and auditing settings in the files being copied [/x] are maintained. Finally, since the example is running xcopy in a script, it doesn't need to see any information about the files as they are copied [/q], the example doesn't want to be prompted to overwrite each file [/y] and also don't want the command to stop if it encounters an error [/c].

C. Copy files and folders over the network

xcopy C:\Videos "\\SERVER\Media Backup" /f /j /s /w /z

Here, the command is used to copy all the files, subfolders and files contained in the [/s] subdirectory from the source C:\Videos to the Media Backup destination folder located on a network computer named is SERVER. Some really large video files are being copied, so write buffering should be turned off to improve copying [/j] and since the copying is happening over the network, the author wanted to be able to continue. continue copying if network connection is lost [/z] midway.

The example would also like to be prompted to start the process before actually doing anything [/w] and see all the details about what files are being copied [/f].

D. Duplicate Folder Structure

xcopy C:\Client032 C:\Client033 /t /e

In this last example, the article has a complete source of well-organized files and folders in C:\Client032 for a client. The article created an empty target directory, Client033, for a new client but didn't want any files copied - merely an empty directory structure [/t] to organize and prepare. There are some empty directories in C:\Client032 that are applicable to the new client, so for example wanted to make sure they were copied [/e] as well.

No comments:

Post a Comment