How to Generate Backup Files with Timestamp using Batch Script
In this tutorial, you will learn how to create a batch file which generates a backup of a file with timestamp when opened.
TLDR solution
@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 FileName=file_name
set BackupFileName=%FileName%__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%)
copy "path-to-file\%FileName%.file_extension" "target-path\%BackupFileName%.file_extension"
Step-by-Step Example
Step 1: Locate File to Backup
Let's say, you have a text file named test.txt on your desktop.
That is, the path should look like this:
C:\Users\DataToFish\Desktop
Step 2: Locate the Backup Destination
Let's create a folder on your desktop named Backups.
C:\Users\DataToFish\Desktop\Backups
Step 3: Create the Batch File
Open your Notepad and copy-paste the following template:
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 FileName=test
set BackupFileName=%FileName%__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%)
copy "C:\Users\DataToFish\Desktop\%FileName%.txt" "C:\Users\DataToFish\Desktop\Backups\%BackupFileName%.txt"
pause
Save the file as backup.bat in your Backups folder.
Verify that it works as expected by double clicking it.
You should find a test__timestamp.txt file in your backups folder.
If so, edit the batch file: add @echo off to suppress the command prompt and remove pause to let the batch file autoclose at the end.
@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 FileName=test
set BackupFileName=%FileName%__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%)
copy "C:\Users\DataToFish\Desktop\%FileName%.txt" "C:\Users\DataToFish\Desktop\Backups\%BackupFileName%.txt"
Optional: Schedule the Run of a Batch Script
Open the Task Scheduler by typing task scheduler in the Windows search bar and hitting return:

Click on Create Task... in the Actions Sidebar of the Task Scheduler:

This opens a Create Task window. Give your task a descriptive name in the Name field under the General tab:

Navigate to the Triggers tab and define a new daily trigger and hit OK:

Navigate to the Actions tab and define a new action. Click on Browse and locate your batch.file and hit OK:

Hit Ok in the Create Task window to save your task.
Verify that your task works by right clicking the relevant entry and clicking Run:

That's it! You just learned how to back up a file using batch script.