While doing development on a database such as a Microsoft Access .mdb or .accdb file, it’s a best practice to create a backup of the file every few hours in case you mess something up and need to revert to a prior copy.

I used to just do the CTRL+C / CTRL+V thing to make a copy of the file, but if you accidentally opened one of those copies you’d lose the info about when the file was created.  That, and the files take up a lot of disk space.

Requirements

What I wanted was to be able to right-click a file, choose some action and have it automatically:

  • Backup the file
  • Name the backup with something that includes the filename and then the date and time in YYYY-MM-DD format so I could sort by the filename to get the latest or oldest and not have to worry about the modified dates changing.
  • Move the file to a .rar file to save space. (Access databases in particular are often highly compressable).

Solution

To solve my problem, I created a little dos batch file I call “BackupFile.bat” and I put it in my send to menu so that go-forward, I could just right-click a file, choose Send To and then Backup File:

image

Create a batch file called BackupFile.bat with these contents:

@ECHO OFF

for /F "eol=; tokens=1-7 delims=/:. " %%a in ("%DATE%:%TIME%") do SET myOutput=%~n1_%%c-%%a-%%b_%%dh_%%em_%%f.%%gs%~x1

if exist "%1" (
if not exist %~dps1Backup\nul md "%~dp1Backup"
Echo Copying to Backup: "%~dp1Backup\%myOutput%"
copy %1 "%~dp1Backup\%myOutput%"
)
REM Edit this next line to have your path to WinRAR:
"C:\Program Files\WinRAR\WinRAR.exe" M "%~dp1Backup\%myOutput%.rar" "%~dp1Backup\%myOutput%"

Make sure you update the path to WinRAR above to a valid WinRAR path, or just modify that line to work with your preferred compression app.

Once you’ve created the .bat file you can quickly add it to your send to menu by clicking Start –> then type: shell:sendto and hit enter.  Doing that should open your send to folder in Windows explorer. 

Create a shortcut to the BackupFile.bat file in that location and you’re set.

Usage

To use, you just right-click the file you want to backup:

image

You’ll see the batch file appear in a command window, and then a WinRAR creating archive box appear:

image

When done, you’ll have a Backup folder under the folder you were in previously, and in that folder you’ll see the .RAR files you just created:

image

Advertisement