$OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = "SilentlyContinue"
Start-Transcript -Path "c:\Users\DYB\Desktop\ADB\cleanup_log.txt" -Force | Out-Null

function Get-FolderSize($path) {
    if (Test-Path $path) {
        return (Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
    }
    return 0
}

$before = (Get-PSDrive C).Free
Write-Host ("=== Before cleanup, C drive free: " + [math]::Round($before/1GB,2) + " GB ===")
Write-Host ""

# 1. Clear Recycle Bin
Write-Host "[1/6] Clearing Recycle Bin..."
try {
    Clear-RecycleBin -Force -ErrorAction Stop
    Write-Host "  Recycle Bin cleared."
} catch {
    Write-Host ("  Clear-RecycleBin failed, trying manual delete: " + $_.Exception.Message)
    Get-ChildItem -Path "C:\`$Recycle.Bin" -Force -ErrorAction SilentlyContinue | ForEach-Object {
        Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
    }
}
Write-Host ""

# 2. Clear System Temp (C:\Windows\Temp)
Write-Host "[2/6] Clearing System Temp (C:\Windows\Temp)..."
$sysTempPath = "C:\Windows\Temp"
$sysTempBefore = Get-FolderSize $sysTempPath
Get-ChildItem -Path $sysTempPath -Force -ErrorAction SilentlyContinue | ForEach-Object {
    Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host ("  Cleared. Freed: " + [math]::Round($sysTempBefore/1MB,2) + " MB")
Write-Host ""

# 3. Clear User Temp (%TEMP%)
Write-Host "[3/6] Clearing User Temp ($env:TEMP)..."
$userTempPath = $env:TEMP
$userTempBefore = Get-FolderSize $userTempPath
Get-ChildItem -Path $userTempPath -Force -ErrorAction SilentlyContinue | Where-Object { $_.Name -ne "disk_check.ps1" -and $_.Name -ne "scan_large.ps1" } | ForEach-Object {
    Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Host ("  Cleared. Freed: " + [math]::Round($userTempBefore/1MB,2) + " MB")
Write-Host ""

# 4. Clear CrashDumps
Write-Host "[4/6] Clearing CrashDumps..."
$crashPath = "$env:LOCALAPPDATA\CrashDumps"
$crashBefore = Get-FolderSize $crashPath
if (Test-Path $crashPath) {
    Get-ChildItem -Path $crashPath -Force -ErrorAction SilentlyContinue | ForEach-Object {
        Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
    }
}
Write-Host ("  Cleared. Freed: " + [math]::Round($crashBefore/1MB,2) + " MB")
Write-Host ""

# 5. Clear Thumbnail cache
Write-Host "[5/6] Clearing Thumbnail cache..."
$thumbPath = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer"
$thumbBefore = (Get-ChildItem -Path $thumbPath -Filter "thumbcache*" -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
Get-ChildItem -Path $thumbPath -Filter "thumbcache*" -Force -ErrorAction SilentlyContinue | ForEach-Object {
    Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue
}
Write-Host ("  Cleared. Freed: " + [math]::Round($thumbBefore/1MB,2) + " MB")
Write-Host ""

# 6. Clear INetCache
Write-Host "[6/6] Clearing IE/Edge INetCache..."
$inetPath = "$env:LOCALAPPDATA\Microsoft\Windows\INetCache"
$inetBefore = Get-FolderSize $inetPath
if (Test-Path $inetPath) {
    Get-ChildItem -Path $inetPath -Force -ErrorAction SilentlyContinue | ForEach-Object {
        Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
    }
}
Write-Host ("  Cleared. Freed: " + [math]::Round($inetBefore/1MB,2) + " MB")
Write-Host ""

$after = (Get-PSDrive C).Free
Write-Host ("=== After cleanup, C drive free: " + [math]::Round($after/1GB,2) + " GB ===")
Write-Host ("=== Total freed: " + [math]::Round(($after-$before)/1GB,2) + " GB ===")
Stop-Transcript | Out-Null
