Search code examples
powershellperformance

what is the best way to in powershell for finding only folder in term of time performance


I have an issue, where I have to purge folders, the name of the folders are most of the time formated and I did a script in powershell to do that. But I understood that even if I do : Get-ChildItem -Path $array[2] -Recurse -Directory the command will check every item in every folder even if that is not a folder, and that is leading to some hours to get every folder path.

how should I do to permit that to perform in term of time ?

here is a part of the script

foreach ($line in Get-Content ".\Exe-Purge-directories.txt") {

    $array = $line.Split("|")
    $deleteType = $array[0]
    $sdate = [int]$array[1] * -1 # nb de jour a retrancher ( nb negatif)
    $expiredDate = (Get-Date).AddDays($sdate) # today + date a retrancher 
    $folders = Get-ChildItem -Path $array[2] -Recurse -Directory -Force -ErrorAction SilentlyContinue
    #the line above is the one taking time  
    $pathException = Get-ChildItem -Path $array[2] -File #-Force #-ErrorAction SilentlyContinue
    
    if ($deleteType.Equals("DateTimeInFolderName")) {
        foreach ($folder in $folders) {
                     if ($folder.Name -match '^\d{4}-\d{2}$')
            # .....

Solution

  • 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 as my folders name have formatted name

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

    }