79134482

Date: 2024-10-28 17:27:35
Score: 5
Natty:
Report link

thanks to your advices, I have done a recursive function. here it is, I know it's not perfect, but if someone gets to have the same problem, here what worked for me

function Recursive-FolderSearch {
param (
    [string[]]$Paths,  
    [string]$expiredDate  
)

foreach ($Path in $Paths) {
    
    if ((Test-Path $Path -PathType Container) -and (Split-Path $Path -Leaf) -match '^\d{4}-\d{2}$') {
        
        $folderDays = Get-ChildItem -Path $Path -Attributes Directory
        foreach ($folderDay in $folderDays) {
            if ($folderDay.Name -match '^\d{2}$') {# if folder respect format DD
                # concat folder AAAA-MM and folder format DD to get complete date
                $dateDossier = [datetime]::ParseExact("$(Split-Path $Path -Leaf)-$($folderDay.Name)", "yyyy-MM-dd", $null)
                if ($dateDossier -lt $expiredDate) { #compare folder date with actual date 
                # then suppress folder if older than requested
                    Remove-Item -Path $folderDay.FullName -Recurse -Force -ErrorAction SilentlyContinue
                }
            }
        }
        #Supprimer le dossier AAAA-MM s'il est vide
        if (-not (Get-ChildItem -Path $Path -Force -Recurse -ErrorAction SilentlyContinue  )) { #| Where-Object { $_. PSIsContainer -eq $true }
            Remove-Item -Path $Path -Force -Recurse -ErrorAction SilentlyContinue
        }
    
    } 
    elseif (Test-Path $Path -PathType Container) {
        $subDirs = Get-ChildItem -Path $Path -Attributes Directory -Force

        foreach ($subDir in $subDirs) {
            Recursive-FolderSearch -Paths $subDir.FullName $expiredDate
        }
    } 
}

}

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Whitelisted phrase (-1): worked for me
  • RegEx Blacklisted phrase (3): thanks to your advices
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same problem
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: jajajf