每月一次将文件夹内容压缩到存档然后删除的脚本 [英] Script to compress contents of a folder once per month into an archive and then delete

查看:21
本文介绍了每月一次将文件夹内容压缩到存档然后删除的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个脚本来将目录(包括子文件夹)的内容备份到单个存档文件.我想按月计划运行此脚本(将使用任务计划程序),因此脚本每 30 天压缩文件夹的内容并创建一个新的存档文件,以月份为文件名,然后删除文件/子文件夹.

I need a script to backup the contents of a directory including sub folders to a single archive file. I would like to run this script on a monthly schedule (will use task scheduler), so every 30 days the script compress the contents of the folder and create a NEW archive file with the month as the name of the file and then delete the files/subfolders.

我把我在网上找到的编码混合在一起,这就是产品.代码按预期运行,只是不确定逻辑是否存在.

Ive put together a mixture of coding I found online, this is the product. The code functions as desired, just not sure if the logic is there.

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x

IF %month%==1 (
SET mth=Jan
)    
IF %month%==2 (
SET mth=Feb
)    
IF %month%==3 (
SET mth=Mar
)
IF %month%==4 (
SET mth=Apr
)    
IF %month%==5 (
SET mth=May
)   
IF %month%==6 (
SET mth=Jun
)   
IF %month%==7 (
SET mth=Jul
)    
IF %month%==8 (
SET mth=Aug
)    
IF %month%==9 (
SET mth=Sep
)    
IF %month%==10 (
SET mth=Oct
)    
IF %month%==11 (
SET mth=Nov
)    
IF %month%==12 (
SET mth=Dec
)

for /d %%X in (*) do "c:Program Files7-Zip7z.exe" a "%mth%-%Year%.zip" "%%X"    

IF EXIST %mth%-%Year%.zip (
    FOR /D %%p IN ("*") DO rmdir "%%p" /s /q
) ELSE (
    echo NO
)

如果有人可以帮助我优化此代码或改进它,我将不胜感激.

If anyone could help me optimize this code or improve it I would be greatly appreciated.

推荐答案

我建议使用以下两种解决方案之一并运行 WinRAR 或使用 7-Zip<的批处理文件/strong> 在一个月的最后一天午夜前几分钟使用任务计划程序,或者每 30 天一次,如果没有必要将一个月的所有文件都放在一个存档中.

I suggest to use one of the two solutions below and run either WinRAR or the batch file using 7-Zip with task scheduler on last day of a month some minutes before midnight, or every 30 days if it is not really necessary to have all files of a month in one archive.

阅读以下有关解决方案之前的一个注意事项:

One note before reading below about the solutions:

不要在包含要存档的文件和子文件夹的文件夹中创建 ZIP 存档.将解决方案与 7-Zip 结合使用并不是一个好主意.

Do not create the ZIP archive in the folder with the files and subfolders to archive. That would not be a good idea on using the solution with 7-Zip.

我针对此任务的第一个解决方案是使用 WinRAR,这使此任务成为单行式:

My first solution for this task is using WinRAR which makes this task a one-liner:

"C:Program FilesWinRARWinRAR.exe" m -afzip -agMMM-YYYY -ibck -cfg- -ep1 -m5 -r "C:Path to Destination Folder of Archive.zip" "C:TempFolder with Files and Subfolders to compress"

在执行 WinRAR 时,ZIP 存档的目标文件夹必须已经存在.

The destination folder of the ZIP archive must already exist on execution of WinRAR.

WinRAR的程序文件文件夹中有一个文本文件Rar.txt,其中包含所有命令和开关以及控制台版本的说明Rar.exe.WinRAR 的帮助在标签Contents 上有项命令行模式 和子项命令开关.两者都可以用来创建像上面一样的命令行.

In program files folder of WinRAR there is the text file Rar.txt which contains all commands and switches with an explanation for console version Rar.exe. Help of WinRAR has on tab Contents the item Command line mode with subitems Commands and Switches. Both can be used to create a command line like above.

命令m是通过移动文件和目录来解释的,这会导致在成功完成打包操作后删除文件和目录.

The command m is explained with moves files and directories which results in the files and directories being erased upon successful completion of the packing operation.

开关 -afzipWinRAR 唯一的开关,它选择 ZIP 作为存档格式.Rar.exe 不支持此开关,因为控制台版本仅支持创建 RAR 存档.

The switch -afzip is a WinRAR only switch which selects ZIP as archive format. Rar.exe does not support this switch as the console version supports only creating RAR archives.

带有选项 MMM-YYYY 的开关 -ag 导致在文件扩展名之前以短名称格式和年份附加到存档文件的名称,例如2014 年 11 月.

The switch -ag with option MMM-YYYY results in appending to name of archive file before file extension the month in short name format and the year, e.g. Nov-2014.

开关 -ibck 导致在存档处理时将 ​​WinRAR 窗口最小化到 Windows 系统托盘.

The switch -ibck results in minimizing WinRAR window to Windows system tray on archive processing.

开关 -cfg- 导致忽略配置文件和 RAR 环境变量(只是为了安全).

The switch -cfg- results in ignoring configuration file and RAR environment variable (just for safety).

开关 -ep1 导致从存档中排除到基本目录的整个路径,这导致这里没有将C:TempFolder with Files and Subfolders to compress"放入存档中,只是此文件夹中的文件和子文件夹.

The switch -ep1 results in excluding from archive the entire path to base directory which results here not putting "C:TempFolder with Files and Subfolders to compress" into the archive, just the file and subfolders in this folder.

开关 -m5 为存档设置最佳压缩.

The switch -m5 sets best compression for the archive.

开关 -r 非常重要,因为它告诉 WinRAR 递归压缩所有文件和文件夹.

The switch -r is a very important one as it tells WinRAR to recursive compress all files and folders.

创建的存档文件例如是 Nov-2014.zip,因为存档文件的名称只是 .zip.

The archive file created is for example Nov-2014.zip because the name of the archive file is just .zip.

WinRAR 许可证投资少量资金可以使许多存档和备份任务变得非常简单.

Investing a small amount of money for WinRAR license can make many archiving and backup tasks very simple.

当然,也可以创建 RAR 存档而不是 ZIP 存档.

Of course it would be also possible to create a RAR archive instead of a ZIP archive.

7-Zip 解决方案需要更多的努力,因为目前没有用于此类备份任务的 WinRAR 等功能.

The 7-Zip solution needs more effort as currently not having features like WinRAR for such backup tasks.

@echo off
setlocal EnableDelayedExpansion
for /f %%x in ('wmic path win32_localtime get /format:list') do (
    set "Value=%%x"
    if "!Value:~0,5!"=="Month" (
        set "Month=!Value:~6!"
    ) else if "!Value:~0,4!"=="Year" (
        set "Year=!Value:~5!"
    )
)
if "%Month%"=="1"  set "Month=Jan" & goto Compress
if "%Month%"=="2"  set "Month=Feb" & goto Compress
if "%Month%"=="3"  set "Month=Mar" & goto Compress
if "%Month%"=="4"  set "Month=Apr" & goto Compress
if "%Month%"=="5"  set "Month=May" & goto Compress
if "%Month%"=="6"  set "Month=Jun" & goto Compress
if "%Month%"=="7"  set "Month=Jul" & goto Compress
if "%Month%"=="8"  set "Month=Aug" & goto Compress
if "%Month%"=="9"  set "Month=Sep" & goto Compress
if "%Month%"=="10" set "Month=Oct" & goto Compress
if "%Month%"=="11" set "Month=Nov" & goto Compress
set "Month=Dec"

:Compress
set "FolderToCompress=C:TempFolder with Files and Subfolders to compress"
"C:Program Files7-Zip7z.exe" a -mx=9 -r -tzip "C:Path to Destination Folder of Archive\%Month%-%Year%.zip" "%FolderToCompress%*"
rd /S /Q "%FolderToCompress%"
md "%FolderToCompress%"
endlocal

首先,命令FORIFSET 用于处理Windows Management Instrumentation 命令 获取月份和年份.

First, the commands FOR, IF and SET are used to process output of Windows Management Instrumentation Command to get month and year.

有关在命令提示符窗口(控制台窗口)中运行的这 3 个命令的帮助

For help on those 3 commands run in a command prompt window (console window)

  • help forfor/?
  • help ifif/?
  • help setset/?
  • help for or for /?
  • help if or if /?
  • help set or set /?

或者 的 Microsoft TechNet 文章,ifset.

Or the Microsoft TechNet articles for for, if and set are read for details.

其他命令GOTOMDRDSETLOCAL的帮助可以通过同样的方式获得.

And help on the other commands GOTO, MD, RD and SETLOCAL can be get the same way.

在确定存档文件名的月份和年份后,执行 7-Zip 以在目标文件夹中创建一个 ZIP 文件,目标文件夹当然必须已经存在.

After determining month and year for archive file name, 7-Zip is executed to create a ZIP file in a destination folder which of course must already exist.

还有 7-Zip 有一个帮助,在 Contents 标签上有 Command Line Version 和子项 Commands开关.

And also 7-Zip has a help which on Contents tab has the item Command Line Version with the subitems Commands and Switches.

命令 a 用于将文件压缩为存档.WinRAR 所提供的没有 m.所以压缩后的删除必须单独进行.

The command a is for compressing files into an archive. There is no m as WinRAR offers. So deletion after compression must be done separately.

带有选项 x=9 的开关 -m 设置最佳压缩.

The switch -m with the option x=9 sets best compression.

开关 -r 非常重要,因为它告诉 7-Zip 递归压缩所有文件和文件夹.

The switch -r is a very important one as it tells 7-Zip to recursive compress all files and folders.

开关 -tzip 选择标准的 ZIP 格式.

And the switch -tzip selects the standard ZIP format.

使用命令 RD 删除包含所有文件和子文件夹的文件夹C:TempFolder with Files and Subfolders to compress",然后使用 MD文件夹本身被重新创建,因为我认为文件夹本身应该保留到下个月.

With the command RD the folder "C:TempFolder with Files and Subfolders to compress" with all files and subfolders is deleted and next with MD the folder itself is recreated as I suppose the folder itself should remain for next month.

这篇关于每月一次将文件夹内容压缩到存档然后删除的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆