From 7aff4198d961351f124598545a4b571b3d6b48ed Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Tue, 20 Jun 2023 14:50:43 +0200 Subject: [PATCH] Update remove-old-dirs.ps1 --- Scripts/remove-old-dirs.ps1 | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Scripts/remove-old-dirs.ps1 b/Scripts/remove-old-dirs.ps1 index 9ce9c12e..a17ef2e6 100644 --- a/Scripts/remove-old-dirs.ps1 +++ b/Scripts/remove-old-dirs.ps1 @@ -1,10 +1,10 @@ -<# +<# .SYNOPSIS Removes old directories .DESCRIPTION - This PowerShell script removes any directory in a given folder older than (using last write time). + This PowerShell script removes any subfolder in a parent folder older than (using last write time). .PARAMETER path - Specifies the file path to the folder + Specifies the file path to the parent folder .PARAMETER numDays Specifies the number of days (1000 by default) .EXAMPLE @@ -17,30 +17,29 @@ param([string]$path = "", [int]$numDays = 1000) try { $stopWatch = [system.diagnostics.stopwatch]::startNew() - if ("$path" -eq "") { $path = Read-Host "Enter the file path to the directory" } + if ("$path" -eq "") { $path = Read-Host "Enter the file path to the parent folder" } if (!(Test-Path -Path "$path" -PathType container)) { throw "Given path doesn't exist - enter a valid path, please" } - Write-Progress "Removing old directories in $path..." - $folders = Get-ChildItem -Path "$path" -Directory + Write-Host "⏳ Removing subfolders older than $numDays days in $path..." + $folders = Get-ChildItem -path "$path" -directory $numRemoved = 0 $count = 0 foreach ($folder in $folders) { [datetime]$folderDate = ($folder | Get-ItemProperty -Name LastWriteTime).LastWriteTime $count++ if ($folderDate -lt (Get-Date).AddDays(-$numDays)) { - Write-Host "#$($count): Removing old '$folder'..." - $fullPath = $folder | Select-Object -ExpandProperty FullName - Remove-Item -Path $fullPath -Force -Recurse + Write-Host "($($count)) Removing old '$folder'..." + $fullPath = $folder | Select-Object -ExpandProperty FullName + Remove-Item -path "$fullPath" -force -recurse $numRemoved++ - } else { - Write-Host "#$($count): Skipping young '$folder'..." + } else { + Write-Host "($($count)) Skipping young '$folder'..." } - } - Write-Progress -completed "." + } [int]$elapsed = $stopWatch.Elapsed.TotalSeconds - Write-Host "Removed $numRemoved of $count directories older than $numDays days at $path in $elapsed sec" + "✔️ Removed $numRemoved of $count subfolders older than $numDays days in $elapsed sec" exit 0 # success } catch { - Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" - exit 1 # failure + "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" + exit 1 }