用bat脚本备份文件到网络主机,当备份数量达到三份后,删除旧的第一份备份
时间: 2023-05-12 22:06:13 浏览: 91
可以使用以下的bat脚本实现:
@echo off
setlocal
set source_folder=C:\path\to\source\folder
set destination_folder=\\network\path\to\destination\folder
set backup_prefix=backup_
set max_backup_count=3
set /a backup_count=0
for /f "delims=" %%a in ('dir /b /a-d "%destination_folder%\%backup_prefix%*" ^| find /c /v ""') do set /a backup_count=%%a
set /a backup_count+=1
set backup_name=%backup_prefix%%backup_count%
xcopy /s /e /y "%source_folder%" "%destination_folder%\%backup_name%"
if %backup_count% gtr %max_backup_count% (
set /a delete_count=%backup_count%-%max_backup_count%
for /l %%a in (1,1,%delete_count%) do (
set delete_name=%backup_prefix%%%a
del /q "%destination_folder%\%delete_name%"
)
)
endlocal
```
这个脚本会将源文件夹中的所有文件和子文件夹复制到目标文件夹中,备份文件名以"backup_"为前缀,后面跟着备份次数。如果备份数量超过了设定的最大备份数量,就会删除最旧的备份文件。
阅读全文