Slecting multiple directories then zipping it

Welcome Forums General PowerShell Q&A Slecting multiple directories then zipping it

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 7 months ago.

  • Author
    Posts
  • #38355

    Participant
    Points: 0
    Rank: Member

    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)

  • #38357
    AK

    Participant
    Points: 14
    Rank: Member

    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

  • #38360

    Participant
    Points: 0
    Rank: Member

    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 ..

  • #38363
    AK

    Participant
    Points: 14
    Rank: Member

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

  • #38396

    Participant
    Points: 0
    Rank: Member

    Thanks AK..

The topic ‘Slecting multiple directories then zipping it’ is closed to new replies.