Find folders and zip them

Welcome Forums General PowerShell Q&A Find folders and zip them

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

js
 
Participant
9 months ago.

  • Author
    Posts
  • #99001

    Participant
    Points: 0
    Rank: Member

    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! =)
    PPS. I'm sorry if this is a double post I feel something went wrong during editting..

  • #99006

    Participant
    Points: 413
    Helping Hand
    Rank: Contributor

    As a starting point you could use something similar to this:

    $startPath = 'G:\Stuff\Logs\Product\Client'
    Get-ChildItem -Path $startPath |
        ForEach-Object {
            $YearPath = $_.FullName
            Get-ChildItem -Path $_.FullName |
                ForEach-Object {
                    $Month = $_.BaseName
                    Get-ChildItem -Path $_.FullName | 
                        Compress-Archive -DestinationPath (Join-Path -Path $YearPath -ChildPath ($Month + '.zip'))
                }
        }

    You just have to specify the start folder where the folders with the years as names are. The zip files will be stored besides the "month folders" in the "year folders". 😉

  • #99007

    Participant
    Points: 0
    Rank: Member

    Wow.. Super! Thanks! =D
    I will get to work with that! =D

  • #99021
    js

    Participant
    Points: 423
    Helping Hand
    Rank: Contributor

    That cmdlet isn't that great. I would use the command line version of 7zip.

The topic ‘Find folders and zip them’ is closed to new replies.