On a Microsoft Windows server, there is a folder that contains 1000s of logfiles. Each logfile varies by name and size. I wanted a script that would compress all logfiles into a single zip file that were older than seven days. After the compression is completed and the zip file is created, all logs older than seven days will be deleted. After much trial and error, I have created the following workable script that utilizes 7-zip.
The date format will be yyyymmdd and the time format will be hhmmss. You can get creative and create user variables for paths and so on, however, this script in this basic form should achieve the desired goals outlined above. The purpose for the >nul 2>&1 is to surpress any error messages that will occur if there is essentially nothing to do, if there are no files that match the date criteria for example. The @echo off prevents the set commands from being written to the screen.
@echo off for /f "tokens=1-3 delims=:." %%a in ('echo %time%') do set hhmmss=%%a%%b%%c for /f "tokens=2,3,4 delims=/ " %%I in ('DATE /T') do set yyyymmdd=%%K%%I%%J forfiles.exe /p d:\mypath /m *.log /d -7 /c "cmd /c 7z a -tzip %yyyymmdd%_%hhmmss%_myfilename.zip @file" >nul 2>&1 forfiles.exe /p d:\mypath /s /m *.log /d -7 /c "cmd /c del @file" >nul 2>&1
Source(s) / Reference(s)