My first suggestion is using WinRAR.
This command line can be used as scheduled task command line directly as also in a batch file.
"%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -agYYY-MM-DD -cfg- -ep1 -ibck -inul -m5 -tn1d -x*.zip -y -- "E:\Logs\SmartLogger\Logs_.zip" "E:\Logs\SmartLogger\*"
The help of WinRAR opened by clicking in GUI main window of WinRAR in menu Help on menu item Help topics contains on tab Contents the list item Command line mode with the help pages:
- Command line syntax
- Commands
- Switches
A command line as above can be easily written on using those 3 help pages and reading the help pages of the switches which seem to be interesting for the current compression/extraction task.
See the Alphabetic switches list to understand the used switches and how this single line makes the entire job.
My second suggestion is using 7-Zip which really requires a batch file as 7-Zip (of version 16.04) does not have switches for file date depending (backup/compression) operations.
Here is my solution using 7-Zip compressing each file with last modification date being todays date into a *.zip file with Logs_YYYY-MM-DD.zip
as file name.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ListFile=%TEMP%\ListFile.tmp"
del "%ListFile%" 2>nul
rem Get todays date in region and language dependent format required for
rem last modification date evaluation of all files in source directory.
echo Get todays file date.>"%TEMP%\GetTodaysFileDate.tmp"
for %%I in ("%TEMP%\GetTodaysFileDate.tmp") do set "Today=%%~tI"
set "Today=%Today:~0,-6%"
del "%TEMP%\GetTodaysFileDate.tmp"
echo Today is: "%Today%"
rem Set current directory being the directory with the files to process.
cd /D "E:\Logs\SmartLogger"
rem Search with DIR for files with todays date as last modification date
rem output by DIR sorted by last modification date with newest file first.
rem The found files with todays date are written into a list file. The
rem loop is exited if the first file is processed not having todays date.
for /F "delims=" %%I in ('dir /A-D /B /O-D /TW * 2^>nul') do (
set "FileDate=%%~tI"
set "FileDate=!FileDate:~0,-6!"
if not "!FileDate!" == "%Today%" goto CompressFiles
if /I not "%%~xI" == ".zip" echo %%I>>"%ListFile%"
)
:CompressFiles
if not exist "%ListFile%" goto EndBatch
rem Get todays date in a Windows region and language settings
rem independent format for usage in the ZIP file name.
for /F "skip=1 delims=." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime') do set "CurrentDate=%%I" & goto ReformatDate
:ReformatDate
set "ZipFileName=Logs_%CurrentDate:~0,4%-%CurrentDate:~4,2%-%CurrentDate:~6,2%.zip"
"%ProgramFiles(x86)\7-Zip\7z.exe" a -tzip -mx5 -scsDOS -sdel -y -- %ZipFileName% "@%ListFile%" >nul
del "%ListFile%"
rem Restore the environment (environment variables, current directory,
rem state of command extensions and delayed expansion) before calling
rem the command SETLOCAL at top of this batch file.
:EndBatch
endlocal
This batch file expects that the folder does not contain any file with a last modification date in future in comparison to todays date. This limitation is caused by the optimized evaluation of the file dates of all files found in the directory with the log files. This limitation does not exist on removing the line with goto CompressFiles
in the FOR loop.
Also 7-Zip has a help file which is 7zip.chm
in program files folder of 7-Zip which after opening with a double click contains on tab Contents under list item Command Line Version the help pages
- Syntax
- Commands opening Command Line Commands
- Switches opening Command Line Switches
Please first check if the line echo Today is: "%Today%"
really outputs on the machine running this batch file under the user account defined for the scheduled task just the file date (without or with weekday) not including a space character and file time.
The Windows region and language settings of used user account determine the date/time format of %%~tI
. The SET command lines below the two command lines with %%~tI
must be perhaps adapted to get just todays date without time.
It would be possible to use WMIC also for getting the last modification date of each file as described in answer on Find out if file is older than 4 hours in batch file, but that is really slow and would require even more code.
For understanding the used Windows commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
del /?
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of >nul
and 2>nul
.