Zipping separate folders with system.io.compression

by Oct 10, 2013

Hello.

Im trying to write a script for automating zipping and archival of logs and files.

I found different zipping methods but i prefer to use the integrated IO.Compression Method.

Problem is as following.

I have the following Folderstructure:

\server1D$
     – EDI
           -MBinput
                  – Folder_00
                          -Processed
                                     – (Foldername based on creation date/Last write)
                                     – (Foldername based on creation date/Last write)
                                     – (Foldername based on creation date/Last write)
                                    
                  – Folder_01
                           -Processed
                                     – (Foldername based on creation date/Last write)
                                     – (Foldername based on creation date/Last write)
                                     – (Foldername based on creation date/Last write)

I need to zip each of the subfolders that is under the -Processed folder.
Each folder needs to be zipped separately and then delete the folder itself.

What i have is under and doesnt work. i can get it to work  with a singel folder but not multiple based on the task i want to do:

 

function Zipit( $zipfilename, $sourcedir )

{

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
$compressionLevel
= [System.IO.Compression.CompressionLevel]::Optimal
[
System.IO.Compression.ZipFile]::CreateFromDirectory( $sourcedir, $zipfilename, $compressionLevel, $false )

}

 

Edit:

I've tried something like:

$sourcedir = \server1D$etcetcetc

$dirs =

Get-ChildItem -Path $Sourcedir -Recurse |
Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Directory; $_.Name -like "Processed" }

 

This would list all folders which name is processed now i only need to get it to zip each folder seperately to a new location then delete the folder that has been zipped.