Generate a Backup File with Timestamp using a Batch Script

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) Then, type/copy the code below into Notepad

You’ll need to modify:

  • 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\Ron\Desktop\Application\Test_Application.accdb" "C:\Users\Ron\Desktop\Backup\%BackupName%.accdb"

And this is how the Notepad would look like after copying the above code:

Generate a Backup File with Timestamp using a Batch Script

(3) Save the Notepad with ‘.bat’ extension. Here, I chose to save the file as ‘Backup.bat

Generate a Backup File with Timestamp using a Batch Script

(4) Once you double-click on the batch file, the backup with the timestamp will be created at your target path.

Backup Batch File

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:

Form Design in MS Access

(2) Under the Design tab, press on the “button shape” and then place the button on the Form itself:

Button on an Access Form

(3) Press on Cancel, as the Command Button Wizard will not be needed:

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 (“…”)

Property Sheet in MS Access

(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 like this:

backup file with timestamp

(6) Save your work and then switch to the Form View. Under this view, you’ll be able to press the button to run the Backup batch file from Access:

Form View

Batch files are fun to work with. Here are some additional tutorials that you may find interesting: