Zipping files older than X days


 

Recently I was working with a firewall that wrote some tremendously large log files and was eating disk at an alarming rate, so to remedy this, and sharpen my Powershell pencil, I decided to write a script that could be setup with task scheduler to zip up older logfiles....

 

The script requires the Powershell Community Extensions (PSCX) module http://pscx.codeplex.com/ as it uses the Write-Zip cmdlet.

 

Running the script with no switches will prompt for the file type, path and age.  

 

To schedule this you would need to add a check for the PSCX module :

if(-not(get-module -name pscx -ea:silentlycontinue)){import-module pscx}

 

Then you can just invoke it with Zip-OldFiles.ps1 log c:\temp\logs 10

The script will ZIP every file older than 10 days in the C:\temp\logs folder, and delete the source file when done.   Resulting ZIP will be originalfilename.extension.zip (eg. Firewall.log becomes Firewall.log.zip).

 

param(

[Parameter(Position=0, Mandatory=$true)] 

[string]$filetypes = $(Read-host "Enter the type of files to ZIP (don't include * or .)"),     

[Parameter(Position=1, Mandatory=$true)] 

[string]$TargetFolder = $(Read-host "Please Enter The Directory Path"),     

[Parameter(Position=2, Mandatory=$false)] 

[string]$Days = $(Read-Host "How Many Days?")

)

if (Test-Path $TargetFolder)

{

  $Now = Get-Date

  $LastWrite = $Now.AddDays(-$days)

  $Files = get-childitem $TargetFolder -include *.$filetypes -recurse | Where {$_.LastWriteTime -le "$LastWrite"}

  if ($files | write-zip) {$files | remove-item}

}

else

  {Write-Host "The Folder $TargetFolder Does Not Exist!" -foregroundcolor "Red" -backgroundcolor "Black"}

Comments (3)

  1. Anonymous says:

    Work with zip archives via how to fix a corrupted zip file ?

    http://www.zipfilefix.com/

  2. Anonymous says:

    You may come back your zip files thanks to download winzip error fix

    More info find at http://www.zip.fixtoolboxx.com

  3. http://www.zip-repair.org says:

    Zip repair easily repair corrupt and damaged zip files CRC errors and fix zip files corruption issues

Skip to main content