1

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
    }
}
Ralf W.
  • 13
  • 4
  • SO is not a free code writing service! Try it on your own, and when you are stuck, come back here, describe precisely what you want to achieve and what your code actually does. – aschipfl Jul 07 '16 at 10:28

2 Answers2

1

Here is something you can start with in PowerShell. It assumes you are using PowerShell 5 (for the Compress-Archive cmdlet).

$dirpath = "D:\temp"

dir $dirpath | group  { $_.Name.Substring(0, 8) } | %{
    $subset = $_

    $subset.Group | %{
        Compress-Archive $_ $subset.Name -Update
    }
}
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • Hi David, thanks for your response. I assume Name.Substring(0, 8) Groups the name by the first 8 characters? PS responds with the following, any idea what might be the reason? Non existing path M63251-25-.txt:string. At H:\tools\powershell\Group_and_Archive.ps1:7 char:9 + Compress-Archive $_ $subset.Name -Update + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (M63251-25-.txt:String) [Compress-Archive], InvalidOperationException + FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Compress-Archive – Ralf W. Jul 07 '16 at 08:27
  • Found the mistake by myself. The script needs to be within the same directory like the files. Now it is working!!! Thanks David. Can you tell me how to set an specific dir. So there is no need that the for the script to be at the exact same dir? Thanks a lot David! – Ralf W. Jul 07 '16 at 08:34
0

you can work with wildcards. There is ? for "any character" and * for "one or more characters". This is called a "file mask", for example M000001-??-.txt.

dir "M000001-??-.txt"
dir "M000001*.txt"
dir "M000001*.*"

Your zip-application most probably also supports file masks, so you can do something like (syntax heavily depends on which application you use - see your documentation):

ZipApp.exe /zip "M000001-??-.txt" "M000001.zip"

Well, that was the easy part. Now to process all your files. You have common prefixes (M000xxx), which allows you to use a simple counter (for /L), add enough leading zeros, take the last 6 characters from that and finally add another "M" in front of it. With that, you can easily build your filemask and your zip file name.

@echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,10) do (
  set "n=0000000%%i"
  set "n=M!n:~-6!"
  echo Mask="!n!-??-.txt" Zipfile="!n!.zip"
)

You need delayed expansion to process your variables inside the for loop.

Instead of the echo line, execute your zip application with the correct parameter syntax. (Can't help you with that - see it's documentation)

If you are unsure about a command, see it's help with command /? (ex. for /?, set /?)

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you for your response. Sorry but I did not mention this in my initial question.The names are not chronological order. More like M065788-01-.txt, M065788-02-.txt.... M0255288-01-.txt, M0255288-02-.txt. Thanks for your effort. – Ralf W. Jul 07 '16 at 07:38