Getting Outlook out of SyncToy’s Way

I use Microsoft’s SyncToy for nightly backups. In the past I’ve used many different solutions, from custom xcopy scripts to robocopy to the built-in backup program that started shipping with Windows back in the NT days. I also keep complete disk images for those rare occasions when a full recovery is needed. But for making sure that my daily work is saved I haven’t found a better alternative than SyncToy and a second hard disk.

SyncToy uses the Microsoft Sync Framework 2.0 to manage pairs of folders and keep them in sync according to certain rules. To keep some set of files safe all you do is organize them under one or more folders, then use SyncToy to define named folder pairs that match the original content folders with sync folders on the second drive. There are several modes, but I use the “echo” action, which propagates changes from the source folder to the sync folder. I have a scheduled task that runs every night to tell SyncToy to process all the defined folder pairs.

This setup worked great for months, or at least I thought it did. It’s one of those deals where I set something up, make sure it works, and then forget about it. In this case it turns out that the SyncToy command didn’t quite work as well as I thought. The problem was my Outlook.pst mail file. I have Outlook open all the time, minimized to the system tray, and when Outlook is open SyncToy can’t read the .pst file. When I happened to check the log I saw the read errors and realized that my voluminous mail file had never been backed up.

So, what I needed was a command that would close Outlook, run SyncToy, then restart Outlook. After Googling around and experimenting this is what I came up with:

@echo off
echo Synchronizing folders...
echo Shutting down Outlook.exe...
"c:\Mark Personal\System Tools\NirCmd\nircmd.exe" closeprocess outlook.exe
timeout 2
tasklist /FI "IMAGENAME eq outlook.exe" 2>NUL | find /I /N "outlook.exe">NUL
if "%ERRORLEVEL%"=="0" goto :wait
echo Outlook closed; executing SyncToyCmd...
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R
echo Sync completed; starting Outlook.exe...
start "" /B /MIN C:\Users\Mark\Desktop\Outlook
echo Outlook.exe started; exiting
@echo on

This script first sends Outlook.exe a shutdown command using a nifty command line tool called NirCmd.exe, from NirSoft. You could also use taskkill, but NirCmd is a gentler method that gives Outlook a chance to properly close its data files, and if you know anything about Outlook, you know you want it to properly close its data files.

After telling Outlook to close the script loops until it can no longer find outlook.exe in the running task list, and then proceeds to run SyncToy for all active folder pairs, and then restart Outlook after it completes. Pretty neat, and I can’t take credit for any of it. Just some stuff cobbled together from bits and pieces of others’ wisdom around the net.

One thought on “Getting Outlook out of SyncToy’s Way

  1. Thank you for this post.
    If a draft is open in outlook and not yet saved Outlook will not shut which will cause this script to fail to run.
    Add Takskkill /f /im OULOOK.exe /t to the beginning of your script. This will force outlook to shut.

Leave a Reply

Your email address will not be published. Required fields are marked *