7zip – Use Windows Task Scheduler to zip a file that has timestamp of previous day

Posted on

Problem :

I have a web server that generates log files everyday, the log of the day will be automatically saved as Log.YYYY-MM-DD.txt (i.e. Log.2016-07-20.txt).

Now I need to use Windows Task Scheduler to zip the log file for the previous day, is there an option available in 7zip cmd that would automatically identify the file name based on the current date?

Solution :

Batch Script – Subtracting (or adding) Days from Current Day Simplified

You can do this using a batch script that sets the date variable and subtracts 1 day (or any number of days for that matter) from the current date which the batch script is executed. So you’d need to ensure that the day you schedule the script to execute, that that day is what you expect the calculation to take place to determine the date values in the log file name for the previous day.

I put more comments in the script to explain what each variable does and so on too, but basically you set the variables in the batch script as usual, and it’ll use those to build three dynamic VB Scripts to do the date subtraction calculation, add leading zeros where needed, and so forth for the YYYY, MM, and DD variables.

To add: You can also use this script to add a number of days to the current date by deleting the minus (-)
symbol from the below batch script in the :DynamicVBSScriptBuild routine, so where you see this,-%MinusDay%, you simple remove the minus symbol to get ,%MinusDay%, on each of those lines and now the MinusDay= variable value will equal the number of days you want to add.

Important Note: It seems that five 9’s (99999) is the limit on the batch script when subtracting with the MinusDays= value. It also seems that six 9’s (999999) is the limit on the batch script when adding with the MinusDays= value.

Batch Script

@ECHO ON

::// Minus days is the number of days to subtract from the CURRENT DAY i.e. 2 for minus 2 days or 99999 for minus 99999 days from when it's run
SET MinusDay=2

::// Set the archive file full path and file name
SET ArchiveFile=C:ArchivePathArchive.zip

:: This calls the temp vbs script routine that will be used to set YYYY-MM-DD values for the subtracted days date you specify
CALL :DynamicVBSScriptBuild

FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%YYYYTmpVBS%"') DO SET YYYY=%%A
FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%MMTmpVBS%"') DO SET MM=%%A
FOR /F "TOKENS=*" %%A IN ('cscript//nologo "%DDTmpVBS%"') DO SET DD=%%A

::// Set your web server log file path in the below variable
SET WebServerLogPath=C:WebServerLogs

::// Set web server log file name where YYYY MM DD variables are set to the values after the day numbers setup above are subtracted
SET WebServerLogFile=Log.%YYYY%-%MM%-%DD%.txt

::CD /D "C:Program Files7-Zip"
7z a "%ArchiveFile%" "%WebServerLogPath%%WebServerLogFile%"

::GOTO EOF
EXIT /B

:DynamicVBSScriptBuild
SET YYYYTmpVBS=%temp%~tmp_yyyy.vbs
SET MMTmpVBS=%temp%~tmp_mm.vbs
SET DDTmpVBS=%temp%~tmp_dd.vbs
IF EXIST "%YYYYTmpVBS%" DEL /Q /F "%YYYYTmpVBS%"
IF EXIST "%MMTmpVBS%" DEL /Q /F "%MMTmpVBS%"
IF EXIST "%DDTmpVBS%" DEL /Q /F "%DDTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%YYYYTmpVBS%"
ECHO yyyy = Year(dt)                    >> "%YYYYTmpVBS%"
ECHO WScript.Echo yyyy                  >> "%YYYYTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%MMTmpVBS%"
ECHO mm = Right("0" ^& Month(dt),2)     >> "%MMTmpVBS%"
ECHO WScript.Echo mm                    >> "%MMTmpVBS%"
ECHO dt = DateAdd("d",-%MinusDay%,date) >> "%DDTmpVBS%"
ECHO dd = Right("0" ^& Day(dt),2)       >> "%DDTmpVBS%"
ECHO WScript.Echo dd                    >> "%DDTmpVBS%"
GOTO EOF

Further Resources

I think it is always quite complicated to calculate dates. Therefore, I would like to propose an alternative solution to achieve what you need.

I would use another scheduled task, to output the current date with the format YYYY-MM-DD in a file.
You can find information on how to do that here.

Then, in your scheduled job that is responsible for the zipping, you just read the file and zip all the files that matches with the date. The only problem is that you would need to run the first task a day earlier.
Like:

Run task 1 at 23:55

Run task 2 at 00:05

Sounds very “unintelligent” but it gets the job done.

Leave a Reply

Your email address will not be published. Required fields are marked *