Загрузка данных


# ─── Конфиг ───────────────────────────────────────────────────────────────────
$DownloadsPath = [Environment]::GetFolderPath("UserProfile") + "\Downloads"
$LogFile       = "$env:TEMP\clean-downloads.log"
# ──────────────────────────────────────────────────────────────────────────────

function Write-Log {
    param([string]$Message, [string]$Level = "INFO")
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $line = "[$timestamp] [$Level] $Message"
    Add-Content -Path $LogFile -Value $line
    switch ($Level) {
        "INFO"  { Write-Host $line -ForegroundColor Cyan }
        "ERROR" { Write-Host $line -ForegroundColor Red }
        "OK"    { Write-Host $line -ForegroundColor Green }
    }
}

Write-Log "=== Запуск очистки Downloads ==="
Write-Log "Папка: $DownloadsPath"

if (-not (Test-Path $DownloadsPath)) {
    Write-Log "Папка не найдена: $DownloadsPath" "ERROR"
    exit 1
}

$deletedCount = 0
$deletedSize  = 0
$errorCount   = 0

$items = Get-ChildItem -Path $DownloadsPath -Force -ErrorAction SilentlyContinue

foreach ($item in $items) {
    try {
        if ($item.PSIsContainer) {
            $size = (Get-ChildItem $item.FullName -Recurse -File -Force -ErrorAction SilentlyContinue |
                     Measure-Object -Property Length -Sum).Sum
        } else {
            $size = $item.Length
        }

        Remove-Item -Path $item.FullName -Force -Recurse -ErrorAction Stop
        Write-Log "Удалено: $($item.FullName) ($([math]::Round($size/1KB, 1)) KB)" "OK"
        $deletedCount++
        $deletedSize += $size
    } catch {
        Write-Log "Ошибка: $($item.FullName) — $_" "ERROR"
        $errorCount++
    }
}

$sizeMB = [math]::Round($deletedSize / 1MB, 2)
Write-Log "=== Готово: удалено $deletedCount объектов, освобождено $sizeMB MB, ошибок: $errorCount ==="

exit 0