Windows Batch script snippets

How to backup a file using BAT script

You just have to copy the following code and past into Notepad, then save with .cmd extension and execute it.

The script make a copy of the file C:\ImportantFolder\ImportantFile.xls and paste it into the path C:\ImportantFolder\ImportantFile_YYYYMMDD.xls.

It also produce a txt log file with information about the lasst execution C:\script\Backup\Log_backup.txt

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%%MM%%DD%

del "C:\script\Backup\Log_backup.txt"
echo Copying started at %date% %time%>>C:\script\Backup\Log_backup.txt
copy "C:\ImportantFolder\ImportantFile.xls" "C:\ImportantFolder\ImportantFile - %stamp%.xls"
echo Copying finished at %date% %time%>>C:\script\Backup\Log_backup.txt
echo File copied successfully at %date% %time%>>C:\script\Backup\Log_backup.txt
echo ------------------------------------ >>C:\script\Backup\Log_backup.txt

Back to Windows Batch script cookbook page