Reputation: 35
In powershell, how should I zip all the files in a folder individually ? For example, I have 10 files in a folder which means I want to have 10 zips.
Below is my code but it only zips one file.
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
function ZipFiles( $zipfilename, $sourcedir )
{
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
$zipfilename, $compressionLevel, $false)
}
ZipFiles "zippath" "sourcepath"
Upvotes: 2
Views: 11475
Reputation: 36
use PowerShell go the the path that you would like to compress, do:
$folderlist = Get-ChildItem "."
foreach ($File in $folderlist) { Compress-Archive -path $File.Name -destinationPath "$($File.Name).zip"}
Upvotes: 2
Reputation: 597
Here is a one-liner that I just used that zips all files in the current directory using the filename and appending .zip extension
Get-ChildItem "." | ForEach-Object { Compress-Archive -path $_.Name -destinationPath "$($_.Name).zip"}
Upvotes: 6
Reputation: 1863
Function call will look like: ZipFiles "C:\Logs.zip" "C:\Logs"
$folderlist = Get-ChildItem -Path C:\ -Directory
Foreach ($Folder in $folderlist)
{ZipFiles "$($Folder.Fullname).zip" "$($Folder.Fullname)"}
Upvotes: 1