To Zip all files indvidually with in a folder


Credit to http://greijer.blogspot.co.uk/2013/06/zip-all-files-in-folder-individually.html

Create a powershell file (in my case compress1.ps1 containing the following:

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$sourceFolderPath,
[Parameter(Mandatory=$True,Position=2)]
[string]$filePattern,
[Parameter(Mandatory=$True,Position=3)]
[string]$7zipExe

)

$sourceFilePattern= $sourceFolderPath +”\” + $filepattern

$files=get-childItem $sourceFilePattern

$ErrorActionPreference = “Stop”

if ($files)
{
foreach ($file in $files)
{
$zipFileName=$file.fullname+”.zip”
$programArgs = “a”, “-tzip”, $zipFileName , $file.fullname
write-host $programArgs
Invoke-Command -ScriptBlock { & $7zipExe $programArgs }
remove-item $file
}
}

Run it using

powershell -file “c:\XXXX\compress1.ps1” “YYYYY” “*.bak” “c:\program files\7-Zip\7z.exe”

change the path to the ps1 file xxxx

change YYYY to the path of the folder that you want to zip the files up within ie c:\backups


Leave a Reply

Your email address will not be published. Required fields are marked *