title: Zip files older than 30 days using 7zip
body: <p>I know this question has been asked several times but my requirement is slightly different from what I have read here and on google.</p>
  
  <p>I have the below folder structure.</p>
  
  <pre><code>c:\weblogs\Servers\Server1
  c:\weblogs\Servers\Server2
  </code></pre>
  
  <p>Server 1 folder may contain folders such as:</p>
  
  <pre><code>W3SVC1
  W3SVC2
  W3SVC3
  </code></pre>
  
  <p>Server 2 would have a similar folder structure.</p>
  
  <p>I am trying to create a script which would look at every <code>W3SVC*</code> folder for <code>.log</code> files older than 30 days. </p>
  
  <p>It would then create a zip file called <code>Oldlogs-Server*-W3SVC*.zip</code> within each such <code>W3SVC*</code> subfolder and move the older logs to that file. On the second run it would simply move any new files older than 30 days to the respective zip file.</p>
  
  <p>I am not a developer but any examples or suggestions would be helpful if there is not an existing script available. </p>
  
  <p>This is the script which I believe comes closest to what I am trying to achieve:</p>
  
  <p>It currently throws an error:</p>
  
  <blockquote>
    <p>"\WebApps\LogHarvest\pkzipc.exe needed"</p>
  </blockquote>
  
  <pre><code># Alias for pkzip
  if (-not (test-path "$env:F:\WebApps\LogHarvest\pkzipc.exe")) {
      throw "$env:F:\WebApps\LogHarvest\pkzipc.exe needed"
  } 
  set-alias sz "$env:WebApps\LogHarvest\pkzipc.exe" 
  
  ############################################ 
  #### Variables  
  
  $filePath = "F:\Weblogs\Test" 
  
  $log = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".log" } 
  
  ########### END of VARABLES ################## 
  
  foreach ($file in $log) { 
      $name = $file.name 
      $directory = $file.DirectoryName 
      $zipfile = $name.Replace(".log",".7z") 
      sz a -t7z "$directory\$zipfile" "$directory\$name"      
  } 
  
  ########### END OF SCRIPT ########## 
  </code></pre>