Zip all files in folder individually

This script will remove items after being zipped. Filename is appended .zip
Create two files from code templates below then edit and execute zipfiles.cmd

zipfiles.ps1



[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
}
}


zipfiles.cmd


powershell -file .\zipfiles.ps1 "c:\FolderWithFiles\ManyTextFiles" "*.txt" "c:\program files\7-Zip\7z.exe"
powershell -file .\zipfiles.ps1 "c:\FolderWithFiles\ManyCsvFiles" "*.csv" "c:\program files\7-Zip\7z.exe"




Comments

Popular posts from this blog

Constructing nested array structs in BigQuery

How to decrypt stored password from SSMS registered servers

Cause for Parameter is incorrect 0x80070057 error in ssis