Symptoms
You have folder used to capture processed files, for instance when loaded successfully by ABBYY FlexiCapture. Over time this folder, if unmanaged, will grow.
- Excessive accumulation of old folders/ files causing disk space issues.
- Degraded system performance due to too many files/folders.
Cause
n/a
Resolution
Implement a PowerShell script that:
- Identifies subfolders older than a defined threshold (e.g., 30 days).
- Deletes these subfolders and logs the deletion details.
Script Details
Script File Name: CleanupProcessedFolders.ps1
# Define the root folders and the age threshold $rootFolders = @("\\localhost\Hotfolders\NDPP2\Processed", "C:\wfd\Data\FileStorage") $nDaysAgo = 30 # Determine the script path and create the log file path $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition $logFile = Join-Path $scriptPath "Log.txt" foreach ($rootFolder in $rootFolders) { # Calculate the cutoff date for folder deletion $cutOffDate = (Get-Date).AddDays(-$nDaysAgo) # Identify subfolders older than the cutoff date $subFoldersToDelete = Get-ChildItem $rootFolder -Directory | Where-Object { $_.CreationTime -lt $cutOffDate } $foldersDeleted = 0 $filesDeleted = 0 $errors = @() # Process each subfolder for deletion foreach ($subFolder in $subFoldersToDelete) { try { $filesDeletedBefore = (Get-ChildItem $subFolder.FullName -File -Recurse).Count Remove-Item $subFolder.FullName -Recurse -Force $foldersDeleted++ $filesDeleted += $filesDeletedBefore } catch { $errors += $_.Exception.Message } } # Log the deletion activity $logEntry = "$(Get-Date) - $foldersDeleted folders older than $nDaysAgo days deleted from $rootFolder. Files deleted: $filesDeleted." if ($errors.Count -gt 0) { $logEntry += " Errors: $($errors -join ';')" } Add-Content -Path $logFile -Value $logEntry Write-Host $logEntry }
Steps:
Save Your Script:
- File Name (suggested): CleanupProcessedFolders.ps1
- Location: Place it in a dedicated folder which will also store logs, e.g., C:\WFD\Housekeeping\CleanupProcessedFolders\
Create a Scheduled Task:
General Tab, Security Options:
- Select "Run whether user is logged on or not".
- Check "Run with highest privileges" (if required).
Actions Tab:
- Action: Start a program.
- Program/script: Enter "powershell.exe"
- Add arguments (optional)
-NoProfile -ExecutionPolicy Bypass -File "C:\WFD\Housekeeping\CleanupProcessedFolders\ CleanupProcessedFolders.ps1"
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article