How to create a batch script to backup your files
Published on Aug. 22, 2023, 12:12 p.m.
In this guide, you’ll see the full steps to create a 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"
You may customize the following parts in the code:
BackupName=File Name_%Yr%-%Mon%-%Day%(%Hr%-%Min%-%Sec%)This is 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“Path where your file is storedFile Name.File Type .You’ll have to ensure that the file name specified is identical to the original file name“Path where your backup file will be stored\%BackupName%.File Type”This represents the target path where your backup file will be stored
Let’s now see the steps to create a batch script to backup a CSV file called ‘Products‘ .
(1) First, open notepad.(2) Then, type/copy the code below .
@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=Backup_Products__%Yr%-%Mon%-%Day%_(%Hr%-%Min%-%Sec%)
copy "C:\Users\Ron\Desktop\Test\Products.csv" "C:\Users\Ron\Desktop\Test\%BackupName%.csv"
(3) Save the Notepad with “.bat .
(4) Once you double-click on the batch file, the backup will be created at your target path.