Our Blog

Calling for Backup (Part 2)

by | Jan 2, 2020 | , | Tools of the Trade | 0 comments

The last blog post presented a short Windows batch file that creates a backup copy of Word’s normal template. While being able to update the backup file with a single double-click on an icon saves a bit of work, having the batch file run automatically can be much handier, and this is pretty easy to do using the Windows Task Scheduler. Automation does create a trade-off, though. This batch file has little intelligence, and automating it using the instructions below trades some backup safety for ease of use: only one backup file is maintained and it gets overwritten on a regular schedule. So while automation provides some added protection, it is still advisable to keep copies of backup files in a separate, protected location just in case.

Adjusting the Batch File

Before creating any automation you may want to modify the batch file. The version presented in the previous post alerts the user of a successful backup, but if you are running this program automatically you may only want to be alerted if there is a problem. The code below, which has been adjusted to remove the successful backup message, can be used to create a new batch file. You may want copies of both versions, with different names, so that you still get a message if you do a manual backup. You may also need to add an exclusion for each batch file to get around your antivirus software.

Setting Up the Task Scheduler

The icon for the Task Scheduler can be found in the Windows Start menu under Windows Administrative Tools; I recommend pinning it to your Start menu for easy access. The following steps will set up a task that runs the batch file every day (but there are options for other schedules).

  1. Open the Task Scheduler.
  2. Right-click on the Task Scheduler Library folder in the left menu bar and select the New Folder option, naming the resulting folder however you like. Having your own folder will make it easy to find your task among the many that other programs create.
  3. Open the Task Scheduler Library folder and then the new folder you created.
  4. In the Actions toolbar on the right side of the window, select the Create Basic Tasks … icon.
  5. Add a name for the task and select the Next button.
  6. Set the frequency with which the batch file will run (daily) and select the Next button.
  7. Enter a backup time (3:00 a.m.) and select the Next button. I recommend a time that is earlier than you typically log into Windows; the reason is described below.
  8. Leave the action set to the default “start a program” value and select the Next button.
  9. Click the Browse… button and locate the batch file. Select it and then select the Next button.
  10. Check the “Open the Properties dialog for this task when I click finish” toggle and select the Finish button.
  11. In the properties dialogue box, click the Settings tab, check the box marked “Run task as soon as possible after a scheduled start is missed,” and select the OK button. Also check the box labeled “If the task fails, restart every” and change the setting to “2 hours.” Since the scheduled time has been set for 3:00 a.m., the batch file will typically run soon after you log in each day. If you start Word before it runs (or if you are still having antivirus program conflicts) you will receive a message that the copying wasn’t completed, but it will make a few more attempts. If you often open Word before the backup is complete, you may want to adjust when the Task Scheduler settings.

Verifying the Backup

With the set-up complete, the batch file will now be run automatically every day shortly after you first log in. You can open the Task Scheduler, navigate to the folder containing your task, and look at the displayed “Last Run Time” and “Last Run Result” (figure xx) to verify it is running as expected. (This display may note that an error occurred, but this will not include the errors that the batch file will alert you to with a message). You can also navigate to the template folder and compare the files to verify the backup was successful.

A Limitation of This Approach

Each user account on your computer will have its own Normal template, but the task created using the instructions above will only make one backup per day, for the first user who logs in. So if you have more than one user account on your computer, the backup for each of them will not be kept current. This may or may not be an issue, depending on how much customization each user does.


The Code

@Echo Off
  setlocal
  set sourcepath=C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Templates\
  set sourcefile=Normal.dotm
  set targetpath= C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Templates\ 
  set targetfile=Normal_backup.dotm
  if EXIST %sourcepath% (
      cd %sourcepath%
      if EXIST %sourcefile% (
          if EXIST %targetpath% (
              copy /Y %sourcepath%%sourcefile% %targetpath%%targetfile%
              if errorlevel 1 (
                      echo Copying %sourcefile% failed due to an error.
                      echo Try re-running the batch file later.
                      echo.
                      pause 
              )
          ) else (
              echo The target destination for backing up %sourcefile% was not found.
              echo Please verify the path is correct and then rerun the batch file.
              echo.
              pause
          )
      ) else (
          echo %sourcefile% was not found and has not been backed up.
          echo Please verify the files exists and you have permission to copy it,
          echo and then rerun the batch file.
          echo.
          pause
      )
  ) else ( 
      echo The folder containing %sourcefile% was not found,
      echo so that file cannot be backed up.
      echo Please verify the path is correct and then rerun the batch file.
      echo.
      pause
  )

0 Comments

Submit a Comment

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

Subscribe!

Sign up to get new blog posts sent directly to your email address and to receive occasional updates about our business and new website content.

Share This