Need to generate a backup file with timestamp in an automated fashion?
If so, in this tutorial I’ll show you the steps to create a backup file with timestamp using a batch script.
To start, I’ll provide you with the batch script that you can tailor to your needs. I will then demonstrate the concept using a simple example.
Batch Script to a Generate Backup File with Timestamp
Here is the batch script that you can use to generate your backup file:
@echo off for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a set Yr=%DateTime:~0,4% set Mon=%DateTime:~4,2% set Day=%DateTime:~6,2% set Hr=%DateTime:~8,2% set Min=%DateTime:~10,2% set Sec=%DateTime:~12,2% set BackupName= File Name__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%) copy "Path where your file is stored\File Name.File Type" "Path where your backup file will be stored\%BackupName%.File Type"
I’ll now explain the parts in the code that you can customize based on your needs:
BackupName= File Name__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%)
“Path where your file is stored\File Name.File Type”
“Path where your backup file will be stored\%BackupName%.File Type”
- The blue portion represents the full file name with the timestamp format (this file name will be created when you execute the batch script). You may change the file name and/or timestamp format
- The portion in green represents the source path where your original file is stored. You’ll have to ensure that the file name specified is identical to the original file name
- The portion in orange represents the target path where your backup file will be stored
Don’t forget to put the file type at the end of the paths (for example, for most Excel files, the file type is “.xlsx”).
Example with the Steps to Create the Batch Script
Let’s now see the steps to create a batch script to backup an MS Access file called ‘Test_Application’ where the file type is ‘accdb’
(1) First open Notepad
(2) Type/copy the below code into Notepad
You’ll need to modify:
- The file name in the BackupName to your desired file name (currently the file name is ‘Test_Application’). Optionally, you may wish to change the timestamp format
- The source path to the location where your original file is stored
- The target path to the location where you want to store the backup file
As mentioned before, don’t forget to add the file type at the end of the paths (here, the MS Access file type is ‘accdb’).
@echo off for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a set Yr=%DateTime:~0,4% set Mon=%DateTime:~4,2% set Day=%DateTime:~6,2% set Hr=%DateTime:~8,2% set Min=%DateTime:~10,2% set Sec=%DateTime:~12,2% set BackupName= Test_Application__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%) copy "C:\Users\Doron E\Desktop\Application\Test_Application.accdb" "C:\Users\Doron E\Desktop\Backup\%BackupName%.accdb"
And this is how the Notepad would look like after copying the above code:
(3) Save the Notepad with ‘.bat’ extension. Here, I chose to save the file as ‘Backup.bat‘
(4) Once you double-click on the batch file, the backup with the timestamp will be created at the target path.
In the last part of this tutorial, I’ll show you how to create a button to execute the batch script within MS Access.
Button to Run the Batch Script from Access
(1) First, open MS Access and then create a new MS Access Form to place your button.
To create a new Form, go to the Create tab, and then press on Form Design:
(2) Under the Design tab, press on the “button shape” and then place the button on the Form itself:
(3) Press Cancel, as we will not be using the Command Button Wizard:
(4) On the Property Sheet, select the “[Event Procedure]” associated with the On Click option under the Event tab. Then, press on the 3 dots (“…”)
(5) Use the following VBA structure to run the Backup batch file when clicking on the button:
Shell ("The path where your Backup batch file is stored\File Name.bat")
In my case, I stored the Backup batch file on my desktop, and so my VBA code would look as follows:
(6) Save your work and then switch to the Form View. Under that view, you’ll be able to press the button to run the Backup batch file from Access:
Batch files are fun to work with. Here are some additional tutorials that you may find interesting: