@echo off
chcp 65001 >nul
title Advanced System Cleaner for Gaming & Privacy
color 0b

:: =================================================================================================
::  Advanced System Cleaner Script
::  Created by: Rezx
::  Date: 2026-01-30
::  Description: Cleans temporary files, logs, cache, and registry traces to optimize system and privacy.
::  WARNING: Run as Administrator. Use at your own risk.
:: =================================================================================================

:: --- 1. Admin Rights Check ---
:AdminCheck
openfiles >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo [ERROR] SCRIPT MUST BE RUN AS ADMINISTRATOR!
    echo [INFO] Right-click the script and select "Run as administrator".
    echo.
    pause
    exit
)

echo.
echo ========================================================
echo       STARTING SYSTEM CLEANUP PROCESS
echo ========================================================
echo.

:: --- 2. Stop Conflicting Processes ---
echo [STEP 1/7] Stopping browser and background processes...
taskkill /F /IM chrome.exe /T >nul 2>&1
taskkill /F /IM firefox.exe /T >nul 2>&1
taskkill /F /IM msedge.exe /T >nul 2>&1
taskkill /F /IM brave.exe /T >nul 2>&1
taskkill /F /IM discord.exe /T >nul 2>&1
taskkill /F /IM steam.exe /T >nul 2>&1
echo [OK] Processes stopped.

:: --- 3. Clean Temporary Files ---
echo.
echo [STEP 2/7] Cleaning Temporary Files...

:: User Temp
echo  - Cleaning User Temp (%temp%)
del /s /f /q "%temp%\*.*" >nul 2>&1
rmdir /s /q "%temp%" >nul 2>&1
mkdir "%temp%" >nul 2>&1

:: Windows Temp
echo  - Cleaning Windows Temp (C:\Windows\Temp)
del /s /f /q "C:\Windows\Temp\*.*" >nul 2>&1
rmdir /s /q "C:\Windows\Temp" >nul 2>&1
mkdir "C:\Windows\Temp" >nul 2>&1

:: Prefetch
echo  - Cleaning Prefetch (C:\Windows\Prefetch)
del /s /f /q "C:\Windows\Prefetch\*.*" >nul 2>&1

:: Downloads (Warning Only - Skipping auto-delete for safety)
echo  - [SKIP] Downloads folder skipped for safety. Please manually check "C:\Users\%username%\Downloads" for suspicious executables.

echo [OK] Temporary files cleaned.

:: --- 4. Clean Browser Caches ---
echo.
echo [STEP 3/7] Cleaning Browser Caches...

:: Chrome
if exist "%localappdata%\Google\Chrome\User Data\Default\Cache" (
    echo  - Cleaning Chrome Cache
    del /s /f /q "%localappdata%\Google\Chrome\User Data\Default\Cache\*.*" >nul 2>&1
    rmdir /s /q "%localappdata%\Google\Chrome\User Data\Default\Cache" >nul 2>&1
)

:: Edge
if exist "%localappdata%\Microsoft\Edge\User Data\Default\Cache" (
    echo  - Cleaning Edge Cache
    del /s /f /q "%localappdata%\Microsoft\Edge\User Data\Default\Cache\*.*" >nul 2>&1
    rmdir /s /q "%localappdata%\Microsoft\Edge\User Data\Default\Cache" >nul 2>&1
)

:: Firefox
if exist "%localappdata%\Mozilla\Firefox\Profiles" (
    echo  - Cleaning Firefox Cache entries
    for /d %%x in ("%localappdata%\Mozilla\Firefox\Profiles\*") do (
        del /s /f /q "%%x\cache2\*.*" >nul 2>&1
        rmdir /s /q "%%x\cache2" >nul 2>&1
    )
)

echo [OK] Browser caches cleaned.

:: --- 5. Clean Registry Traces ---
echo.
echo [STEP 4/7] Cleaning Registry Traces (UserAssist, MuiCache, CompatFlags)...

:: UserAssist (Rot13 encoded history of executed programs)
echo  - Clearing UserAssist
reg delete "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist" /f >nul 2>&1
:: Re-create keys maybe needed for explorer stability, usually Windows rebuilds them.

:: MuiCache (Stores file locations of programs)
echo  - Clearing MuiCache
reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\Shell\MuiCache" /f >nul 2>&1
reg delete "HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache" /f >nul 2>&1

:: AppCompatFlags (Compatibility Assistant Store) - GRANULAR CLEANING
echo  - smart Filtering AppCompatFlags... (Deleting only suspicious entries)

:: Create temporary PowerShell script for filtering
set "PSFile=%temp%\RegClean.ps1"
echo $path = "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store" > "%PSFile%"
echo if (Test-Path $path) { >> "%PSFile%"
echo     $entries = Get-ItemProperty -Path $path ^| Get-Member -MemberType NoteProperty >> "%PSFile%"
echo     foreach ($entry in $entries) { >> "%PSFile%"
echo         $name = $entry.Name >> "%PSFile%"
echo         $suspicious = $false >> "%PSFile%"
echo         # Define suspicious criteria >> "%PSFile%"
echo         if ($name -match "\\Desktop\\" -or $name -match "\\Downloads\\" -or $name -match "\\Temp\\" -or $name -match "AppData\\Local\\Temp") { >> "%PSFile%"
echo             $suspicious = $true >> "%PSFile%"
echo         } >> "%PSFile%"
echo         elseif ($name -match "\.(exe|dll|sys)$") { >> "%PSFile%"
echo             if ($name -notmatch "C:\\Windows" -and $name -notmatch "C:\\Program Files" -and $name -notmatch "OneDrive" -and $name -notmatch "Microsoft\\Edge") { >> "%PSFile%"
echo                 $suspicious = $true >> "%PSFile%"
echo             } >> "%PSFile%"
echo         } >> "%PSFile%"
echo         if ($suspicious) { >> "%PSFile%"
echo             Write-Host " [DELETING] Suspicious: $name" -ForegroundColor Red >> "%PSFile%"
echo             Remove-ItemProperty -Path $path -Name $name -ErrorAction SilentlyContinue >> "%PSFile%"
echo         } >> "%PSFile%"
echo     } >> "%PSFile%"
echo } >> "%PSFile%"

:: Run the PowerShell script
powershell -ExecutionPolicy Bypass -File "%PSFile%"

:: Delete the clean-up script
del "%PSFile%" >nul 2>&1

:: Background Activity Moderator (BAM) - Requires SYSTEM rights usually, trying anyway
echo  - Attempting to clear BAM (Background Activity Moderator) traces...
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\bam\State\UserSettings\%USERDOMAIN%\%USERNAME%" /f >nul 2>&1

echo [OK] Registry traces cleaned.

:: --- 6. Clean Logs & Event Viewer ---
echo.
echo [STEP 5/7] Cleaning System Logs & Event Viewer...

echo  - Clearing All Event Logs (This may take a moment)
for /F "tokens=*" %%1 in ('wevtutil.exe el') DO wevtutil.exe cl "%%1" >nul 2>&1

echo  - Cleaning Windows Update Logs
del /s /f /q "C:\Windows\SoftwareDistribution\DataStore\Logs\*.*" >nul 2>&1

echo  - Cleaning CBS Logs
del /s /f /q "C:\Windows\Logs\CBS\*.*" >nul 2>&1

echo [OK] Logs cleaned.

:: --- 7. Network & DNS ---
echo.
echo [STEP 6/7] Resetting Network & DNS...
ipconfig /flushdns >nul
echo [OK] DNS Cache flushed.

:: --- 8. Shadow Copies & Drivers ---
echo.
echo [STEP 7/7] Advanced Cleanup (Shadow Copies & Drivers)...

:: Shadow Copies (Deletes all restore points - Caution!)
echo  - Deleting old Shadow Copies...
vssadmin delete shadows /All /Quiet >nul 2>&1

:: Driver Cache (Not deleting actual drivers, only cache/installers)
echo  - Cleaning DirectX Shader Cache
del /s /f /q "%localappdata%\D3DSCache\*.*" >nul 2>&1
del /s /f /q "%localappdata%\NVIDIA\GLCache\*.*" >nul 2>&1
del /s /f /q "%localappdata%\AMD\DxCache\*.*" >nul 2>&1

echo.
echo ========================================================
echo    CLEANUP COMPLETE! 
echo ========================================================
echo.
echo Please restart your computer to ensure all changes take effect properly.
echo.
pause
exit
