Robocopy or Robust File and Folder Copy application is very popular amongst in Windows system administrators. Robocopy will copy the local or given file to the remote or destination location.
The general syntax of
robocopy command is like below.
- SOURCE is the source file or directory we want to copy
- DESTINATION is the destination file or directory we want to copy
- OPTIONS are different features or behaviors we want to use like recursive, verbose, extension filter etc.
robocopy SOURCE DESTINATION FILE OPTIONS
The most basic operation with
robocopy is copying sources folders to the destination. In this example, we will copy the directory named
mytest to the destination folder
> robocopy mytest yourtest
After copy operation, some statistics are provided by
Startshows start time of copy operation
Sourceshows source directory
Destshows destination directory
Filesshows file filter
Optionsshows given options
Totalshows total files and folders count
Copiedshows copied files and folders count
Skippedshows not copied files and folders
Copy To Remote System
We can also copy file and folders to the remote system. We will provide remote system hostname or IP address before the destination path. We can use
\\10.5.2.30 to specify remote system.
In this example we will copy source directory named
mytest to the remote system with IP address
192.168.122.46 under the path
Users\ismail with the named
> robocopy mytest \\192.168.122.46\Users\ismail\yourtest
Copy Only Given File Types or Extensions
robocopy provides some filtering features while copying files. We can specify the filenames and extensions we want to copy. This will skip other file named and extensions. We will provide the file name or extension after the destination folder.
In this example, we only want to copy
*.txt extension files.
> robocopy mytest yourtest *.txt
We can see from the output that only one file is copied but there was more than one file. Copied file name and byte count is also printed.
By default, only first level directories are copied to the destination. If we need to copy all level and subdirectories folders we should enable
> robocopy mytest yourtest /S
There is a very useful feature for robocopy where source files are not copied and just listed. This feature can be useful if we want to check existing files and folders. We can use
/L option for print list of files and directories.
> robocopy mytest yourtest /L
As we can see from the output that all files and folders are skipped and not copied.
List Files Recursively
While robocopy is mainly used to copy files from remote to local or local to remove we can use
/S options in order to list remote files. We can merge two options list and recursive to list all level files and folders.
> robocopy mytest yourtest /L /S
Copy Files Lower Than Given Size
While copying files we can specify the size of files we want to copy. In this example we will copy files those sizes are lower than
1K . We will use
/MAX option for this.
> robocopy mytest yourtest /S /MAX:1000
As we can see from results some of the files are copied but some of them are not copied because of the size restriction wi
Copy Files Higher Than Given Size
The reverse of previous option is
/MIN . This will only copy files higher than given size. In this example we will copy files higher than
1K which is 1000 kilobyte.
> robocopy mytest yourtest /S /MIN:1000
The default behavior of
robocopy is copying files. Sometimes we may need to move files. This can be done with
robocopy too. We will provide
/MOV option in order to move files and folders.
> robocopy mytest yourtest /S /MOVE
While doing copy and move operations we can also need some logs, especially in bulk data operations. We can write log about specified operations with
/LOG option. We can also specify the log file name y adding at the end of
/LOG option. In this example, we will write
robocopy operation log to log file named
backup.log . This will prevent regular output
> robocopy mytest yourtest /S /LOG:backup.log
The log file can be listed with
type command in MS-DOS command line like below.
> type backup.log
Copy Given File Properties
Files and folders may have different properties. These properties can be used for different purposes like the listing owner, audit information, timestamps etc. We can copy these attributes too with the robocopy command. Here is the attributed we can specify for the copy. We will also use
/copy: option by adding the property we want to copy.
- `D` is used to copy data properties
- `A` used for attributes
- `T` for time stamps
- `S` is used for NTFS access control list simply ACL
- `O` is used for the owner information
- `U` is used for Auditing information
In this example, we will copy time stamp attributes or properties with the following command.
> robocopy mytest yourtest /copy:T
Copy All Attributes with /copyall
We can also copy files with all properties of attributes with the
/copyall parameter like below. This is equal to the
> robocopy mytest yourtest /copyall
Delete or Remove Destination Files and Directories That No Longer Exist In The Source
If we are trying to make an identical copy of the local files and directories to the remote we need to purge or remove remote files and directories that no longer exist in the source. We can use
/purge option for this.
> robocopy mytest yourtest /purge