2

I'm on Windows 10 and trying to work it out with Winrar. The folder contains 500 files, I would like to compress every 10 of them as a separate zip/rar file and create 50 zip/rar files.

Is there a way to not do it manually?

3 Answers 3

2

My suggestion would be to use something like a custom script, which would also give you good flexibility.

Since you are using Windows, I quickly developed a simple PowerShell script for you that doesn't even require WinRAR.

Create a .ps1 text file (e.g. Create-MultiZip.ps1) with the following content:

param
(
    # The input folder containing the files to zip
    [Parameter(Mandatory = $true)]
    [string] $InputFolder,

    # The output folder that will contain the zip files
    [Parameter(Mandatory = $true)]
    [string] $OutputFolder
)

Set-Variable SET_SIZE -option Constant -value 10
$i = 0
$zipSet = 0

Get-ChildItem $InputFolder | ForEach-Object {
    $zipSetName = "archive" + ($zipSet + 1) + ".zip"
    Compress-Archive -Path $_.FullName -Update -DestinationPath "$OutputFolder\$zipSetName"
    $i++;

    if ($i -eq $SET_SIZE) {
        $i = 0;
        $zipSet++;
    }
}

Then run it in a PowerShell environment, providing the folder containing the files you want to zip, followed by the folder which will contain the zip files. For example:

Create-MultiZip.ps1 C:\tmp\input C:\tmp\output

What does the script do?
Basically it gets a list of all the files in the input folder, then each file is added to the archive1.zip file in the output folder, until the prefixed set size is reached. At that point, a new archive2.zip file is created, the same as before... and so on, until all the files in input have been processed.
Note that the resulting files are called archive<n>.zip, where n represents the progressive number for each zip file.

The heart of the script is the Compress-Archive cmdlet. It was recently introduced with PowerShell 5.0, which comes by default in Windows 10.

If you know Powershell a bit, the script can be easily improved, for example by adding a CompressionLevel, or some text output to the console.

It's also straightforward to change the number of files you want in each set, you just need to modify the constant value at the following line:

Set-Variable SET_SIZE -option Constant -value 10

I've tested the script briefly and it works fine - it goes without saying that you should give it a go on some test files first.

2

Here's a script to get you started. I must confess I'm not familiar with the rar command line, so you may need to tweak that.

@echo off
Setlocal EnableDelayedExpansion

:: Folder containing files
set input=D:\temp
:: Folder for zips
set output=D:\output
:: Temp filename to hold list of 10 
set listfile=%temp%\listfile
:: Zip counter
set z=1
:: Files per zip
set n=10

cd /d %input%

if exist %listfile% del %listfile%

set i=0

for %%f in (*.*) do (
    set /a i=!i! + 1
    echo %%f >> %listfile%

    if !i!==%n% (
        rar a %output%\!z!.rar @%listfile%
        set i=0
        set /a z=!z! + 1
        del %listfile%
    )
)

:: Process remaining files, if any
if exist %listfile% (
    rar a %output%\!z!.rar @%listfile%
    del %listfile%
)
1

This improves mguassa's solution by reducing 500 compress actions to 50.

$InputFolder = "c:\users\ching\Downloads\app"
$OutputFolder = "c:\users\ching\Downloads\arc"

$filesPerZip = 10
$counter = [PSCustomObject] @{ Value = 0 } 

$arcs = gci $InputFolder | ?{!$_.PsIsContainer} | group {[Math]::Floor($counter.Value++ / $filesPerZip)}

cd $InputFolder
$OutputZipPrefix = $OutputFolder + "\" + ($InputFolder | Split-path -leaf) 
foreach ($arc in $arcs) {

    compress-archive $arc.group ($OutputZipPrefix + $arc.name + ".zip")
}

The output zip files are named as app0.zip, app1.zip,... according to the input folder name in the output folder.

Every zip file is created in one-shot. It can reduce the processing time and avoid from possible disk fragmentation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .