I have a huge amount of files (> 30K), that are all stored within one directory and need to be reorganized and archived.
Files are named like (non chronologic order):
- M056811-01-.txt
- M056811-02-.txt
- M056811-03-.txt
- M956782-01-.txt
- M956782-02-.txt ...
I want to archive files that belong together (have same prefix) to one zip file. Which would result in sort of:
- M056811.zip
- M956782.zip ...
Is there a way to achive this with some Batch or Powershell scripting.
I am not very advanced in scripting. Except from the answer itself (if there is a solution) I'd really like to know it works.
My final solution (big thanks to David Brabant):
I edited the script for remote dir usage. There is no need to place the script within the same dir. Just edit the first two variables and you should be good to go.
$dirpath = "<Enter Directorypath here>"
$offsetlength = 6
$pathlength = $dirpath.Length
$absolutelength = $pathlength + $offsetlength
dir $dirpath | group { $_.FullName.Substring(0, $absolutelength) } | %{
$subset = $_
$subset.Group | %{
Compress-Archive $_ $subset.Name -Update
}
}