用于重命名每日备份文件的批处理脚本

用于重命名每日备份文件的批处理脚本

我每天都会用一个程序来创建备份文件。无法设置该程序保留多个备份,它只会覆盖或删除之前的备份,但我不喜欢这种行为。

因此,我正在考虑创建一个可以通过计划任务启动的批处理文件,但我的知识仅限于几个命令。

例如,假设备份位置文件夹是“C:/backup”,并且备份文件始终包含单词“skiman”

然后我希望脚本将任何包含单词“skiman”的文件重命名为 AutoBackup01.zip,并最多有 10 个备份,同时将 AutoBackup01.zip 保留为最新备份,将 AutoBackup10.zip 保留为最旧备份,并删除任何早于 AutoBackup10.zip 的旧备份。换句话说,每次有新的备份文件时,它都会为每个文件添加 +1。

任何提示或帮助都将不胜感激...我猜 powershell 脚本也可以解决问题,但我对 powershell 一无所知。

谢谢(PS:如果有一些英文错误我很抱歉,英语不是我的母语)。

答案1

与 Tetsujin 在此处所述的相同,但即使不是,您也可以使用 PowerShell FileSystemWatcher 来监视这些备份所在的目录,并使用当前日期和时间戳作为文件名的一部分自动重命名它们。

使用 FileSystemWatcher 是非常常见的事情,有大量关于该主题的完整记录文章和示例代码。

例如:

Powershell 文件系统观察器

该脚本使用 .net FileSystemWatcher 类订阅 NTFS 文件系统中文件或文件夹的“创建”、“更改”和“删除”事件。它可用于监视一个或多个文件夹,并可修改为在触发这些事件时执行任何操作。

https://gallery.technet.microsoft.com/scriptcenter/Powershell-FileSystemWatche-dfd7084b/file/42206/1/FileSystemWatcher.ps1

您可以对其进行调整以执行任何您想要的操作。

答案2

你可以用一个简单的批处理脚本来完成这个任务。基本上有两个步骤:

  • 重命名文件
  • 删除超过 X 天的文件

重命名文件很容易,因此第一行看起来像:

for %%f in (*skiman*) do rename "%%f" "AutoBackup%date:~10,4%%date:~6,2%/%date:~4,2%.bak"

然后删除部分:

ForFiles /p "C:\Backup" /s /d -30 /c "cmd /c del @file"

这将删除超过 30 天的文件。

因此整个文件看起来将是这样的:

@echo off
for %%f in (*skiman*) do rename "%%f" "AutoBackup%date:~10,4%%date:~6,2%/%date:~4,2%.bak"
ForFiles /p "C:\Backup" /s /d -30 /c "cmd /c del @file"

就是这样。只需为其命名Rename.bat或类似名称(注意.bat文件扩展名),将其放在C:\Backup文件夹中并使用计划任务运行它。

免责声明

信息按原样提供,未经检查是否正确使用。请谨慎使用。

答案3

所以,很难向你解释这一点,我的英语有限......


尝试使用一个 bat 文件,其中将检查:

1)文件夹 c:\backup-> exist

2)文件 *skiman*.zip-> exist

3)文件 AutoBackupXY.zip-> exist

注意:- 考虑接受在另一个数值范围内工作:

  • 00 09为了保持AutoBackup00.zip作为最近的
    • AutoBackup09.zip作为最旧的备份
@echo off && verify ^.not 2>nul || ;color 0a && ;setlocal enabledelayedexpansion
set "_= /y /v /b " && mode 105,15 && set "_msg=*Press any key to continue^!!..."

if exist c:\Backup\nul (;cd /d "c:\Backup\." && ;title <nul && ;title ...\%~nx0)
for /f tokens^=* %%k in ('%__APPDIR__%where .:*skiman*.zip')do set "_backs=%%~k"
if not "!_backs!"=="" (set "_target=!cd!\AutoBackup#.zip") else (echo/Path: !cd!
echo=Drive/Folder/file skiman.zip not found "c:\Backup" [Error^^!!] & goto :^[ )

echo/:Copy file backup:&echo/"!_backs!"^[ok^^!!]^|^Pending renaming ^[00^-^0^9^]
for /l %%i in (9 -1 1) do for /f %%I in ('set /a %%i-1')do >nul cd. & rem./ && (
if exist "!_target:#=0%%~I!" >nul copy !_!"!_target:#=0%%~I!" "!_target:#=0%%i!"
if exist "!_target:#=0%%I!" echo/"!_target:#=0%%I!" "!_target:#=0%%i!" [ok^^!!])

if exist "!_backs!" (copy !_! "!_backs!" "!_target:#=00!">nul) else (>nul cd. &(
echo/Backup file source: "!_backs!" not found: [Error^^!!] & echo/ & goto :^[ ))
:^[ 
echo/ & echo/%_msg% && timeout -1 >nul & <nul endlocal && exit /b 1 || goto :EOF

  • 输出 1st run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]

  • 输出 2nd run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 3rd run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 4th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 5th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 6th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 7th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup05.zip" "C:\Backup\AutoBackup06.zip" [ok!]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 8th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup06.zip" "C:\Backup\AutoBackup07.zip" [ok!]
"C:\Backup\AutoBackup05.zip" "C:\Backup\AutoBackup06.zip" [ok!]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 9th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup07.zip" "C:\Backup\AutoBackup08.zip" [ok!]
"C:\Backup\AutoBackup06.zip" "C:\Backup\AutoBackup07.zip" [ok!]
"C:\Backup\AutoBackup05.zip" "C:\Backup\AutoBackup06.zip" [ok!]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 10th run
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup08.zip" "C:\Backup\AutoBackup09.zip" [ok!]
"C:\Backup\AutoBackup07.zip" "C:\Backup\AutoBackup08.zip" [ok!]
"C:\Backup\AutoBackup06.zip" "C:\Backup\AutoBackup07.zip" [ok!]
"C:\Backup\AutoBackup05.zip" "C:\Backup\AutoBackup06.zip" [ok!]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

  • 输出 Nth run== same输出 10th run:
:Copy file backup:
"C:\Backup\2020-01-09_skiman.zip"[ok!]|Pending renaming [00-09]
"C:\Backup\AutoBackup08.zip" "C:\Backup\AutoBackup09.zip" [ok!]
"C:\Backup\AutoBackup07.zip" "C:\Backup\AutoBackup08.zip" [ok!]
"C:\Backup\AutoBackup06.zip" "C:\Backup\AutoBackup07.zip" [ok!]
"C:\Backup\AutoBackup05.zip" "C:\Backup\AutoBackup06.zip" [ok!]
"C:\Backup\AutoBackup04.zip" "C:\Backup\AutoBackup05.zip" [ok!]
"C:\Backup\AutoBackup03.zip" "C:\Backup\AutoBackup04.zip" [ok!]
"C:\Backup\AutoBackup02.zip" "C:\Backup\AutoBackup03.zip" [ok!]
"C:\Backup\AutoBackup01.zip" "C:\Backup\AutoBackup02.zip" [ok!]
"C:\Backup\AutoBackup00.zip" "C:\Backup\AutoBackup01.zip" [ok!]

相关内容