You want to create a batch script to copy or move large files, or multiple files from one folder to another? Robust File Copy for Windows, or RoboCopy is a great way to go!
To create a simple file copy script, you need just two parameters: source and destination:
robocopy "C:\Folder1" "C:\Folder2"
Save this simple script as *.bat file, run it, and everything that is in Folder1 will be copied in Folder2.
If you want to show the progress of copy job, add switch /tee
robocopy "C:\Folder1" "C:\Folder2" /tee
You can add multiple switches to a robocopy command. Switches that I commonly use in my scripts are:
/e
– Copies subdirectories. This option automatically includes empty directories./is
– Includes the same files./it
– Includes modified files./eta
– Shows the estimated time of arrival (ETA) of the copied files./tee
– Writes the status output to the console window, as well as to the log file.
You can also log the whole operation and save it in .log or .txt file:/log:
– Writes the status output to the log file (overwrites the existing log file)/log+:
– Writes the status output to the log file (appends the output to the existing log file)
Command for recurring copy task with logging would look like this:
robocopy "C:\Folder1" "C:\Folder2" /e /is /it /tee /eta /log:"C:\Logs\log.log"
If you want to dynamically create new log file every time you run a recurring copy task, the best way is to add current date to log file name. Add these lines before robocopy call in you batch script:
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%
Be sure not to forget to add the dynamic variable to log name:
/log:"C:\Logs\log_%SUBFILENAME%.txt"
In the example below, you can see the script that I created for the user to copy backup files from remote PC to server. Script is created to be run manually on remote PC when PC is connected to VPN network:
REM Turn the echo off to hide command prompt lines
ECHO OFF
REM Generate current date
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%
REM Map the remote server drive to use it in destination path
net use s: \\server.contoso.corp\backups
REM Notify the user that file copy has started
ECHO "File copy in progress…"
robocopy "C:\Backup" "S:\remote\" /e /is /it /tee /eta /log:"S:\logs\log_%SUBFILENAME%.txt"
REM Notify the user that file copy is completed
ECHO "File copy completed. Press ENTER to end"
REM pause switch prevents command prompt to close
PAUSE
For scripts that will be run automatically, for example through Windows Task Schedule, don’t use /tee
, /eta
, and PAUSE
switches, and absolutely log the progress, so you have much needed information for debugging if the script doesn’t work as you planned.
I hope this post helped you understand the basics of RoboCopy.
Happy scripting! 😊
For whole documentation on Robust File Copy for Windows, check official Microsoft Documentation