Author Posts

April 28, 2016 at 9:36 am

I'm new in PowerShell and I need help. I have multiple folder and I need to create a zip archive.

Basically,
Here is my script. Here's what I need to do..
* Get the name of the folders
*Create a separate zip archive for each folder.
*add the folder in the zip archive created above.

But, This script only creates .zip archive and copy all the folders in 1 zip archive.

Appreciate anyone help!

Get-ChildItem | Where-Object {$_.PSIsContainer} | Select-Object FullName
$ZipFileName = $LCfolder + ".zip"
Set-Content $ZipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
# Wait for the zip file to be created.
while (!(Test-Path -PathType leaf -Path $ZipFileName))
{
Start-Sleep -Milliseconds 20
}
$ZipFile = (new-object -com shell.application).NameSpace($Zipfilename)
Write-Output (">> Waiting Compression : " + $ZipFileName)
$ZipFile.CopyHere($LCfolder)

April 28, 2016 at 10:46 am

Hello Rolando,
You might find these bits useful:

$source = "directory_2_archive"
$destination = "destination_directory" + "your_archive_name.zip";

if(test-path $destination) {Remove-Item $destination}
Add-Type -Assembly "system.io.compression.filesystem"
$compressionlevel = [system.io.compression.compressionlevel]::Optimal

[io.compression.zipfile]::CreateFromDirectory($source,$destination,$compressionlevel,$true)

There is also a script called "new-zip" on technet, but I've never used it:

https://gallery.technet.microsoft.com/1baf63c9-d904-412e-bcf9-d811a2276cc6

April 28, 2016 at 1:56 pm

Thanks AK, I tried that already.. I need to be able to add the folder then, if any changes in the folders it will go in the same zip archive.
The script above creates an empty zip archive then add the folders!
Thanks for your help ..

April 28, 2016 at 4:12 pm

Forgive me, I did not understand the part about adding change in folders...

Try lsing each directory into a variable, then adding each file to an archive... I believe if dir2archive contains directories, they'll be added too:

$dir2archive = (Get-ChildItem).FullName
foreach($file in $dir2archive)
{
$ZipFile.CopyHere($file)
Start-sleep -milliseconds 500
}

For recently (5 days) changed files try this:
(Get-ChildItem directory2archive -Directory).WHere({$_.LastWriteTime -ge (get-date).AddDays(-5)}).Foreach(
{
$ZipFile.CopyHere($_.FullName)
Start-sleep -milliseconds 500
})