title: Powershell and 7zip - multiple files from multiple (sub)folders
body: <p>I'm working on a (quite simple) powershell script that has to compress multiple files (.bak files in this case) from multiple subfolders to a destination folder.</p>
  
  <p>Something like this:</p>
  
  <p>From:</p>
  
  <p>f:\backup\user1\file1.bak<br>
  f:\backup\user2\file2.bak </p>
  
  <p>To: </p>
  
  <p>x:\backupfolder\users\file1.zip<br>
  x:\backupfolder\users\file2.zip</p>
  
  <p>Script:</p>
  
  <pre><code>$path = "f:\backup\"
  $dest = "x:\backupfolder\users\"
  $mask = "file*.bak"
  
  $days = 1
  
  $files = dir $path -Recurse -Include $mask | where {
      ($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)
      }
  
  ForEach ($file in $files) 
      {
      7z.exe a -tzip "$dest\$file.name.zip" $file
      }
  </code></pre>
  
  <p>The problem is that, in this case, i get The specified path is invalid error, because 7zip tries to create an archive in the form of this</p>
  
  <pre><code>x:\backupfolder\users\f:\backup\user1\file1.bak.name.zip
  </code></pre>
  
  <p>And if i try this:</p>
  
  <pre><code>7z.exe a -tzip "$dest\$_.basename.zip" $file
  </code></pre>
  
  <p>I get an archive x:\backupfolder\users\.basename.zip</p>
  
  <p>All i want this script to do is compress files (file1.bak) to an archive with the same name (file1.zip) to a folder i told it to.</p>
  
  <p>What am i doing wrong??</p>
  
  <p>TYVM.</p>