Tássio Carlini
Tássio Carlini

Reputation: 47

Compress-7zip each object

I'm trying to generate a script that goes to the directory and compresses each file to .zip with the same name as the file.

I managed with the compress-archive, however it has limitations for files up to 2gb.

so searching, I found compress-7zip, but it uses different parameters.

$InputFolder= "C:\Temp\teste"
$OutputFolder="C:\Temp\teste"

#coletando arquivos
$CsvFiles = Get-ChildItem $InputFolder -Filter '*.xlsx'

#loop compactar arquivos
$CsvFiles | ForEach-Object { 
    $zipSetName =  $_.BaseName + ".zip"
    Compress-7zip -Path $InputFolder -Format Zip -ArchiveFileName $zipSetName
}

But I am not having success doing this with compress-7zip, the above script does not work hehe, am I passing wrong parameters? doing it manually works, however throwing all files into a single file. could someone help me make the above script work, which compresses file by file.

Upvotes: 1

Views: 1251

Answers (1)

Theo
Theo

Reputation: 61168

You can read the function you mentioned here, including the parameter names.

In this case, use

$CsvFiles | ForEach-Object { 
    $zipSetName = [System.IO.Path]::ChangeExtension($_.FullName, '.zip')
    Compress-7zip -FullName $_.FullName -OutputFile $zipSetName -ArchiveType Zip 
}

P.S. I used the ChangeExtension method here because both your input and output paths are the same. If that is not the case, use

$zipSetName = Join-Path -Path $OutputFolder -ChildPath ($_.BaseName + '.zip')

Upvotes: 2

Related Questions