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

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

问题描述

我需要一个脚本来备份目录的内容包括子文件夹到一个存档文件。我想每月按时运行此脚本(将使用任务调度程序),每30天中的脚本,COM preSS该文件夹的内容,并创建月份新的归档文件作为该文件的名称,然后删除的文件/文件夹。

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.

伊夫放在一起混合编码,我发现网上,这就是产品。在code功能所需的,只是不知道如果逻辑是存在的。

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 Files\7-Zip\7z.exe" a "%mth%-%Year%.zip" "%%X\"    

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

如果有人可以帮助我优化这个code或改善它,我将不胜AP preciated。

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

推荐答案

我建议使用以下两种解决方案之一,并运行任何 WinRAR的或批处理使用文件的的7-Zip 上月的最后一天的任务调度午夜前几分钟,或每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 Files\WinRAR\WinRAR.exe" m -afzip -agMMM-YYYY -ibck -cfg- -ep1 -m5 -r "C:\Path to Destination Folder of Archive\.zip" "C:\Temp\Folder with Files and Subfolders to compress\"

档案文件的目标文件夹必须已经在执行中存在的 WinRAR的

在Program Files文件夹的 WinRAR的有文本文件中的 Rar.txt 其中包含的所有命令和解释可切换控制台版本的 RAR.EXE 的。的 WinRAR的有标签的内容帮助的项目的命令行模式与子项的命令开关。既可以用于创建像上面的命令行。

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.

开关的 -afzip WinRAR的只能切换它选择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.

开关的 -ag 与选项 MMM-YYYY 的结果追加到档案文件名文件扩展名前一个月的短名称格式和年份,例如月 - 2014年

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:处理文件和子文件夹\\ TE​​MP \\文件夹以COM preSS \\成存档,只是文件和子文件夹此文件夹中。

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

开关的 -m5 设置最佳COM pression存档。

The switch -m5 sets best compression for the archive.

开关的 -r 是一个非常重要的,因为它告诉 WinRAR的以递归COM preSS的所有文件和文件夹。

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:\Temp\Folder with Files and Subfolders to compress"
"C:\Program Files\7-Zip\7z.exe" a -mx=9 -r -tzip "C:\Path to Destination Folder of Archive\%Month%-%Year%.zip" "%FolderToCompress%\*"
rd /S /Q "%FolderToCompress%"
md "%FolderToCompress%"
endlocal

首先,命令 FOR 如果设置用于处理的 Windows管理规范命令来获取月份和年份。

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 for or for /?
  • help if or if /?
  • help set or set /?

或为微软TechNet文章的如果和的设置读的详细信息。

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

而在其他命令帮助 GOTO MD RD SETLOCAL 可以得到相同的方式

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 具有上的内容的选项卡具有项目的命令行版本的子项的命令<帮助/ STRONG>和开关

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

命令 是COM pressing文件放入档案。没有的 M WinRAR的优惠。所以经过COM pression删除必须单独完成。

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.

开关的 -m 与该选项的 X = 9 设置最佳COM pression。

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

开关的 -r 是因为它讲述了一个非常重要的的7-Zip 以递归COM preSS的所有文件和文件夹。

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

和开关的 -tzip 选择标准的邮政编码格式。

And the switch -tzip selects the standard ZIP format.

随着命令的 RD 文件夹:所有文件和子文件夹C处理文件和子文件夹\\ TE​​MP \\文件夹以COM preSS \\将被删除,接下来是 MD 我想文件夹本身应该保持下个月文件夹本身被重建。

With the command RD the folder "C:\Temp\Folder 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.

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

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