none
compress folders by months RRS feed

  • General discussion

  • Hi

    I would need an idea like making a script.
    I need to compress folders with a multitude of subfolders and files maintaining the structure but always leaving the last 3 months intact.

    I've started doing this script, but it does not show me results and I do not know why.

    What advice do you send me to make a script for this purpose?

    clear-host
    $Source="D:\Folder"
    $date = Get-Date
    $currentmonth = ($date.ToString("MM/yy"))
    $previousmonth = ($date.AddMonths(-3).ToString("MM/yy"))
    $firstdayofpreviousmonth = Get-Date "01/$previousmonth"
    Get-ChildItem -Path "$($Source)\*.*" -Recurse | Where {$_.LastWriteTime -lt $firstdayofpreviousmonth -and $_.PSIsContainer -eq $false}

    Thanks!

    • Changed type Bill_Stewart Tuesday, November 7, 2017 9:31 PM
    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:32 PM Help vampire
    Thursday, September 7, 2017 3:01 PM

All replies

  • Here's one way to get a DateTime object containing the first day of the previous month:


    PS C:\> ([DateTime]::Today).AddDays(-([DateTime]::Today.Day)+1).AddMonths(-1)
    Tuesday, August 01, 2017 12:00:00 AM
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 7, 2017 3:27 PM
  • Hi ThorElPoderoso

    In the script are you looking for files or folders. Above you speicify you want to copress folders but in the script you have the PSIsContainer set to false that should be set to true for folders and false for files. 


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, September 7, 2017 3:28 PM
  • But how do I specify that I want to compress folders and their content for a specific month?
    Sunday, September 10, 2017 5:35 PM
  • I want to compress folders and files, all content
    Sunday, September 10, 2017 5:35 PM
  • I want to compress folders and files, all content

    compress-archive -Path c:\test7 -DestinationPath c:\test7.zip

    help compress-archive -full


    \_(ツ)_/

    Sunday, September 10, 2017 7:36 PM
  • Here is one I have used to zip files/folders older that 7 days to a different location then delete the original files.  You could modify the $limit with Bills suggestion 

    ([DateTime]::Today).AddDays(-([DateTime]::Today.Day)+1).AddMonths(-2)

    which would set $limit to the first of the month 2 months ago

    $a = Get-Date -format "ddMMyyyy"
    $limit = (get-date).AddDays(-7)
    $srcdir = "c:\test"
    $name=[System.IO.Path]::GetFileName($srcdir)
    $zipFilename = $name + $a + ".zip"
    $zipFilepath = "c:\temp\"
    $zipFile = "$zipFilepath$zipFilename"
    
    #Prepare zip file
    if(-not (test-path($zipFile))) {
        set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
        (dir $zipFile).IsReadOnly = $false  
    }
    
    $shellApplication = new-object -com shell.application
    $zipPackage = $shellApplication.NameSpace($zipFile)
    $files = Get-ChildItem -Path $srcdir | where{! $_.PSIsContainer -and $_.CreationTime -lt $limit}
    
    foreach($file in $files) { 
        $zipPackage.CopyHere($file.FullName)
    #using this method, sometimes files can be 'skipped'
    #this 'while' loop checks each file is added before moving to the next
        while($zipPackage.Items().Item($file.name) -eq $null){
            Start-sleep -seconds 1
        }
    }
    
    Get-ChildItem -Path $srcdir -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
    

    Monday, September 11, 2017 7:51 AM