Basic commands (Windows cmd). Xcopy Options and Examples

Copies files and directories, including subdirectories. Runs on the Windows command line.

Command syntax:

xcopy source [target] ] ] [(/a|/m)] ][+] [(/y|/-y)]

Options:

source: required parameter. Specifies the location and names of the files you need to copy. This parameter must include a device or path.

destination: Specifies the destination of the copied files. May include a drive letter followed by a colon, a directory name, a file name, or a combination of these.

/w: Displays the message "Press any key to begin copying file(s)" and waits for your response before starting to copy files.

/p: will prompt before creating each file.

/c: ignore errors.

/v: checks each file after it is written to ensure it is identical to the original one. It is not used in Windows XP. Left for compatibility with MS-DOS files

/q: quiet mode. Does not display messages during operation.

/f: Displays file names when copying.

/l: display the names of the files that will be copied.

/g: Copy encrypted files to a target directory that does not support encryption.

/d[:mm-dd-yyyy] : copy files that were modified after the specified date, including the specified one. If you do not specify mm-dd-yyyy (month-day-year), xcopy will copy files that are newer than those in the destination folder. In general, this command allows you to update files that have changed.

/u: copy only those files that are already in the destination folder. Those. update them.

/i: if the source is a directory or contains wildcards (like *) and the target_object is not specified, then xcopy assumes that the target_object is a directory and creates it. By default, xcopy prompts you for target_object to be a directory or file.

/s: directories and subdirectories that do not contain files will be skipped. If you omit this parameter, xcopy works inside the same directory.

/e: Copy all subdirectories, even if they are empty. Equivalent to the /s /e switch combination. Compatible with the /t switch.

/t: Create a directory structure without copying files. Empty directories and subdirectories are not included in the copy process. To create empty directories and subdirectories, use the /t /e switch combination.

/k: Preserves the read-only attribute when copying. By default it is reset.

/r: Overwrites read-only files.

/h: Copy hidden and system files. By default, xcopy does not copy these files.

/a: copy files that have an archive attribute. The attribute itself is preserved. You can use the attrib command to set this attribute.

/m: almost the same as with the /a key, only in this case the archive attribute will be reset for the source files.

/n: Makes file and folder names short. May be needed when copying from FAT to NTFS. Because FAT (8.3 characters) does not support long names like NTFS.

/o: Copies the file's user information and DACL (discretionary access control list) information.

/x: Copies file audit settings and SACL information (implies the /o switch).

/exclude:file1[+[file2]][+[file3]] ...: a list of files containing strings with criteria for excluding files and folders from the copy process. Each line must be on a separate line in the file. If any of the strings matches any part of the absolute path to the file being copied, that file is excluded from the copy operation. For example, by specifying the string \obj\ or .obj, you can exclude all files in the obj folder or all files with the OBJ extension, respectively.

/y: so that you don’t have to ask when you need to overwrite an existing file.

/-y: The opposite of /y .

/z: copy over the network in resumable mode.

/? : display help. Hints on command parameters.

Examples of using xcopy

How to copy a folder to another folder while maintaining its permissions

When you copy files and folders in Windows Explorer, the permissions of those files and folders will change. The files will be marked as new, and will inherit the permissions of the folder to which we are copying. Your account will be listed as the creator of the file.

Note: You must have write permissions to the folder you are going to copy files to.

To keep the original file permissions, you need to use the following parameters /O, /X, /E, /H and /K, which mean the following:

  • /E - Copies all files and folders, including empty ones.
  • /H - Also copies hidden and system files.
  • /K - Copies attributes. By default, the "Xcopy" command clears the "Read Only" attribute.
  • /O - Copies owner and ACL information.
  • /X - Copies audit settings (requires "/O" parameter).

Note: This procedure does not change inherited permissions. The following inheritable permissions will apply:

  • Security settings applied directly to files and folders are retained.
  • Security settings inherited from the destination folder are lost.
  • The security settings of the destination folder are inherited.
  • When you move files or folders to a FAT drive, those files and folders lose their NTFS permissions because FAT partitions do not support NTFS permissions.

Example

xcopy c:\oldfolder c:\newfolder /O /X /E /H /K , where "oldfolder" is the folder containing the files to be copied and "newfolder" is the destination folder.

In modern versions of Windows it is more convenient to use the program

XCOPY copies files and directories, including subdirectories, on the Windows command line. This is a powerful command for copying files and folders, has more options than the simple COPY command.

XCOPY Utility Options

xcopy source [result] ] ] [(/a|/m)] ][+[file3]] [(/y|/-y)]

  • source - Required parameter. Specifies the location and file names to copy. The parameter must specify either a drive or a path.
  • result- Specifies the location where the files will be copied. The parameter can include a drive name followed by a colon, a directory name, a file name, or a combination of these.
  • /w- Displays the following message waiting for confirmation to start copying: Press any key to start copying files
  • /p- Requests confirmation when creating each result file.
  • /c- Ignores errors.
  • /v- Checks each copied file for compliance with its original.
  • /q- Prevents xcopy command messages from being displayed.
  • /f- Displays the names of source files and result files during the copying process.
  • /l- Displays a list of copied files.
  • /g- Creates unencrypted result files.
  • /d[:mm-dd-yyyy]- Copies only files modified no earlier than the specified date. If you do not include mm-dd-yyyy, the xcopy command copies all source files that are newer than existing result files. This feature allows you to update only changed files.
  • /u- Copies (updates) only those source files that already exist in the result directory.
  • /i- If the source is a directory or the source contains wildcards and the result does not exist, the xcopy command assumes the result is the name of the directory and creates a new directory. xcopy then copies all specified files to a new directory. By default, the xcopy command asks for confirmation whether the result parameter is a directory or a file.
  • /s- Copies directories and subdirectories if they are not empty. If the /s option is not specified, the xcopy command will only work on one directory.
  • /e- Copies all subdirectories, including empty ones. The /e option is used with the /s and /t options.
  • /t- Copies only the subdirectory structure (i.e. tree), not files. To copy empty directories, use the /e switch.
  • /k- Copies files with a read-only attribute, preserving this attribute for copied files whose originals have this attribute. By default, the xcopy command removes the read-only attribute.
  • /r- Copies files with the read-only attribute.
  • /h- Copies system and hidden files. By default, the xcopy command does not copy system or hidden files.
  • /a- Copies only those files that have the “archive” attribute set. Using the /a option does not change the "archive" attribute of the source files. For information about setting attributes using the attrib command, see the See Also link. Also".
  • /m- Copies only those files that have the “archive” attribute set. Unlike the /a option, the /m option clears the archive attribute of copied files. For information about setting attributes using the attrib command, see the See Also link. Also".
  • /n- Copies using short NTFS file system names. The /n option is required when copying from an NTFS file system to a FAT file system, or when the resulting disk requires a file naming convention similar to the FAT (8.3) file system. Files can be written to the FAT or NTFS file system.
  • /o- Copies file ownership and selective access control table (DACL) information.
  • /x- Copies file audit settings and system access control table (SACL) information (assumes /p).
  • /exclude:file1[+[file2]][+[file3]]- Defines a list of files containing strings.
  • /y- Eliminates prompting for confirmation to overwrite an existing target file.
  • /-y- Prompts for confirmation to overwrite an existing target file.
  • /z- Copies over the network in restart mode.

XCOPY Command Examples

To copy all files and subdirectories (including empty subdirectories) from drive D to drive E, enter.

The problems mentioned in the description of the COPY command can be solved using the XCOPY command, which provides much more options when copying. It should be noted that XCOPY can only work with files and directories, not devices.

Command syntax:

Xcopy source [result] [keys]

The XCOPY command has many switches, some of which are described below.

Key /D[:[date]] allows you to copy only files modified on or after the specified date. If the date parameter is not specified, then copying will be performed only if the source is newer than the result.

For example, the command

XCOPY "C:\My Documents\*.*" "D:\BACKUP\My Documents" /D

will copy to the "D:\BACKUP\My Documents" directory only those files from the "C:\My Documents" directory that have been changed since the last such copying or that were not in "D:\BACKUP\My Documents" at all.

Key /S allows you to copy all non-empty subdirectories in the source directory.

With a key /E you can copy all subdirectories, including empty ones.

If a key is specified /C, then copying will continue even if errors occur. This can be very useful for copy operations performed on groups of files, such as when backing up data.

Key /I important for the case where several files are copied and the destination file is missing. When this switch is specified, the XCOPY command assumes that the destination file must be a directory. For example, if you specify the /I switch in the command to copy all files with the txt extension from the current directory to the TEXT subdirectory that does not yet exist,

XCOPY *.txt TEXT /I

then the TEXT subdirectory will be created without additional requests.

Keys /Q, /FAnd/L are responsible for the display mode when copying. When you specify the /Q key, file names are not displayed when copying, and the /F key - the full paths of the source and result are displayed. The /L switch means that only files that need to be copied are displayed (the copying itself is not performed).

With a key /H you can copy hidden and system files, and using the key /R- replace files with the "Read-Only" attribute.

For example, to copy all files from the root directory of drive C: (including system and hidden ones) to the SYS directory on drive D:, you need to enter the command:

XCOPY C:\*.* D:\SYS /H

Key /T allows you to use XCOPY to copy only the source directory structure, without duplicating the files in those directories, and empty directories and subdirectories are not included. In order to still include empty directories and subdirectories, you need to use a key combination /T/E.

Using XCOPY, you can only update existing files when copying (new files are not written). To do this, use the /U switch. For example, if the C:\2 directory contained the files a.txt and b.txt, and the C:\1 directory contained the files a.txt, b.txt, c.txt and d.txt, then after executing the command

XCOPY C:\1 C:\2 /U

in the C:\2 directory there will still only be two files a.txt and b.txt, the contents of which will be replaced by the contents of the corresponding files from the C:\1 directory.

If a file with the Read-Only attribute was copied using XCOPY, then by default this attribute will be removed from the copied file. In order to copy not only data, but also the entire attributes of a file, you must use the key /K.

Keys /Y And /-Y determine whether confirmation must be requested before replacing files when copying. /Y means that such a request is needed, /-Y- not needed.

Unlike the COPY utility, the XCOPY command also allows you to copy a file via the command line, but has more functions. However, it is worth noting that XCOPY allows you to work only with files and directories, but not with devices (for example, a printer). But, as always, a little lyrics first...

In the old days, I had a disk with the Windows XP Sp1 operating system, at that time there were no installation disks with auto-run, so I had to first enter the command shell, and only then run the installation file on the disk. The disk was already quite shabby, small holes were visible, but strangely enough, with the help of the good old Wolves Commander, I managed to copy all the files through the command line, although not on the first try. Moreover, sometimes there were times when the system crashed, and I had to manually copy from one screw to another, this took a day...

xcopy command line

XCOPY source [result] ] ]

[+ file 3]...]

Source– specify the path to the file(s) to be copied.

Result– indicate the location where you want to copy the file via the command line. If this parameter is absent, then the documents will be duplicated in the current directory.

/V– as in the previous version, this parameter is responsible for checking the physicality of documents, but is no longer mandatory, since the verification is carried out by the system itself. It was left only for compatibility with previous versions of MS-DOS.

/EXCLUDE:file1[+file2][+file3]… – allows you to exclude several files or folders from the copying process. Everything is simple here, you just need to specify the names of the objects that should be excluded, but you cannot use special characters such as “*” and “?”. The keys file1, file2, ... point to documents in which lines are written, each line must be in a separate line. If at least one of the lines matches any part of the absolute path of the copied document, then it will not be possible to copy this file using the command line. Let's say in the D:DATA directory there are three documents with the txt extension: data1.txt, data2.txt and data3.txt. Let's open CMD and write the following commands in it:

echo data1.txt>exclude.txt

echo data2.txt>>exclude.txt

In this example, we created a text document exlude.txt in the current directory, which contains two lines data1.txt and data2.txt. The ">>" symbol differs from the ">" symbol only in that it appends data to the end of the document, rather than overwriting the entire content with new information. Okay, now let's try to execute the following directives:

XCOPY *.txt D:DATA /EXCLUDE: exclude.txt – in this example, from the current folder to the D:DATA folder, we copied all documents with the txt extension except for the files data1 and data2.

Another example, if you specify strings of the form OBJ or .OBJ, then all objects from the OBJ folder and all objects with the .OBJ extension will be excluded from the process.

/Z– allows you to duplicate network documents with renewal.

As you know, the file system assigns an owner to each file/document; as a rule, this is the user who created the document. When using commands and XCOPY from the command line, information about the old owner is overwritten, and the owner of the copy becomes the user under whose account the files were actually copied from the command line.

/A And /M– set the mode for archival documents. The first key, after duplicating an object, leaves it with the “Archive” attribute, while the second key disables this attribute upon completion of the manipulation process.

/O– this parameter allows you to copy only those data that were changed no earlier than the specified date (the date y is indicated in the /D[:date] attribute; if the date itself is not specified, then copying will be performed only if the source is newer than the result). For example:

XCOPY "C:My document*.*" "D:RESERVEMy document" /D

Here, only those files will be copied via the command line that have been changed since the last such manipulation or that were not in D: RESERVEMy document.

/S– this key indicates that in addition to documents, the folder must also be copied, provided that it (they) is not empty.

/E– this key is similar to the previous one, with the only difference being that it allows you to copy an empty folder via the command line.

/W– the mode of requesting to press any key before duplication starts will be turned on.

/C– the copying process will continue even if an error occurs

/I– This switch specifies that the destination must be a directory. For example, if you set the key /I in the command to copy all objects with the txt extension from the current directory to a non-existent TEXT subdirectory:

XCOPY *.txt TEXT /I

then the TEXT subdirectory will be created without additional requests.

/Q– this key will not display names during the process

/F– this key will display the full source and result paths during the process

/L– this key will only display those documents that need to be copied, but the process itself will not happen.

/H– allows you to duplicate hidden and system documents.

/K- replace objects with the “Read-Only” attribute

/T– creates a snapshot of the structure of the copied folders, but does not copy the documents themselves, and empty directories are also not included. In order to still include empty directories and subdirectories, you need to use a key combination /T /E.

/U– updates the contents of the directory, that is, copies only those files that are already in the directory.

/Y And /-Y determine whether confirmation must be requested before substitution. /Y means that such a request is needed, /-Y- not needed.

/N– copying using the command line using generated short names.

As mentioned above, when using the COPY and XCOPY utilities, information about the previous owner is overwritten. To prevent this from happening, you can use the command SCOPY from the package Resource Kit, which has the following syntax:

SCOPY source [result]

/ABOUT– allows you to duplicate information about the owner

/A– duplication of information related to auditing access to a file or directory

/S– copying files via the command line from all subdirectories of the specified directory.

Writing to NTFS Streams

And also, I would like to recall such a concept as “copying to streams”. I partially touched on this issue in an article with examples when describing the FileSystemObject method. This feature is only available on the NTFS file system. In a simple version, the essence of the matter is this: you can attach your own stream to each document or disk, and write information directly to the stream, that is, the information will be recorded, but the document itself will not change its size. Let's do a simple experiment, write on the command line:

echo “Hello, word”>new.txt:potok.txt – in this example, we created a text document new.txt, and copied the phrase Hellow, word into the potok.txt stream. If we run the command notepad new.txt, we won’t see anything, but if we run it like this:

notepad new.txt.potok.txt– then we will see the cherished phrase Hellow, word.

Command line xcopy command.

When a file is deleted, the stream associated with it is also deleted. There is one rather primitive way to find out that something is hardwired into our supposedly empty document, this is to place it in an archive, as a result, the zero size of the document will increase during archiving.

Team XCOPY used to copy files and directories while maintaining their structure. Compared to the team COPY has more capabilities and is the most flexible copy tool in the Windows command line

Command line format:

XCOPY source [target] ] ] [+file3]...]

Command line options:

source- Copied files.

target_object- Location or names of new files.

/A- Copying only files with the archive attribute set; the attribute itself does not change.

/M- Copying only files with the archive attribute set; after copying the attribute is removed.

/D:m-d-y- Copying files modified no earlier than the specified date. If no date is specified, only the target files that are older than the source files are replaced.

/EXCLUDE:file1[+file2][+file3]...- A list of files containing lines with criteria for excluding files and folders from the copying process. Each line must be on a separate line in the file. If any of the strings matches any part of the absolute path to the file being copied, that file is excluded from the copy operation. For example, by specifying the string \obj\ or .obj, you can exclude all files in the obj folder or all files with the OBJ extension, respectively.

/P- Output prompts before creating each new file.

/S- Copy only non-empty directories with subdirectories.

/E- Copying directories with subdirectories, including empty ones. Equivalent to the /S /E switch combination. Compatible with /T key.

/V- Checking the size of each new file.

/W- Prompts you to press a key before copying.

/C- Continue copying regardless of errors.

/I- If the target does not exist and multiple files are copied, the target is assumed to specify a directory.

/Q- Prohibition of displaying names of copied files.

/F- Output full names of source and target files.

/L- Displays the names of copied files.

/G- Copying encrypted files to a target directory that does not support encryption.

/H- Copying, among others, hidden and system files.

/R- Overwriting read-only files.

/T- Create directory structure without copying files. Empty directories and subdirectories are not included in the copy process. To create empty directories and subdirectories, use the /T /E switch combination.

/U- Copy only files already present in the target directory.

/K- Copying attributes. Using the XCOPY command typically resets the Read-Only attributes.

/N- Using short names when copying.

/O- Copy owner information and ACL data.

/X- Copying file audit parameters (implies the /O key).

/Y- Suppress confirmation prompt to overwrite existing target file.

/-Y- Prompt for confirmation to overwrite an existing target file.

/Z- Copy network files with resume.

/J- Copy using unbuffered I/O. Recommended for very large files.

Key /Y can be set via the COPYCMD environment variable.

Key /-Y command line overrides this setting.

Examples of using XCOPY

XCOPY /?- provide brief information on using the command.

xcopy C:\users D:\copy1- copy files from the C:\users directory to the D:\copy1 directory. Copying will be performed without subdirectories and only files without the “Hidden” and “System” attributes. The copied files will have the attribute set to Archival. If the directory being copied to does not exist, the user will be prompted with the following message:

What does D:\copy1 mean:
file or directory name
(F = file, D = directory)? D

After answer D the target directory will be created and the copy will be performed to D:\COPY1\. To suppress the request to create the target directory, use the /I option:

xcopy C:\users D:\copy1 /I

xcopy C:\users D:\copy1 /H /Y /C- copying files, including hidden and system ones, suppressing the request to overwrite existing ones and resuming in case of an error. If a file existing in the target directory has the Read-Only attribute, then the copy is not performed. To overwrite such files, use the key /R

xcopy C:\users D:\copy1 /H /Y /C /R /S- copy all files and subdirectories (/S) overwriting existing ones without prompting (/Y), including hidden and system ones. (/H) with overwriting files with the Read-Only attribute (/R) and ignoring errors (/C)

xcopy C:\users D:\copy1 /H /Y /C /R /S /EXCLUDE:C:\users\listnotcopy.txt- the same as in the previous case, but a text file C:\users\listnotcopy.txt specifies exceptions from the copying procedure. Example file contents:

\User1\- exclude the directory C:\users\user1 from copying
All Users exclude the directory C:\users\All Users from copying
de*.*- exclude from copying all files and directories starting with the letter combination "de"

xcopy C:\users\*.exe D:\copy1 /H /Y /C /R /S /EXCLUDE:C:\users\listnotcopy.txt- the same as in the previous example, but only copying executable files with the extension is performed .exe.

xcopy %TEMP%\*.ini D:\copy1\ini /H /Y /C /R /S /I- copying all files with the extension .ini from the temporary files directory to the directory D:\copy1\ini\. If the target subdirectory \ini\ does not exist, then it will be created without prompting the user (/I) .

xcopy %TEMP%\*.ini D:\copy1\ini /H /Y /C /R /S /I /D:09-16-2013- the same as in the previous example, but only those files that have a modified date of September 16, 2013 or older are copied.

xcopy C:\ D:\copy1\LISTDIR /H /Y /C /R /S /I /E /T- create a folder structure of drive C: in the directory D:\copy1\LISTDIR. Files are not copied. Only folders are copied, including empty, hidden and system folders.

xcopy C:\ D:\copy1\LISTDIR /H /Y /C /R /S /I /E /T /D:09-16-2013 recreate the folder structure of drive C: in the D:\copy1\LISTDIR directory, with the modification date of September 16, 2013 and later.

To add new files to directories and update existing ones to newer versions, you can use the REPLACE command.