Find folders and zip them

This topic contains 3 replies, has 3 voices, and was last updated by  Matt Bloomfield 1 week ago.

  • Author
    Posts
  • #99000

    Thursten
    Participant

    Hello,
    I'm trying really hard to get the hang of Powershell, but I have a hard time sometimes figuring it out..

    I have the following situation:

    We have a folder structure like this:
    G:\Stuff\Logs

    Inside the "Logs" folder there are recursive folders like this:

    Logs
       Product
           Client
              2011
                 01
                 02
                 03
              2012
                 01
                 02
                 03
              2013
                 01
    

    Etc. Now the idea is with one single script, we want each folder in the year's folders to be zipped to their own zip file.
    But the folder 2018 should not be zipped everything before should.
    I have the following code, But I'm stuck on how to get it to zip subfolders recursively..

    Get-ChildItem G:\StUF\Log -Depth 3 -Recurse -Directory | 
    Where-Object If (-Not "2018"){Get-ChildItem .... 
    

    And somewhere with a "For-Each" statement I imagine it would be:

     | Compress-Archive -DestinationPath $Path\$Folder.zip 

    But I dont want to use absolute paths, it should zip if there is any..

    I'm sorry for not being able to explain it too clear..
    I'm getting headaches from this stuff.. Sorry =X

    Thanks in advance!
    PS. I'm sorry if it's not formatted right or the wrong section, I'm new here, but eager to learn! =)

  • #99078

    Matt Bloomfield
    Participant

    I hope this is fairly self explanatory but if you want me to elaborate, please ask.

    $outputFolder = 'E:\Temp\zip\'
    
    $dirToZIP = Get-ChildItem "E:\Temp\Logs\*\*\*\*\" | Where-Object {$_.Parent -notlike '*2018*'}
    
    foreach ($dir in $dirToZIP) {
    
        $year = $dir.Parent | Split-Path -Leaf
        $month = $dir | Split-Path -Leaf
        
        if (-not(Test-Path "$outputFolder\$year")) {
    
            New-Item -Path "$outputFolder\$year" -ItemType Directory | Out-Null
    
        }
    
        Compress-Archive -Path $dir.FullName -DestinationPath "$outputFolder\$year\$month.zip"
    }
    
  • #99091

    Matt Bloomfield
    Participant

    😳 that's what happens when you don't read the post titles and just browse for threads without replies.

    Had to wait an extra 20 minutes to post it too because I got my password wrong too many times 🙂

You must be logged in to reply to this topic.