From 5038877cd7302ac25c35e65414cb26fc14b986ff Mon Sep 17 00:00:00 2001 From: Joeri Date: Sat, 7 Feb 2026 12:19:16 +0100 Subject: [PATCH] - --- cleanup.ps1 | 125 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 19 deletions(-) diff --git a/cleanup.ps1 b/cleanup.ps1 index dfedf95..b7dc1d3 100644 --- a/cleanup.ps1 +++ b/cleanup.ps1 @@ -500,27 +500,107 @@ try { Write-Log "Warning: Could not stop Edge processes: $($_.Exception.Message)" -Level "WARNING" } -# Check for installed BleachBit -Write-Log "Checking for BleachBit installation..." -Level "INFO" -$BleachBitExe = $null +# Download BleachBit Portable +Write-Log "Downloading BleachBit Portable..." -Level "INFO" +$BleachBitZip = "C:\Quest\BleachBitPortable.zip" +$BleachBitUrl = "https://www.bleachbit.org/download/file/t?file=BleachBit-5.0.2-portable.zip" +$MinFileSizeBytes = 10000000 # Minimum 10MB -# Check common installation paths -$PossiblePaths = @( - "C:\Program Files\BleachBit\bleachbit_console.exe", - "C:\Program Files (x86)\BleachBit\bleachbit_console.exe", - "C:\Quest\BleachBit-Portable\bleachbit_console.exe" -) +try { + # Remove old zip file if exists + if (Test-Path $BleachBitZip) { + Remove-Item $BleachBitZip -Force -ErrorAction Stop + } + + # Download with retry logic + $DownloadSuccess = $false + $RetryCount = 0 + $MaxRetries = 3 + + while (-not $DownloadSuccess -and $RetryCount -lt $MaxRetries) { + $RetryCount++ + Write-Log "Download attempt $RetryCount of $MaxRetries..." -Level "INFO" + + try { + $WebClient = New-Object System.Net.WebClient + $WebClient.DownloadFile($BleachBitUrl, $BleachBitZip) + $DownloadSuccess = $true + Write-Log "Download completed." -Level "INFO" + } catch { + Write-Log "Download attempt failed: $($_.Exception.Message)" -Level "WARNING" + Start-Sleep -Seconds 5 + } + } + + if (-not $DownloadSuccess) { + throw "Failed to download after $MaxRetries attempts" + } + + # Verify file size + $FileSize = (Get-Item $BleachBitZip).Length + Write-Log "Downloaded file size: $($FileSize / 1MB) MB" -Level "INFO" + + if ($FileSize -lt $MinFileSizeBytes) { + throw "Downloaded file is too small ($($FileSize / 1MB) MB). Expected at least $($MinFileSizeBytes / 1MB) MB." + } + + $ScriptResult.bleachbit_cleanup.downloaded = $true + Write-Log "BleachBit Portable downloaded successfully." -Level "INFO" +} catch { + $ErrorMsg = "Failed to download BleachBit Portable: $($_.Exception.Message)" + $ScriptResult.bleachbit_cleanup.errors += $ErrorMsg + Write-Log $ErrorMsg -Level "ERROR" +} -foreach ($Path in $PossiblePaths) { - if (Test-Path $Path) { - $BleachBitExe = $Path - Write-Log "Found BleachBit at: $BleachBitExe" -Level "INFO" - break +# Extract BleachBit Portable +if ($ScriptResult.bleachbit_cleanup.downloaded) { + Write-Log "Extracting BleachBit Portable..." -Level "INFO" + $BleachBitDest = "C:\Quest" + + try { + # Remove old extraction folder if exists + $OldBleachBitPath = Join-Path $BleachBitDest "BleachBit-Portable" + if (Test-Path $OldBleachBitPath) { + Remove-Item $OldBleachBitPath -Recurse -Force -ErrorAction Stop + } + + # Extract using Expand-Archive + Expand-Archive -Path $BleachBitZip -DestinationPath $BleachBitDest -Force -ErrorAction Stop + $ScriptResult.bleachbit_cleanup.extracted = $true + Write-Log "BleachBit Portable extracted successfully." -Level "INFO" + + # Remove zip file + Remove-Item $BleachBitZip -Force -ErrorAction SilentlyContinue + Write-Log "Removed temporary zip file." -Level "INFO" + } catch { + $ErrorMsg = "Failed to extract BleachBit Portable: $($_.Exception.Message)" + $ScriptResult.bleachbit_cleanup.errors += $ErrorMsg + Write-Log $ErrorMsg -Level "ERROR" + + # Try alternative extraction using shell + try { + Write-Log "Trying alternative extraction method..." -Level "WARNING" + + $shell = New-Object -ComObject Shell.Application + $zip = $shell.NameSpace($BleachBitZip) + $dest = $shell.NameSpace($BleachBitDest) + $dest.CopyHere($zip.items(), 16) + + Start-Sleep -Seconds 5 + $ScriptResult.bleachbit_cleanup.extracted = $true + Write-Log "BleachBit Portable extracted using shell method." -Level "INFO" + + # Remove zip file + Remove-Item $BleachBitZip -Force -ErrorAction SilentlyContinue + } catch { + Write-Log "Alternative extraction also failed: $($_.Exception.Message)" -Level "ERROR" + } } } -# Run BleachBit cleanup if found -if ($BleachBitExe) { +# Run BleachBit cleanup +$BleachBitExe = "C:\Quest\BleachBit-Portable\bleachbit_console.exe" +if (Test-Path $BleachBitExe) { Write-Log "Running BleachBit cleanup with $($BleachBitCleaners.Count) cleaners..." -Level "INFO" try { @@ -531,8 +611,16 @@ if ($BleachBitExe) { $BleachBitProcess = Start-Process -FilePath $BleachBitExe -ArgumentList "--clean $CleanerArgs" -NoNewWindow -Wait -PassThru -ErrorAction Stop $ScriptResult.bleachbit_cleanup.cleaners_run = $BleachBitCleaners.Count - $ScriptResult.bleachbit_cleanup.extracted = $true Write-Log "BleachBit cleanup completed. Exit code: $($BleachBitProcess.ExitCode)" -Level "INFO" + + # Clean up BleachBit files after use + try { + Write-Log "Cleaning up BleachBit Portable files..." -Level "INFO" + Remove-Item "C:\Quest\BleachBit-Portable" -Recurse -Force -ErrorAction SilentlyContinue + Write-Log "BleachBit Portable files removed." -Level "INFO" + } catch { + Write-Log "Warning: Could not remove BleachBit files: $($_.Exception.Message)" -Level "WARNING" + } } catch { $ErrorMsg = "Failed to run BleachBit cleanup: $($_.Exception.Message)" $ScriptResult.bleachbit_cleanup.errors += $ErrorMsg @@ -540,8 +628,7 @@ if ($BleachBitExe) { Write-Log $ErrorMsg -Level "ERROR" } } else { - Write-Log "BleachBit not found. Skipping cleanup." -Level "WARNING" - Write-Log "Install BleachBit to enable automatic cleanup." -Level "INFO" + Write-Log "BleachBit executable not found at: $BleachBitExe" -Level "ERROR" } Write-Log "BleachBit cleanup section completed." -Level "INFO"