Windows – Compress files to daily/weekly/monthly archives with Windows scheduled tasks

archivefilesscheduled-taskwindows

For EDI files which need to be archived, I would like to use a compression utility (like 7Zip) which collects and compresses files based on their file date. For example, a weekly archive (incoming-2009-01.7z for week 1) needs to be updated with all incoming files whose file date is in this week.

Is there a command line utility which can be used to invoke a compression tool with the necessary arguments, or an other easy way to implement such an archive strategy?

Best Answer

I don't know of a utility that provides this directly but it's fairly straightforward to build a script (using powershell, perl, python ..) to identify the files that meet your criteria, drop the fully qualified names (e.g. d:\incoming\something\filename.ext ) into a flat text file, one file name per line and then have your compression utility create an archive from that list file e.g.

7z -a output.zip @filelist.txt

If you wrap the whole thing inside the script then it's trivially easy to assign the type of archive names that you specified.

Added a Powershell example.

$archiveroot="c:\temp"
$oldest = (get-date) - (new-timespan -day 31)
$archivename="Incoming-" + $oldest.year + "-" +$oldest.month+".7z"
$filelist= get-childitem $archiveroot -recurse | where-object {$_.lastwritetime -gt $oldest}
$filelist | format-table -hideTableHeaders FullName | out-file -encoding utf8 -filepath lastmonthsfiles.txt
& .\7z.exe a $archivename `@lastmonthsfiles.txt

It's not quite a one liner, you will need to point it properly at the 7-Zip exe, add some parameter handling, some logging\error handling and it should clean up the working files but it's a pointer in the right direction.