• Skip to primary navigation
  • Skip to content
CMIT 215 M1

CMIT 215 M1

Presentation

  • Home
  • References
  • Comments

-PRESENTATION TOPIC-

CREATE A BATCH FILE THAT WILL RUN THE WINDOWS 7 ROBOCOPY UTILITY TO SYNCHRONIZE FILES BETWEEN TWO SYSTEMS / DEVICES ON A SCHEDULED BASIS



An Overview Of How To This Topic Was Executed

  • Researched what robocopy is and how it works
  • Created two “systems” or “devices” to synchronize
    • Created two disk partitions in a virtual machine (System 1) X: and (System 2)Y:
    • Placed files and folders in the X: drive to be robocopied/synchronized with the Y: drive
  • Created a newSynch.bat batch file that runs minimized and copies the files and folders from the X: drive to the Y: drive
    • Minimized running of the batch file automatically prevents interruption to the user
  • Created a task in Windows Task Scheduler that automatically executes the batch file

A Brief Explanation Of Robocopy…

Robocopy, short for robust copy, is a program that allows file copying with far more control over the copying process than copy and xcopy.  This control is obtained by the use of switches, a forward slash followed by an option such as “/s” (this switch copies subdirectories excluding empty directories).  The syntax for using robocopy is as follows:

robocopy <Source> <Destination> [<File>[…]] [<Options>]

For example, one would copy the file “token.txt” from the c: drive to a d: drive by typing:

robocopy c:\ d:\ token.txt

The following thumbnails show screenshots of the steps of the robocopy syntax example immediately above.

D: drive before robocopy Robocopy from c: to d: Results of robocopy D: drive after robocopy

To copy all the files from the c: drive to a d: drive (sub directories will not be copied) , the syntax would simply be:

robocopy c:\ d:\

Running this command again would only update the file(s) in the target drive (d:) if the files in the source location had changed.  Also note that no wild cards are needed to copy all the files from one location to another.

A list of options and an explanation of what they do is available at Reference 4.  In this presentation the switches “/copyall”, “/e”, and “/purge” will be used.  The “/copyall” switch copies all the file data and all its properties.  This switch requires the command window to be run as an administrator which will be vital to know when scheduling the task.  The “/e” switch copies all the sub directories including empty sub directories.  The “/purge” switch will delete files and folders in the destination location that have been deleted from the source location.

Devices To Be Synchronized…

Using a VMware virtual machine with Windows 7 loaded, two disk partitions were created to represent two systems / devices (an X: and Y: drive).  The X: drive volume is labeled as System 1 and the Y: drive volume is labeled as System 2.  A selection of files and folders, some of which are empty, have been placed on System 1, the X: drive.  These devices can be seen by clicking the two thumbnails below.

System 1 (X:) System 2 (Y:)

The Batch File…

A batch file is a script file that has a .bat extension.  It is not an executable, instead DOS and Windows run the commands listed in the batch (script) file.  A batch file is created using plain text in notepad (or any text editor), listing the commands to run, and saving the name with the .bat extension.  Windows also processes vbs scripts using the Windows Script Host.  These have a .vbs extension.

If you’ve created a .bat file, Windows will create an icon that looks like this:

If you’ve created a .vbs file, Windows will create an icon that looks like this:

The batch file that follows will synchronize the files between the two devices (the X: drive and Y: drive).

Here is what each line of the script does:

  • @echo off – Prevents the commands from showing in the console window
  • if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start “” /min “%~dpnx0” %* && exit – Runs the command window in minimized mode.  This prevents the user from being disturbed when the batch file is executed.  When the batch file is run, IS_MINIMIZED is not defined so this let sets IS_MINIMIZED=1 and starts a copy of itself minimized.
  • The (“” /min “%~dpnx0” %*) is what tells the start command to empty the window (“”), run minimized (/min), the path to the script (“%~dpnx0”), passes through the script parameters (%*), the initial script finishes (&& exit).  See Reference 1 for more information about running the batch file minimized.
    • Variable expansion is used to obtain the script location from the variable dpnx0.
    • Variable expansion means to replace the variable enclosed by the % with it’s value.  For more information on variable expansion, see Reference 2.
    • The ~ expands the variable dpnx0.
    • The d gets the drive letter of the script location.
    • The p expands the path.
    • The n expands the filename.
    • The x expands the file extension.
    • The 0 is the argument to the script path, basically execute the file whose information it’s just obtained.  For more on enhanced variable substitution see Reference 3. 
  • Robocopy X:\ Y:\ /copyall /e /purge will copy all the files and folders in the X: drive to the Y: drive with all the file and folder parameters.  Every time this script runs, only files and folders that have changed will be copied.  Files that are on the Y: drive that no longer exist on the X: drive will be deleted thus syncing the two systems / devices.
  • Start %userprofile%\desktop\notify.vbs is a vbs script that runs once the syncing is complete.  Because the execution of the batch file occurs without any indication to the user, it is convenient to get a notification that the operation occurred with no hangups.  The ability to get a pop up message box is not available in shell scripting, but is available with vbs scripting because it uses the Windows script host.
  • When the syncing is complete the notify.vbs script will be called, the user will get the following message box:

  • The vbs script for notify.vbs is very simple and is as follows:

  • Note that the batch file does not wait for the user to click OK to end the script.  The script moves to the exit line and exits (closes the console).  The message box serves only as a notification the process is complete and closes when either the OK or X button is clicked.  If there was no exit command, the console would stay open and you would be able to review all the actions that were performed by robocopy that remain in the console buffer.
  • For a multi-series YouTube tutorial on shell scripting, see Reference 5.

Now To Schedule The Batch File To Run Automatically…

In the Start>Run window, type compmgmt.msc to load the Computer Management Console.  Select Task Scheduler in the Computer Management pane.

Select Create Task in the Actions pane.  The Create Task window will open.

Give the task a name, select whether the task should run whether the user is logged on or not or only when logged on, and MOST IMPORTANTLY, check the box that says Run with highest privileges.  If this box is not checked, the batch file will not run. 

Click the Triggers tab.  In the Begin the task: dropdown, select On a schedule.  In this example, select the Weekly radio button in Settings.  Select a Start: date and time.  Set the Recur every: to 1 weeks on: check Sunday.  2:30 AM on Sunday was chosen to produce the least impact to a user even though the batch file essentially runs in the background anyway without disturbing the user.  Click the OK button.

Click on the Actions tab and click the New… button.

In the Action: dropdown select Start a program.  Browse to the location of the batch file.  Click the OK button.

Click the OK button after creating the action.

A popup will appear asking for your password, enter it.  Click the OK button.

Run a test on the task you’ve created to make sure it will work.  In the Computer Management pane, select Task Scheduler > Task Scheduler Library.  Then in the middle window, find your task name, in this case Synchronize Two Devices, and select it.  In the Actions pane, click Run.

Now the Y: drive (System 2) will contain all the files and folders robocopied from the X: drive (System 1).

That is how a batch file is created to sync two systems or devices and scheduled to run automatically.

Author Email: ronniepkupfer@smccme.edu

Last Updated: October 23, 2018

CLICK TO JUMP TO THE TOP OF THE PAGE

Reader Interactions

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in