-1

I want to use PowerShell and 7zip to create an archive file in a directory where in name of file is number for example 1 and create archive from files 1 through 100. Then create a second archive from files 101 through 200, and so on to 100'000 (I have 100k files in that directory).

How can I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Did you try the solutions here: https://stackoverflow.com/questions/13180346/script-to-create-archive-using-powershell-and-7zip? – harper Aug 24 '18 at 10:00
  • this doesn't work in my case because my file in folder are without extension so i can't do this. file name for example WP_LOG_00001,WP_LOG_00002 and so on. – Marek Korcz Aug 24 '18 at 10:08
  • Please don't extend your question text within comments. Update the question if you have additional information. – harper Aug 24 '18 at 10:17
  • Is your pattern `files 101 through 200` related to the file names `WP_LOG_00001`? In that case you need to break the task: 1) create a function that calls the external archiver (7-Zip) that accepts an array of file names. 2) create a function the scans your directory and sleects the file names that match a pattern using a regular expression. 3) call the second function to get the file name arrays and use this arrays to invoke the archiver with the first function. If this is unclear, write a new question about the specific problem. – harper Aug 24 '18 at 10:21
  • @harper do you have some example of that kind of script that include all task? – Marek Korcz Aug 24 '18 at 10:49
  • can you try my example? – Pedro Monte Aug 24 '18 at 14:25

1 Answers1

0

Try this example: This will get all files from a directory and compress 5 files for each .zip. You may need to adapt parts of the code to your requirements ( like the 5 to 100, the 5 was for testing less files. )

Set-Location $PSScriptRoot

#UPDATE THIS VARIABLES ACCORDING YOUR NEEDS
$7zip = ".\packages\7zip\7za.exe"
$filesDir = ".\files"
$numberOfFilesPerZip = 5
#-------

$count = 0
$totalFile = 0

$filesToCompress = Get-ChildItem $filesDir
$zipFileName = "1_To_{0}" -f $numberOfFilesPerZip

foreach($file in $filesToCompress){
    $totalFile++
    $fileFullPath = $file.FullName

    if($count -lt $numberOfFilesPerZip){    
        $count++
    } else {
        $zipFileName = "{0}_To_{1}" -f $totalFile, ($totalFile + $numberOfFilesPerZip - 1)          
        $count = 1
    }

    Invoke-Expression -Command "$7zip a $zipFileName '$fileFullPath'"

}

The output will be

  • 1_To_5.7z
  • 6_To_10.7z
  • 11_To_15.7z
  • ...
Pedro Monte
  • 235
  • 2
  • 10