title: How do I select files in a folder based on part of filename and zip them in Powershell?
body: <p>I'm fairly new to Powershell(using Powershell 2.0 btw) and am trying to make a script that does several things(this is my 3rd script or so). I have most things in place but the last thing remaining is to group files of different types (xml, tfw and tif) in a folder, based on the first part of the filename(first three characters) and then zip these files into several zip-files with name like the first 3 characters, either in the same location or in a new one.</p>
  
  <p>Sample of folder content:</p>
  
  <pre><code>001.tif
  001.tfw
  001.metadata.xml
  002.tif
  002.tfw
  002.metadata.xml
  003.tif
  003.tfw
  003.metadata.xml
  003_svel.tif
  003_svel.tfw
  003_svel.metadata.xml
  </code></pre>
  
  <p>Wanted result:</p>
  
  <pre><code>001.zip containing 001.tif, 001.tfw, 001.metadata.xml
  002.zip containing 002.tif, 002.tfw, 002.metadata.xml
  003.zip containing 003.tif, 003.tfw, 003.metadata.xml, 003_svel.tif,
  003_svel.tfw and 003_svel.metadata.xml
  </code></pre>
  
  <p>I have installed 7-zip to do the zipping and am using the commandline version. I've used 7-zip local on some testfiles and got it to work, but then it was only tif-files. I have a source folder where I search for the latest created folder and then process the files in it.
  This is what I have so far(Powershell 2.0):</p>
  
  <pre><code>$dir_source = "c:\Test"
  $new_folder = Get-ChildItem $dir_source -Recurse |
  Where { $_.PSIsContainer} | 
  Sort-Object LastWriteTime -Descending | 
  Select-Object -ExpandProperty    Fullname &lt;-First 1
  Get-ChildItem $new_folder -recurse -Exclude metafile.xml | 
  Group-Object {$_.Name.Substring(0,3)} 
  </code></pre>
  
  <p>This gives me a list of grouped files in the lates created folder based on the first 3 characters in the filename. It also show what files are in each group. 
  Like below:</p>
  
  <p><strong><em>Count Name                      Group<br>
  ----- ----                      -----<br>
      3 003                       {C:\Test\20150708 063255_B\003.metafile.xml, C:\Test\20150708 063255_B\003.tfw, C:\Test\20150708 063255_B\003.tif}<br>
      6 004                       {C:\Test\20150708 063255_B\004.metafile.xml, C:\Test\20150708 063255_B\004.tfw, C:\Test\20150708 063255_B\004.tif,C:\Test...
      6 009                       {C:\Test\20150708 063255_B\009.metafile.xml, C:\Test\20150708 063255_B\009.tfw, C:\Test\20150708 063255_B\009.tif,C:\Test...</em></strong></p>
  
  <p>Now my next step ist to take these groups and zip them. Ideally create these zip-files in a different destination directory (I believe I can change this when setting the $directory- variable in the script below.)</p>
  
  <pre><code>foreach ($group in $dataset) { 
                  $name = $file.name 
                  $directory = $file.DirectoryName
                  $zipFile = $file.Name + ".zip" 
                  sz a -t7z "$directory\$zipfile" "$directory\$name" 
  </code></pre>
  
  <p>This last code is causing some trouble. I either get the message: </p>
  
  <blockquote>
    <p>7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18 Error:
    c:\Test\Dest_test460.zip is not supported archive System error:
    Incorrect function.</p>
  </blockquote>
  
  <p>,or</p>
  
  <blockquote>
    <p>WARNING: Cannot find 1 file 7-Zip (A) 9.20  Copyright (c) 1999-2010
    Igor Pavlov  2010-11-18 Scanning \460:  WARNING: The system cannot
    find the file specified.</p>
  </blockquote>
  
  <p>,or it starts zipping all files on my userprofile into a zip-file. Depending on changes I do to the $group-value. I believe there are one ore more basic errors in my script causing this, and this is where I'm asking for some help. It may be that I am approaching this the wrong way by first grouping the files I want and then try to zip them? </p>
  
  <p>Anyone that can see my error or give me some hint to what I have to do?</p>
  
  <p>Thanks for your time!</p>