Xcopy replicates files and directories, including subdirectories. _dos/bat

Source: Internet
Author: User
Tags file copy goto naming convention
Xcopy
Copy files and directories, including subdirectories.

Grammar
xcopy Source [destination] [/w] [/P] [/C] [/v] [//////////////////////[///////////////////////L] h] [{/a|/m}] [/n] [o] [/x] [/exclude:file1[+[file2]][+[file3]] [{/y|/-y}] [/z]

parameters
Source
Required. Specifies the location and name of the file to be copied. The parameter must contain a drive or path.
Destination
Specifies the target of the file to be copied. This parameter can contain a drive letter and a colon, a directory name, a file name, or a combination of them.
/w
The following message is displayed and waits for your response before you begin copying files:
Press any key to begin copying file (s)

/P
Prompts you to confirm whether you want to create each destination file.
/C
Ignore the error.
/V
Validate each file when writing to the destination file to ensure that the destination file is exactly the same as the source file.
/q
Suppresses the display of XCOPY messages.
/F
Displays the source and destination file names when copying.
/L
Displays a list of files to copy.
/g
Creates a decrypted destination file.
/D[:MM-DD-YYYY]
Copies only those source files that have been changed after a specified date or a specified date. If you do not include the MM-DD-YYYY value, xcopy copies all new Source files that are newer than the existing destination file. This command-line option allows you to update files that have changed.
/u
Copy only the files that are already in the destination from source.
/I
If Source is a directory or contains wildcard characters, and destination does not exist, Xcopy assumes that destination specifies the directory name and creates a new directory. Xcopy then copies all the specified files to the new directory. By default, Xcopy prompts you to specify whether destination is a file or a directory.
/s
Copies non-empty directories and subdirectories. If omitted, the/s,xcopy will work in a directory.
/e
Copy all subdirectories, including empty directories. Use the/e,/s, and/T command-line options at the same time.
/t
Copy only the subdirectory structure (that is, the directory tree) and do not copy the files. To copy an empty directory, you must include the/e command-line option.
/k
Copies the file and retains the property in the destination file if the source file has a read-only property. By default, Xcopy deletes read-only properties.
/R
Copy a read-only file.
/h
Copy files that have hidden and system file attributes. By default, Xcopy does not replicate hidden or system files.
/A
Copy only the source files that have the archive file property settings. /A does not modify the archive file properties of the source file. For information about how to set archive file properties by using attrib, see.
/m
Copy the source file with the Archive file property settings. Unlike/A,/m closes the archive file attribute of the file specified in the source. For information about how to set archive file properties by using attrib, see.
/n
Create a copy using an NTFS short file or directory name. You need/n when you copy a file or directory from an NTFS volume to a FAT volume or when the target file system requires a FAT file system naming convention (that is, 8.3 characters). The destination file system can be FAT or NTFS.
/O
Copy file ownership and discretionary access control List (DACL) information.
/x
Copy file auditing settings and system access control List (SACL) information (including/O).
/exclude:filename1[+[filename2]][+[filename3]]
Specifies a list of files that contain strings.
/y
Suppresses prompting you to confirm that you want to overwrite an existing destination file.
/-y
Prompts you to confirm that you want to overwrite an existing destination file.
/z
Replicated over the network in restartable mode.
/?
Display Help at the command prompt.
Comments
Use/V
XOX

Using/exclude
Lists each string in a separate row for each file. If any of the strings listed match any part of the absolute path of the file to be copied, the file is excluded from the replication process. For example, if the string "\obj\" is specified, all files under the OBJ directory are excluded. If the string ". obj" is specified, all files with the. obj extension are excluded.

Using/z
If a connection is lost during replication (for example, if the server used for the connection is offline), the replication process is restored after the connection is reconnected. /z also displays the percentage of replication operations completed per file.

using/y in Copycmd environment variables
You can use/y in copycmd environment variables. Use/-y on the command line to override the command. By default, you are prompted to overwrite unless you run copy from within a batch script.

Copy an encrypted file
Copying an encrypted file to a volume that does not support EFS can result in an error. You should first decrypt the file or copy the file to a volume that supports EFS.

Ancillary files
To attach a file, specify a single destination file, multiple source files (using wildcard characters or file 1 + file 2 + file 3 format).

Default value for Destination
If you omit the destination,xcopy command, copy the file to the current directory.

Specify whether destination is a file or a directory
If destination does not contain an existing directory and does not end with a backslash (\), the following message appears:

Does destination specify a file name
or directory name on the target
(F = file, D = directory)?

If you want to copy one or more files to a file, press F. If you want to copy one or more files to a directory, press D.

Using the/I command-line option suppresses the display of this message, which causes Xcopy to determine that the destination is a directory when the source is multiple files or a directory.

to set the archive property of a destination file using the xcopy command
The xcopy command creates a file with an archive property setting, regardless of whether this property is set in the source file. For more information about file properties and attrib, see.

compare xcopy and diskcopy
If the disk's subdirectories contain files, and you want to copy them to a disk with a different format, use the xcopy command instead of diskcopy. Because the diskcopy command is a track to copy a disk, you need the same format for both the source and destination disks. The xcopy command does not have this requirement. You typically use xcopy unless you need a full copy of the disk image.

Xcopy's exit code
To handle the exit code returned by Xcopy, use the errorlevel parameter on the IF command line of the batch program. For an example of a batch program that uses "if" to handle exit codes, see. The following table lists each exit code and description.

Exit code Description
0 file copy no errors.
1 No files were found to copy.
2 users terminated xcopy by pressing CTRL + C.
4 An initialization error occurred. There is not enough memory or disk space, or an invalid drive name or syntax was entered on the command line.
5 A disk write error has occurred.

Example
To copy all files and subdirectories (including all empty subdirectories) to drive B from drive A, type:

Copy Code code as follows:

xcopy A:b:/s/e

To include any system or hidden files in the previous example, add the/h command-line option as follows:

Copy Code code as follows:

xcopy A:b:/s/e/h


To update the files in the \reports directory using the files changed after December 29, 1993 in the \rawdata directory, type:

xcopy \rawdata \reports/d:12-29-1993

To update all files that exist in \reports in the previous example, regardless of the date, type:

xcopy \rawdata \reports/u

If you want to obtain only the list of files that you want to copy from the previous command, and do not copy the files, type:

xcopy \rawdata \reports/d:12-29-1993/l > Xcopy.out

File Xcopy.out lists all the files that you want to copy.

To copy the \customer directory and all subdirectories to the directory on network drive H: \\Public\Address, keep the read-only property and prompt you when creating a new file on H:, type:

xcopy \customer h:\public\address/s/e/k/P

To issue the previous command, make sure that Xcopy creates the directory when the \address directory does not exist, and suppresses the display of messages to display when the new directory is created, adding the/I command-line option as follows:

xcopy \customer h:\public\address/s/e/k/p/i

You can create a batch program to perform an XCOPY operation and use the IF batch command to handle an exit code when an error occurs. For example, the following batch program uses the replaceable xcopy source and target parameters:

Copy Code code as follows:

@echo off
REM Copyit. BAT transfers all files in all subdirectories of
REM the source drive or directory (% 1) to the destination

REM drive or directory (% 2)

xcopy%1%2/s/e

if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit

: lowmemory
echo Insufficient memory to copy files or
echo Invalid drive or command-line syntax.
Goto exit

: Abort
echo You pressed CTRL + C to end the copy operation.
Goto exit

: Exit

To use this batch program to copy all files in the C:\Prgmcode directory and its subdirectories to drive B, type:

Copyit C:\prgmcode B:

The command interpreter replaces%1 with B with C:\Prgmcode:. For%2, then use the Xcopy and/e and/s command-line options. If Xcopy encounters an error, the batch program reads the exit code and moves to the label indicated in the appropriate IF ERRORLEVEL statement, and then displays the corresponding message and exits the batch program.

XOX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.