如何仅保留五个最新的备份文件夹并删除较旧的文件夹

如何仅保留五个最新的备份文件夹并删除较旧的文件夹

本文介绍了如何仅保留五个最新的备份文件夹并删除较旧的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每天使用批处理文件定期进行备份.考虑以下文件夹:

I want to use a batch file to make a backup regularly, every day. Consider these folders:

C:\game folder
D:\backup1
D:\backup2
D:\backup3
D:\backup4
D:\backup5

里面有游戏文件夹:

c:\game folder\remote\                       »Folder
c:\game folder\remote\remotecache.vdf        ».vdf file
c:\game folder\remote\FullProfile            »No extension file
c:\game folder\remote\ProfileSaves\          »Folder
c:\game folder\remote\ProfileSaves\1054678   »No extension file

我需要一个批处理文件

  • 验证backup1上的时间戳(文件夹或其中的任何文件)超过1天,如果是,则继续,否则停止;
  • 删除backup5文件夹或其内容;将backup4的文件夹/文件复制到backup5文件夹;
  • 删除backup4文件夹或其内容;将backup3的文件夹/文件复制到backup4文件夹;
  • 等最多backup1文件夹.
  • 最后将游戏文件夹复制到backup1文件夹.
  • verifies if time stamp on backup1 (either folder or any file inside it)is older than 1 day and if YES then continue, else stop;
  • deletes backup5 folder or its contents; copies folders/files of backup4 to backup5 folder;
  • deletes backup4 folder or its contents; copies folders/files of backup3 to backup4 folder;
  • etc. up to backup1 folder.
  • Finally copies game folder to backup1 folder.

有可能吗?

我的想法是运行计划任务(Windows),. bat文件通过使用backup1或其中的任何文件的时间戳,与实际日期进行比较并检查是否为已超过一天.

My idea is to run a scheduled task (Windows), and the .bat file verifies if backup is needed or not by using time stamp of backup1 or any file inside it, comparing with actual date and checking if backup1 is older than one day.

考虑下面的代码,为了使其能够像我之前描述的那样工作,我应该放置什么?

Considering the code below, what should I place in order to make it works as I've described before?

@echo off

echo
echo ------------------------------------------------------------------
echo Daily script that backs-up game folder
echo ------------------------------------------------------------------

echo ------------------------------------------------------------------
echo Calculation of date
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
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 datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

如何在此处制作某种 IF 来检查backup1文件夹的时间戳,如果它早于1天,请运行以下代码?以及如何设置@path?

How to make some kind of IF here to check time stamp of backup1 folder and if it's older than 1 day run the code below? And how to set the @path?

echo ------------------------------------------------------------------
echo Delete old backup folder
FORFILES -p "" /D -1 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"

echo ------------------------------------------------------------------
echo Make new Backup folder
md D:\Backup1

echo ------------------------------------------------------------------
echo Copy files into backup folder
xcopy /s /y "C:\game folder" "D:\Backup1"

推荐答案

此日常备份操作任务的简单批处理文件如何处理?

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%\System32\wbem\wmic.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%\System32\xcopy.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

它使用命令 WMIC yyyy-MM-dd格式获取当前日期,如为什么%date%在作为计划任务执行的批处理文件中产生不同的结果?国际日期格式yyyy-MM-dd具有良好的可读性.此外,它还有一个很大的优点,即在文件夹名称中使用这种格式的日期时,按名称按字母顺序排序的文件夹列表也会自动按创建该文件夹的日期进行排序.一定要知道何时创建备份文件夹,因此最好使用2017-12-222017-12-232017-12-24等,而不是仅使用123等.

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.

否则,将在已配置的父目录中使用已配置的前缀对要备份的文件夹进行备份.必须在批处理文件中指定BackupParentFolder,但根本不能定义BackupNamePrefix.

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.

进行备份后,将执行命令 DIR ,以列出所有备份文件夹,其中备份日期的文件夹名称按名称反向排列,这意味着最新的备份文件夹由 DIR 输出. >最旧的,最后一个.

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.

将忽略五个最新备份,并删除所有其他备份文件夹,这通常意味着从现有备份文件夹列表中删除一个目录.在 Bat文件中仅在存在较新文件的情况下删除文件的答案中详细解释了这种非常简单但非常有效的备份策略..

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 /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?
  • xcopy /?

还要阅读有关使用命令重定向运算符以获取2>nul的解释.当Windows命令解释器在执行命令 FOR 之前处理此命令行时,重定向操作符>必须在 FOR 命令行上转义字符^进行转义,以将其解释为文字字符. >,它将在后台启动的单独命令过程中执行嵌入的dir命令行.

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.

这篇关于如何仅保留五个最新的备份文件夹并删除较旧的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:20