-->

Script to compress contents of a folder once per m

2019-09-07 05:54发布

问题:

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 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
)

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

回答1:

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:

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 solution

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\"

The destination folder of the ZIP archive must already exist on execution of 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.

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.

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.

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.

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

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

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.

The switch -m5 sets best compression for the archive.

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

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

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

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


7-Zip solution

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

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

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 /?

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

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

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.

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

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.

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

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

And the switch -tzip selects the standard ZIP format.

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.