Slecting multiple directories then zipping it

This topic contains 4 replies, has 2 voices, and was last updated by  roland 2 years ago.

  • Author
  • #38355


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

    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)

  • #38357


    Hello Rolando,
    You might find these bits useful:

    $source = "directory_2_archive"
    $destination = "destination_directory" + "";

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


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

  • #38360


    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


    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)
    Start-sleep -milliseconds 500

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

  • #38396


    Thanks AK..

You must be logged in to reply to this topic.