robocopy

PowerShell script that uses Robocopy to copy files, verify source and destination and then sends an email on completion.

Here is a basic PowerShell script to copy files also checking if the source and destination locations exist. PowerShell will then send an email if the Robocopy process completed successfully.

Example:

@echo off

set "source=C:\SourceFolder"
set "destination=\\RemoteServer\SharedFolder"
set "smtpServer=smtp.example.com"
set "sender=sender@example.com"
set "recipient=recipient@example.com"

REM Check if source folder exists
if not exist "%source%" (
    echo Source folder does not exist.
    exit /b
)

REM Check if destination folder exists
if not exist "%destination%" (
    echo Destination folder does not exist.
    exit /b
)

REM Check if destination is accessible
dir "%destination%" >nul 2>&1
if not %errorlevel%==0 (
    echo Destination folder is not accessible.
    exit /b
)

REM Perform the copy operation
robocopy "%source%" "%destination%" /E /COPYALL /R:3 /W:10 /MT

REM Check Robocopy exit code
if not %errorlevel%==0 (
    echo Robocopy encountered an error.
    exit /b
)

REM Send email notification
powershell.exe -ExecutionPolicy Bypass -Command "Send-MailMessage -From '%sender%' -To '%recipient%' -Subject 'Robocopy Complete' -Body 'Robocopy operation completed successfully.'"

exit /b

Let’s breakdown some of the variables in the script:

  • ‘source’: Path of the local source folder.
  • ‘destination’: Path of the remote destination folder.
  • ‘smtpserver’: SMTP server address for sending email.
  • ‘sender’: Email address of the sender.
  • ‘recipient’: Email address of the recipient.

So, after the Robocopy operation finishes, the script checks the Robocopy exit code, if the exit code is a success, the script will proceed to send and email using PowerShell ‘Send-EmailMessage’ cmdlet. Please feel free to customize it, you also might need to add variables for mail port and possible username and password of the email sender.

You will also need adjust the execution policy of PowerShell (‘Set-ExecutionPolicy Unrestricted’) if needed.

Using Robocopy to copy files to a remote location without deletion.

Today I am going to provide an example of a Robocopy script I use to copy files, retain security, attributes and provide multi-treaded copying for improved performance.

Script:

robocopy <source> <destination> /E /COPYALL /R:1 /W:5 /MT

Let’s break down the options used in this command.

  • <source>: The path the source directory you want to copy from.
  • <destination>: The path to the destination directory you want to copy to.

Options used:

  • /E : Copies all subdirectories, including empty ones
  • /COPYALL : Copies all file information, including timestamps, Security Attributes and ownership.
  • /R:1 : Specifies the number of retries on failed copies (1 in this example)
  • /W:10 : Specifies the wait time between retries in seconds (5 seconds in this example)
  • /MT :Enables multi-threaded copying, which improves performance by utilizing multiple threads.

Example:

robocopy C:\DataFiles \\RemoteServer\SharedFolder /E /COPYALL /R:1 /W:5 /MT

In this example, all files and subdirectories from ‘C:\DataFiles’ will be copied to the remote location on another server ‘\\RemoteServer\SharedFolder’ without deleting any files. I hope this example helps someone that needs a quick script to copy files to a local or remote location using a fast copy method. You can speed the copy up if you remove /R:1 and /W:5, robocopy will just skip over files that are locked or cannot be copied.

Copying Files With Robocopy

Robocopy (Robust File Copy) is a powerful command-line tool in Windows used for advanced file and folder copying operations. Robocopy provides more control and flexibility compared to copy commands like ‘copy’ or ‘xcopy’. Robocopy has is really useful for tasks such as mirrioring directories, backing up files, or syncing files/folders between different locations. I will provide you with some basic examples on how to use Robocopy today.

Basic Robocopy command:

robocopy <source> <destination>

This command copies files and sub-directories from the source directory to the destination directory.

Example:

robocopy C:\SourceFolder D:\DestinationFolder

This command copies all files and sub-directories from ‘C:\SourceFolder’ to ‘D:\DestinationFolder’

Mirror a directory:

robocopy <source> <destination> /MIR

The ‘/MIR’ option mirrors the source directory to the destination directory, which means it copies files and sub-directories and also removes any files or directories in the destination that no longer exist in the source.

Example:

robocopy C:\SourceFolder D:\DestinationFolder /MIR

This commands mirrors ‘C:\SourceFolder’ to ‘D:\DestinationFolder’, copying any new or changing files and deleting any files or directories in the destination that don’t exist in the source.

Copy files in restartable mode:

robocopy <source> <destination> /Z

The ‘/Z’ option enables restartable mode, which allows the copying process to resume from the point of it was interrupted.

Example:

robocopy C:\SourceFolder D:\DestinationFolder /Z

Like the examples above this command copies files from ‘C:\SourceFolder’ to ‘D:\DestinationFolder’ but this time it’s in a restartable mode.

The last example for today is how to just copy new or changed files, which will prevent files in the destination directory from being overwritten. So only files that are newer or don’t exist are going to exist in the destination folder.

Copy only new or changed files:

robocopy C:\SourceFolders D:\DestinationFolder /XO

I will start adding some advanced methods I use Robocopy for in a production environment, but this tool is just so handy for even backing up personal computers to a NAS or other storage device. Thanks for reading and have a wonderful day!