Synchronize Files

These simple batch scripts will demonstrate how you can use Directory Monitor to synchronize files and directories based on the changes that occur.

Option 1 - Synchronize using RoboCopy


RoboCopy has a mirroring option that makes it easy to keep two directories in sync. With the right combination of options in Directory Monitor, this is the siplest way to completely synchronize all changes from one location to another. Because RoboCopy performs intelligent copying, it will only mirror the changes keeping the syncing process nice and quick. All you need is a simple batch file, so let's begin.

robosync.bat
@echo off

rem Parameters: %dirpath% (only works in 2.4.1.10+).
rem Set this to the directory you want to sync all changes into.
set syncpath=C:\Sync\DirMon\

robocopy /MIR "%~1" "%syncpath%"
exit

All you will need to change in this script is the location where you want to the changes to be synchronized. Set the syncpath variable appropriately and you're ready to go. If you are not using at least version 2.4.1.10 of Directory Monitor, you will need to replace %~1 with the path you are monitoring because the %dirname% parameter will not be passed in (bug).

Next up, we need to configure Directory Monitor to call this batch file and set the options in an optimal way to synchronize often without causing conflicts when a lot of changes are happening. Edit or add a new directory to monitor, and go to the Execute tab.


  1. Add the path to the batch file you created earlier to Execute.
  2. Optionally set the Parameters to %dirpath% if you want the path to be passed into the %1 variable (this only works in 2.4.1.10+).
  3. Ensure you set the option to execute sequentially, you don't want multiple RoboCopy instances trying to sync the same directory.
  4. It's a good idea to wait for some inactivity as well so that RoboCopy can batch the work it does rather than trying to sync for every tiny change. This is especially important if you expect the directory to be very busy at times.
  5. If you have the option to run things silently it would be a good idea to enable it since RoboCopy can take some time before the script window closes.
One of the bonuses of using RoboCopy in this way is that even if sync's were missed or the machine was offline, the next event in Directory Monitor that triggers this script will sync everything back to normal.

NB: Be very careful with something like this, RoboCopy will happily delete files if they are deleted in the source. Make sure you use test folder thoroughly before taking an automation script like this to your live directories/shares.

Option 2 - Synchronize only changes from Directory Monitor


This is a somewhat complicated script that will keep a directory up to date with changes that are being tracked by Directory Monitor. If you don't want full synchronization like you get in option 1, this is a viable alternative.

copysync.bat
@echo off

rem Parameters: %event% %fullfile% %dir% %file% %oldfullfile%
rem %1 = %event%
rem %2 = %fullfile%
rem %3 = %dir%
rem %4 = %file%
rem %5 = %oldfullfile%

rem Set this to the directory you want to sync all changes into !! No trailing slashes !!.
set syncpath=C:\Sync\DirMon

echo Detected event '%1'

if %1==New goto :copy
if %1==Modified goto :copy
if %1==Deleted goto :delete
if %1==Renamed goto :rename

:copy
echo Copying files...
md "%syncpath%\%~3\"
copy /Y "%~2" "%syncpath%\%~3\%~4"
goto :exit

:delete
echo Deleting files...
del "%syncpath%\%~3\%~4"
goto :exit

:rename
echo Renaming files...
md "%syncpath%\%~3\"
copy /Y "%~2" "%syncpath%\%~3\%~4"
del "%syncpath%\%~3\%~nx5"
goto :exit

:exit

All you will need to change in this script is the location where you want to the changes to be synchronized. Set the syncpath variable appropriately and you're ready to go (with no trailing slashes otherwise it breaks the script).

Next up, we need to configure Directory Monitor to call this batch file and set the options in an optimal way to synchronize often. Edit or add a new directory to monitor, and go to the Execute tab.


  1. Add the path to the batch file you created earlier to Execute.
  2. Set Parameters to %event% %fullfile% %dir% %file% %oldfullfile%
  3. The other options are completely up to you to decide. The script can be run sequentially without any conflicts and is usually quite fast. If you are copying large files I would suggest disabling the timeout altogether to make sure the files are completely copied.
You are welcome to make tweaks to the script as you see fit for your requirements. For some explanation on parameter manipulation being used you can read up on Batch File Parameters.

Feedback and Knowledge Base