Bio

# Define the source and destination folders
$sourceFolder = "D:\Ravinder"
$destinationFolder = "\\Wrdne003xspwfe\d$\Ravinder"
$logFilePath = "D:\Ravinder\report.log"
$pattern = "\d{6}"

# Calculate the date for one week ago
$oneWeekAgo = (Get-Date).AddDays(-7)

# Get folders created a week ago or more
$oldFolders = Get-ChildItem -Path $sourceFolder -Directory | Where-Object {$_.Name -match $pattern}

# Print found folders
Write-Host "`nBelow folders found for archival:`n" -ForegroundColor Cyan
foreach ($folder in $oldFolders) {
Write-Host "$($folder.Name)" -BackgroundColor White -ForegroundColor DarkMagenta
}

# Prompt for the number of threads
$threadCount = Read-Host "Enter the number of threads to use for compression and moving"

# Function to compress and move folders
function CompressAndMoveFolder {
param (
[string]$folderName
)

$compressedFilePath = Join-Path -Path $sourceFolder -ChildPath "$folderName.zip"

try {
# Compress the folder using .NET ZipFile class
Write-Host "`nCompressing the folder $($folderName)" -ForegroundColor Cyan
[System.IO.Compression.ZipFile]::CreateFromDirectory($folderName, $compressedFilePath)
Write-Host "`nDone Compressing the folder $($folderName)" -ForegroundColor Green

try {
# Use Robocopy to move the compressed folder to the destination
Write-Host "`nMoving the folder $($folderName.zip) to $($destinationFolder)" -ForegroundColor Cyan
robocopy $sourceFolder $destinationFolder "$folderName.zip" /MOVE /Z /R:3 /W:2
Write-Host "`nFolder $($folderName.zip) has been moved successfully to destination folder: $($destinationFolder)" -ForegroundColor Green

try {
# Remove the original folder
Write-Host "`nRemoving the $($folderName) from $($sourceFolder)" -ForegroundColor Cyan
Remove-Item -Path $folderName -Recurse -Force
Log "`nFolder '$folderName' compressed, moved, and deleted successfully."
Write-Host "`nFolder '$folderName' compressed, moved, and deleted successfully." -ForegroundColor Green

}
catch {
Log "Error deleting folder '$folderName': $_"
}
}
catch {
Log "Error moving folder '$folderName': $_"
}
}
catch {
Log "Error compressing folder '$folderName': $_"
}
}

# Loop through each old folder and compress/move them concurrently
foreach ($folder in $oldFolders) {
# Start a job for each folder
$job = Start-Job -ScriptBlock { param($folderName) CompressAndMoveFolder $folderName } -ArgumentList $folder.FullName
}

# Wait for all jobs to finish
$completedJobs = 0
do {
$completedJobs = (Get-Job -State Completed).Count
Write-Host "Waiting for compression and moving to complete... ($completedJobs/$($oldFolders.Count) completed)" -ForegroundColor Yellow
Start-Sleep -Seconds 5
} while ($completedJobs -lt $oldFolders.Count)

# Remove completed jobs
Get-Job | Remove-Job

Write-Host "`nWeekly cleanup complete." -ForegroundColor Green
Read-Host "`nPress Enter to exit"

# Log function
function Log {
param(
[string]$Message
)

$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp - $Message"
Add-Content -Path $logFilePath -Value $logMessage
}

Recent Posts     Most popular