What about this simple batch file for this daily backup operation task?
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FolderToBackup=C:game folder"
set "BackupParentFolder=D:"
set "BackupNamePrefix=Backup_"
if not exist "%FolderToBackup%" goto EndBackup
rem Get region independent current date in format yyyy-MM-dd.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%System32wbemwmic.exe OS GET LocalDateTime /VALUE') do set "FolderNameDate=%%I"
set "TodayBackupFolder=%BackupParentFolder%\%BackupNamePrefix%%FolderNameDate:~0,4%-%FolderNameDate:~4,2%-%FolderNameDate:~6,2%"
rem Was a backup folder created already today?
if exist "%TodayBackupFolder%" goto EndBackup
rem Create a backup of the folder to backup by doing a simple folder copy.
%SystemRoot%System32xcopy.exe "%FolderToBackup%" "%TodayBackupFolder%" /C /H /I /K /Q /R /S /Y >nul
rem Keep only the newest 5 backup folders and delete all others.
for /F "skip=5 delims=" %%I in ('dir "%BackupParentFolder%\%BackupNamePrefix%????-??-??" /AD /B /O-N 2^>nul') do rd /Q /S "%BackupParentFolder%\%%I"
:EndBackup
endlocal
It uses command WMIC to get the current date in format yyyy-MM-dd
as described in detail in answer on Why does %date% produce a different result in batch file executed as scheduled task? The international date format yyyy-MM-dd
is good readable. It has additionally the big advantage that on using the date in this format in a folder name, the list of folders sorted alphabetically by name are automatically also sorted by date on which the folder was created. It is surely good to know when a?backup folder was created and so it is better to use 2017-12-22
, 2017-12-23
, 2017-12-24
, etc. instead of just 1
, 2
, 3
, etc.
The batch file next checks if today a backup folder was created already which is simple as the backup date is included in folder name in international date format. The batch file does nothing else?if on same day a backup was created already before.
Otherwise a backup of the folder to backup is made in configured parent directory with configured prefix. BackupParentFolder
must be specified in the batch file, but BackupNamePrefix
could be not defined at all.
After making a backup the command DIR is executed for listing all backup folders with backup date in folder name sorted reverse by name which means the newest backup folders are output by DIR first and the oldest last.
The five newest backups are ignored and all other backup folders are deleted which means usually removing one directory from existing backup folders list. This very simple, but very effective backup strategy is explained in detail in answer on Bat file to delete files only when younger files are present.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rd /?
rem /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
xcopy /?
Read also the Microsoft article about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir
command line in a separate command process started in background.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…