robocopy copy files (reproduced)

Friends who often perform file management operations are not satisfied with the built-in copy function of the Windows system, because it is too slow. So everyone uses software such as FastCopy and TeraCopy to speed up copying, but do you know that Windows 7 has built-in fast copying function?

This feature is RoboCopy, a command-line directory copy command that has been part of the Windows Resource Kit since Windows NT 4.0, and then adopted as a standard feature in Windows Vista, Windows 7, and Windows Server 2008. This function of Windows 7 has also been upgraded - it can already support multi-threading, which means that the copying speed can be greatly improved. And the feature creates two fully structured mirror copies of files without duplicating any unwanted duplicate files, while also allowing you to preserve all related file information including date and time stamps, security access control lists (ACLs) and more. Let's take a look at the usage of this command:

Command syntax : ROBOCOPY source destination [file [file]…] [options]

source: source directory (drive:\path or \\server\share\path)
target: target directory (drive:\path or \\server\share\path)
file: file to copy (name/wildcard: default is " *.*")

copy options

/S: Copy subdirectories, but do not copy empty subdirectories.
/E: Copy subdirectories, including empty subdirectories.
/LEV:n: Copy only the first n levels of the source directory tree.
/Z: Copy files in restartable mode.
/B: Copy files in backup mode.
/ZB: Use restartable mode; use backup mode if access is denied.
/EFSRAW: Copy all encrypted files in EFS RAW mode.
/COPY:Copy Flags:: Contents of files to copy (default is /COPY:DAT). (Copy flags: D=data, A=attributes, T=timestamp). (S=Security=NTFS ACL, O=Owner Info, U=Audit Info).
/DCOPY:T: Copy directory timestamp.
/SEC: Copy files with security (equivalent to /COPY:DATS).
/COPYALL: Copy all file information (equivalent to /COPY:DATSOU).
/NOCOPY: Do not copy any file information (use with /PURGE to take effect).
/SECFIX: Fix file security for all files, even skipped files.
/TIMFIX: Fix file times for all files, even skipped ones.
/PURGE: Delete target files/directories that no longer exist in the source.
/MIR: mirror directory tree (equivalent to /E and /PURGE).
/MOV: Move files (delete from source after copying).
/MOVE: Move files and directories (delete from source after copying).
/A+:[RASHCNET]: Add the given attribute to the copied file.
/A-:[RASHCNET]: Remove given attributes from copied files.
/CREATE: Only create directory trees and zero-length files.
/FAT: Create target files using only 8.3 FAT filenames.
/256: Turn off very long path (> 256 characters) support.
/MON:n: Monitor source; run again if more than n changes are found.
/MOT:m: monitor source; run again in m minutes if changed.
/RH:hhmm-hhmm:Run Hours – How long a new copy can be started.
/PF: Check run hours on a per-file (rather than per-step) basis.
/IPG:n: Gap (ms) between program packets to free up bandwidth on low speed lines.
/SL: Copy a symbolic link against the target.
/MT[:n]: Use n threads for multithreaded replication (default is 8). n must be at least 1, but no greater than 128. This option is incompatible with the /IPG and /EFSRAW options. Use the /LOG option to redirect output for best performance.

file selection options

/A: Copy only files with the archive attribute set.
/M: Copy only files with archive attribute and reset archive attribute.
/IA:[RASHCNETO]: Include only files with any given attribute set.
/XA:[RASHCNETO]: Exclude files with any given attribute set.
/XF files [files]...: Exclude files matching the given name/path/wildcard.
/XD directory [directory]...: Exclude directories matching the given name/path.
/XC: Exclude changed files.
/XN: Exclude newer files.
/XO: Exclude older files.
/XX: Exclude redundant files and directories.
/XL: Exclude orphaned files and directories.
/IS: Include identical files.
/IT: Contains adjusted files.
/MAX:n: Maximum file size - Exclude files larger than n bytes.
/MIN:n: Minimum file size – excludes files smaller than n bytes.
/MAXAGE:n: Maximum file age – exclude files older than n days/dates.
/MINAGE:n: Minimum file age – exclude files older than n days/dates.
/MAXLAD:n: Maximum Last Access Date - Exclude files that have not been used since n.
/MINLAD:n: minimum last access date – exclude files used since n. (If n < 1900 then n = n days, else n = YYYYMMDD date).
/XJ: Exclude junctions. (usually included by default).
/FFT: Assume FAT file times (2 second granularity).
/DST: Make up for the 1-hour DST time difference.
/XJD: Exclude junction points for directories.
/XJF: Excludes junction points for files.

retry option

/R:n: Number of retries for failed replicas: defaults to 1 million.
/W:n: Waiting time between retries: Default is 30 seconds.
/REG: Saves /R:n and /W:n in the registry as default settings.
/TBD: Waiting for share name to be defined (retry error 67).

logging options

/L: List only – do not copy, timestamp or delete any files.
/X: Report all extra files, not just selected ones.
/V: Generate verbose output, also showing skipped files.
/TS: Include the timestamp of the source file in the output.
/FP: Include the full path name of the file in the output.
/BYTES: Print size in bytes.
/NS:no-size - do not log file sizes.
/NC: No Category - Do not log file categories.
/NFL: No file list - do not log file names.
/NDL: No Directory Listing - Do not log directory names.
/NP: No Progress - Don't show the copied percentage.
/ETA: Displays the expected arrival time of copied files.
/LOG:file: Output status to log file (overwrite existing log).
/LOG+:file: Output status to logfile (appends to existing log).
/UNILOG:file: Output status to log file in UNICODE (overwrite existing log).
/UNILOG+:file: Output status to logfile in UNICODE (append to existing log).
/TEE: Output to console window and log file.
/NJH: No job header.
/NJS: No job summary.
/UNICODE: Output status in UNICODE.

job options

/JOB:作业名称:从命名的作业文件中提取参数。
/SAVE:作业名称:将参数保存到命名的作业文件
/QUIT:处理命令行后退出(以查看参数)。
/NOSD:未指定源目录。
/NODD:未指定目标目录。
/IF:包含以下文件。

这么多的命令我不会用怎么办?你可以到官网下载微软推出的Robocopy GUI,它提供交互界面使得使用简化不少:

【复制测试】

我将一共达7.9G的《阿凡达》高清电影文件,通过Robocopy、系统内置的复制功能和Terecopy三项分别复制到相同的目的地,复制过程中基本没有进行其他系统操作,得出复制完成所需要的时间并比较

1、通过Robocopy命令复制文件,使用最大线程128,花费的时间为6分02秒

2、使用系统内置的文件复制功能,使用秒表计时显示时间为6分22秒

3、使用Terecopy复制,显示使用5分32秒

测试速度大小结果:Terecopy > Robocopy命令 > 内置复制功能,可见Robocopy与专业的复制软件在速度上有一定的差距,而且易用性比较差。但是在没有安装其他复制软件的情况下,要想快速复制大量的文件,Robocopy命令还是一个不错的选择。

----------------------------------------------------

----------------【例子】-------------------
实例一:文件,想怎么复制就怎么复制
  [实现效果]
  随时将源文件夹中的纯文本(TXT)、Word文档(DOC)还有BMP、TIF图像文件复制到目标文件夹中
,这是在“资源管理器”中直接拖放所做不到的。[简单命令]
robocopy d:\work e:\back *.txt *.doc *.bmp *.tif /s
 [参数讲解]
  将d:\work文件下的TXT、DOC、BMP、TIF复制到e:\back文件夹,其他文件则不复制。/s是表示包括
除空文件下的所有子目录,如果没有则仅复制d:\work下的文件而不包括子目录。
  [举一反三]
  (1)如果要将文件夹下的所有文件包括空文件夹全部复制到目标文件夹,命令为:robocopy
d:\work e:\back /e
  /e表示复制所有子目录包括空文件夹。
  (2)如果只想复制两个层级内的文件(再下的层级里的文件则不拷贝),输入命令:robocopy
d:\work e:\back /e /lev:2
  /lev:2表示复制的层级,比如指定层级后d:\work\soft(二级)下的文件会被复制,而
d:\work\soft\office(***)下的文件则不被复制。
实例二:垃圾文件不复制
  [实现效果]When backing up data, it is inevitable that junk files similar to *.bak and *.tmp will also be copied to the target folder. With Robocopy, the
junk files we often encounter can be cleaned out of the house when copying, thereby generating a Clean folders.
  [Simple command]
  robocopy d:\work e:\back /e /xf *.tmp *.bak
  [Explanation of parameters]
  /xf specifies the file type that does not perform the copy operation, and the following file names support wildcards. If the parameter is /xd, it means to exclude
the folder specified later.
Example 3: I don’t want to
  [realize the effect]
  when copying large files, in order to save time, some large files need not be copied temporarily, and it is not too late to perform the copy operation when there is free time
.
  [Simple command]
  robocopy d:\work e:\back /e /max:6000000
  [Parameter explanation]
  Only copy files not larger than 6000000bytes, and /max: is followed by a value directly, and the value unit is byte. If it is /min:1000, then
copy files not smaller than 1000bytes. Both parameters can be used at the same time to define the range of the copied file size.
Example 4: As long as the files modified this week
  [realize the effect]
  Which documents were processed this week? edited those files? Copy these modified documents to a folder, and then
have a good aftertaste. This example is to copy TXT, DOC, XLS, PPT and other documents created or modified within the last seven days in d:\work to the e:\look directory
.
  [simple command]
  robocopy d:\work e:\look /s *.txt *.doc *.xls *.ppt /maxage:7
  [parameter explanation]
  /maxage: Indicates the furthest modification time, and the modification time is older than this file will not be copied. /maxage: can be followed by numbers
or dates. When the number is less than 1900, it means the number of days. Otherwise, you have to use YYYYMMDD to indicate the specific date.
   For example, /maxage:20040210 means that files modified after February 10, 2004 are allowed to be copied. The corresponding parameter
is /minage, which means the latest modification time, and files newer than this time will not be copied.
  [Inference]
  robocopy d:\work e:\look /s *.txt *.doc *.xls *.ppt /maxage:7 /minage:1
  means that only files modified in the past seven days except today are allowed to be copied.
  Relative to the modification time is the latest access time, /maxage refers to the farthest access time, and files older than this time will not be copied.
/minage refers to the latest access time, files newer than this time will not be copied.
Example 5: My Document Arranging Station
  [Implementation Effect]
  When copying in the above example, all documents will bring their own path information, and the document will also copy the folder where it is stored.
This brings a lot of inconvenience to our browsing and viewing. We have to click on multi-layer folders to see the files. Can we directly copy all the files to
one folder? This needs to be achieved with XXCOPY.
      [simple command] 
  xxcopy d:\work e:\look /s /in*.txt /in*.doc /sg /yy /da:2004-02-02 /DB:2004-02-10
/DA:1997-01-01
  [ Parameter Explanation]
  This command copies all the TXT and DOC files generated in d:\work from February 2, 2004 to February 10, 2004 directly to the e:\look directory, without creating a new subfolder, open e:\
look to see all files. The grammatical parameters of XXCOPY and Robocopy are quite different.
When specifying the file type, XXCOPY will use /in*.* to indicate the date. /da means after the specified time, /db means before the specified time.
Expressed as YYYY-MM-DD.
  In addition, /sg means to copy files to the same folder, /yy means to automatically consider Yes in all places where the user is required to answer, and
no user intervention is required during execution.
Example 6: Complete cloning
  [Implementation effect]
  The default copy method we usually adopt is incremental copy, that is, copy the content in the source folder to
the target There may be a large number of files and folders in the source folder that have been deleted long ago and are useless.
If you want to make the backup exactly the same, that is, delete the files that exist in the target folder but not in the source folder when copying.
  [Simple command]
  robcopy d:\work e:\look /mir
  [Parameter explanation]
  The function of /mir is equivalent to /purge /e, and /purge means to clean up the files (folders) that exist in the target folder but not in the source folder. pass
This command can ensure that the source folder and target folder structure are exactly the same as the file. This is very useful when creating mirrored backups.
Example 7: Move files
  [realize the effect]
  What we talked about before is copying. If we want to move the folder to the target folder, how to do it?
  [Simple command]
  robocopy d:\work e:\look /move /e
  [Explanation of parameters]
  Among them, /move means to move.
Example 8: I only need the folder skeleton
  [realize the effect]
  The author's work needs to periodically create and organize a large number of folders. The folder structure is the same every time, but the files are different.
If it was before, you would have to simply repeat it one by one, but now you don't need it. Robocopy and XXCOPY allow you to easily
copy folder structures.
  [Simple command]
  xxcopy d:\work e:\mystruct /t /yy /dl3
  [Parameter explanation]
  Copy the three-level folder structure of d:\work to e:\mystruct. Among them, /t means to create a folder structure, /dl3 means that the copied
folder structure is ***, and the number can be input by yourself. If there is no such parameter, the entire folder structure will be created by default.
  It is relatively troublesome for Robocopy to create a folder. The /create parameter not only needs to create a structure, but also creates 0-byte files of all files in the folder. It
is not a real structure, so we have to take a different approach.
  robocopy d:\work e:\mystruct /e /lev:3 /minage:19000101
  Specify a time for Robocopy that it is impossible to create a file, because of the parameter /e, although no file meets the requirements (there is no file in your
machine earlier than January 1, 1900), Robocopy will still faithfully create the file folder, so that the purpose of generating a folder structure is achieved
. In addition, /lev:3 indicates the level of the copied folder, which can also be entered by yourself.
Example 9: I don’t want to hide files
  [Implement effect]
  By default, hidden files are not displayed, but they will be copied along with the folder during the copying process. Can
these hidden files be excluded from the scope of replication?
  [Simple command]
  robocopy d:\work e:\back /e /xa:h
  [Parameter explanation]
  Robocopy supports operations on attributes. The above command will copy all files in the source folder, but all files with hidden attribute
will not be copied. /xa:h means to exclude those with hidden attributes from the scope of replication, and the corresponding one is /ia, which means to include only those with
specified attributes. Attributes include R: read-only, A: archive, S: system, H: hidden and so on.
  [Inference] (1) In addition, Robocopy also has parameters such as /a+ and /a- to operate on file attributes, which respectively indicate adding or removing certain attributes
  to a file after the file is copied.   robocopy d:\work e:\back /ia:rs     means only copy files with read-only and system attributes.   (2) robocopy c: d: /-a: rsh




  Indicates that after the file is copied to the D drive, the read-only, system, and hidden attributes of the copied file will be removed to facilitate viewing and modification.
Example 10: NTFS permissions follow
  [realization effect]
  When using Windows 2000 and Windows XP, when multiple users use a computer or manage servers, it is often necessary to specify
user access and modification permissions for folders (as shown in Figure 1, The partition needs to be NTFS). However, the permissions we set cannot be retained by the general copy method
, which means that we have to reset the user permissions when the server is backed up. However, Robocopy allows us to
copy attributes, user permissions, and owners to the target folder when copying files.

5 wonderful examples of backing up two friends and neighbors - mastering Robocopy and xxcopy
  [simple command]
  robocopy d:\work e:\back /e /copyall
  [parameter explanation]
  /copyall is equal to /copy:datsou, which means that the information of all source folders will be copied Copy to the target folder, where D: file data,
A: file attributes, T: time information, S: permission information, O: owner information, U: audit information.
  [Inference]
  If the permissions of the source folder are modified and the permissions of the target folder need to be updated, we don’t have to repeat the copy, just enter
robocopy d:\work e:\back /is /copy:sou
  if there is no /copy Parameter, the default is /copy:dat, there is no information about the latter three items.
Example 11: Centralized backup to the file server
  [realization effect]
  It is hoped that the corresponding data can be backed up to the file server every day to avoid data loss.
  [Simple command]
  robocopy d:\work \ctips_serverarticlessshare /e /copyall
  [Explanation of parameters]
  It can be seen from this that robocopy fully supports the copying of file server shared folder data.
  [Inference]
  (1) If we create an environment variable, such as defining %databack% as \ctips_serverarticles\back, then just
enter corresponding command: robocopy d:\work %databack% /e /copyall to copy d: All files under \work are backed up under
\ctips_serverarticles\back. Of course, if there is not enough space in \ctips_serverarticles\back, just change
the value of the environment variable to another folder, such as \ctips_serverarticles ewback, and the above command can copy the data to the new
folder without changing it.
  (2) If you just want to back up the data to the shared folder named after your machine in the shared folder, it will be more
convenient to use xxcopy: xxcopy d:\work\ctips_serverarticles/$HOST$ /e /yy where /$HOST$ means your machine name, if your
machine name is CTIPS_SYJ, it will copy the files in the d:\work folder to the \ctips_serverarticles\CTIPS_SYJ folder
. If your machine name is CTIPS_MLB, after executing the above command, you can copy the files in the d:\work folder to
\ctips_serverarticles\CTIPS_MLB folder. Since the names of the machines in the same LAN will not be the same, after executing the above command,
it can be guaranteed that the data will not be copied to the same folder.
Example 12: Back up as soon as you move
  [realize the effect]
  Some work content is very important, and we need to pay attention to backup all the time, but how can we have such good memory and patience? Let's leave this
to Robocopy. Robocopy can monitor the folders to be backed up all the time, as long as the folders are modified to a certain time and program,
Robocopy will start backing up immediately. It will keep watching the folder unless terminated by the user himself.
  [Simple command]
  robocopy d:\work e:\back /e /copyall /mot:1 /mon:2
  [Parameter explanation]
  The above command means to copy all the information of the folder to the target folder and monitor it. After executing this command, the command prompt window
is shown in Figure 2. Once the time has passed for one minute, and the source folder has at least 2 or more modifications, Robocopy will automatically start another process to
perform the above backup operation. Robocopy can monitor the source folder structure, file and folder names, size, and last modification time. Here,
even when information such as attributes and permissions are modified, Robocopy will record the number of modifications.
5 wonderful examples of backing up two friends and neighbors - Robocopy and xxcopy are fully mastered.
  /mot is followed by time (minutes), which refers to the shortest time for Robocopy to perform operations; /mon is followed by the minimum number of file modifications
. During the monitoring process, the time and number of modifications will be automatically recorded, and only when the above two conditions are met at the same time, Robocopy will start to execute
, after the execution is completed, the two data will be automatically cleared, and the folder will be monitored again, unless the user closes the command prompt window or presses the
Ctrl+Break key.
  [Inference]
  If you want to monitor and back up the corresponding folder after booting, you can put the above command in the "Startup" group.
Example 13: Not backing up before the time
  [realize the effect]
  The copying process will always occupy system resources. It will be much more convenient if you can set the copying and backup to start when you are not by the computer
. For example, if you leave work at 5:50 every day, you hope that Robocopy will automatically start backing up the corresponding data when it reaches 6 o'clock after get off work.
  [Simple command]
  robocopy d:\work e:\back /e /rh:1800-1830
  [Inference]
  After running, the copy operation will wait until the specified time to start (as shown in Figure 3). Among them, the RH format is /RH:hhmm-hhmm, which specifies
the time period when Robocopy starts, and the copy will be suspended outside this time period. The time is in 24-hour format, 4 numbers long, from 0000 to
2359, and the two numbers cannot be the same.
5 wonderful examples of backing up two friends and neighbors - Mastering Robocopy and xxcopy
Example 14: Scheduled task backup
  The above methods of time backup and monitoring folder backup will require re-execution of the above commands every time the system is restarted, which is far from
smart enough. If it can cooperate with Windows' scheduled tasks, all problems will be solved. Take for example that we want to automatically call
Robocopy to start monitoring the backup folder at 10:00 every day, let’s talk about the cooperation between scheduled tasks and Robocopy.
  Step 1: Create a new BAT file
Create a new batch file autobak.bat, enter the following command
robocopy d:\work e:\back /e /copyall /mot:1 /mon:2
  Step 2: Create a scheduled task
Select "Start → Settings → Control Panel", Double-click the Scheduled Tasks icon. Drag and drop the autobak.bat batch file to the "Task Scheduler
" window, then double-click the newly generated autobak.bat icon, and set its properties in the pop-up window.
  Step 3: Set the start time
Switch to the "Schedule" tab, click the "Task Schedule" drop-down list box, select "Daily" from it, set the "Start Time" to
10:00, and click "Advanced" to set the start and end schedules. date (as shown in Figure 4).
  After passing the above settings, when the set time comes, the machine will automatically back up and execute batch files, and automatically back up and monitor folders.
5 wonderful examples of backing up two friends and neighbors - Mastering Robocopy and xxcopy
Example 15: Cleaning up files is also good In addition
  to the general copy function, XXCOPY also has a file cleaning function, which can quickly clean up unnecessary files from the hard disk. The following commands
will clear files and folders. Please create a test folder first to master its operation before operating the folder.
1. Clear all useless files
xxcopy d:\work*.tmp /s /h /yy /rs
/rs means to delete the specified files directly from the source folder without copying, /h means to specify hidden and system files.
2. Clear the folder
xxcopy c: emp /rmdir /db:2002-01-01 /yy
Delete the modified folder in c: emp before January 1, 2002.
3.删除空文件夹
xxcopy c: emp  /rmdir /sz:0 /yy
删除c: emp下所有空文件夹,/sz:0指定大小为0的文件(夹)。
4.删除0字节文件
xxcopy c: emp*.*  /sz:0 /s /h /yy /rs
通过以上四种方式的组合,我们可以轻松实现对文件及文件夹的整理工作。
最后,还有一几个小技巧告诉你,希望能有利于你更好地使用XXCOPY和Robocopy。
★将命令变成任务
在Robocopy加上“/save:任务名”可以将当前操作记录为任务文件,通过“/job:任务名”就可以方便调
用,不需要再输入一长串的命令了。此任务文件可以用纯文本编辑器(如记事本)来编辑。
★将复制备份过程录下来
默认情况下,Robocopy的操作过程只是在命令提示符中显示,我们可以让它把操作过程输出为TXT文件,
加上“/log:文件名.txt”即可。  

Guess you like

Origin blog.csdn.net/bodybo/article/details/128596488