Author Posts

April 17, 2018 at 7:44 am

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

April 17, 2018 at 9:37 am

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

April 17, 2018 at 9:50 am

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

April 17, 2018 at 12:26 pm

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