1

I have a lot of folders like below:

D:\forZip\2019-11
D:\forZip\2019-10
D:\forZip\2019-09
D:\forZip\2019-08
D:\forZip\2019-07
D:\forZip\2019-06

I want to:

  • ZIP compress the folders 2019-07, 2019-06, etc. if today's year and month is 2019-11;
  • ZIP compress the folders 2019-09, 2019-08, etc. if today's year and month is 2020-01.

How could this be done?

Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

This could be done with a batch file like this one:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ParentFolder=D:\forZip"

rem Get current date/time as string in format yyyyMMddHHmmss region independent.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDate=%%I"

rem Year of folders to ZIP compress is by default current year.
set "FolderYear=%CurrentDate:~0,4%"

rem Month of folders to ZIP compress is by default current month minus four
rem months. A number with a leading 0 would be interpreted as octal number
rem and 08 and 09 are invalid octal numbers being replaced on evaluation
rem of arithmetic expression by value 0 which is not wanted here. For that
rem reason character 1 is prepended to string of current month resulting in
rem the values 101 to 112 on evaluation of arithmetic expression subtracting
rem 104 to subtract in real four from current month value with leading 0 for
rem the months 1 to 9.

set /A FolderMonth=1%CurrentDate:~4,2% - 104

rem If the month value is less than one, the folder year must be decremented
rem by one and the folder month must be incremented by twelve to get correct
rem date in previous year.

if %FolderMonth% LSS 1 (
    set /A FolderYear-=1
    set /A FolderMonth+=12
)

rem Prepend a 0 to string value of folder month and next get the last
rem two characters from folder month to have finally the folder month
rem string always with two digits, i.e. 01, 02, 03, ..., 10, 11, 12.

set "FolderMonth=0%FolderMonth%"
set "FolderMonth=%FolderMonth:~-2%"

rem Search with DIR executed by a background command process started by FOR
rem for directores matching the pattern ????-?? in configured parent folder
rem and process this list of folder names without path. Each folder name is
rem split up into two parts - the four digit year assigned to loop variable J
rem and the two digit month assigned to loop variable K. If the year string
rem (not number) from folder name is less than the folder year string, this
rem folder is moved into a ZIP file. If the year string from folder name is
rem equal the folder year string and the month string from folder name is
rem less or equal the month string, the folder is also moved into a ZIP file.

echo Processing all folders up to %FolderYear%-%FolderMonth%.
for /F "eol=| delims=" %%I in ('dir "%ParentFolder%\????-??" /AD /B /O-N 2^>nul') do (
    for /F "tokens=1,2 delims=-" %%J in ("%%I") do (
        if "%%J" LSS "%FolderYear%" (
            echo Compressing folder %%I ...
            "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ep1 -ibck -inul -m5 -r "%ParentFolder%\%%I.zip" "%ParentFolder%\%%I"
        ) else if "%%J" == "%FolderYear%" if "%%K" LEQ "%FolderMonth%" (
            echo Compressing folder %%I ...
            "%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -cfg- -ep1 -ibck -inul -m5 -r "%ParentFolder%\%%I.zip" "%ParentFolder%\%%I"
        )
    )
)

endlocal

The batch file code contains comments which are the lines starting with rem. These comments explain most of the code. Please read for an explanation of the first FOR command line my answer on Why does %date% produce a different result in batch file executed as scheduled task?

I use since more than 20 years WinRAR as compression tool. For that reason the batch file is written to use shareware WinRAR.exe to compress the folders into ZIP archive files. The two command lines with WinRAR.exe can be replaced of course by any other command lines which call an executable or script to compress the current folder into a ZIP file and delete that folder after successful compression.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line specified between the two ' appended.

Mofi
  • 46,139
  • 17
  • 80
  • 143