FocusedWolf

Windows 10: Installer Cleaner.bat

Feb 21st, 2022 (edited)
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.80 KB | None | 0 0
  1. @echo off
  2. REM Version 9
  3.  
  4. title Installer Cleaner
  5. color 05
  6.  
  7. REM SOURCE: https://ourrick.wordpress.com/2015/04/04/find-orphaned-installer-files-in-cwindowsinstaller-folder/    <-- NOTE: There is some major bugs in that script!
  8. REM Where i first learned of the script: https://sourceforge.net/p/patchcleaner/discussion/general/thread/73e4f56a/?limit=25
  9.  
  10. :: http://www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur
  11. setlocal enableextensions enabledelayedexpansion
  12. cd /d "%~dp0"
  13.  
  14. if /i "%~1" == "nopause" (
  15.     set NOPAUSE=1
  16. ) else (
  17.     set NOPAUSE=0
  18. )
  19.  
  20. if /i "%~2" == "purge" (
  21.     set PURGE=1
  22. ) else (
  23.     set PURGE=0
  24. )
  25.  
  26. set LOG_DIRECTORY=%~dp0Logs\
  27.  
  28. set FAILURE_TO_DELETE=0
  29. set MISSING_INSTALLER=0
  30.  
  31. :CheckPermissions
  32.     fltmc >nul 2>&1 || (
  33.         echo Requesting administrative privileges...
  34.         start cmd /c powershell.exe -Command "Start-Process cmd -Verb runas -ArgumentList '/c \"\"%~s0\"\" %*'"
  35.         exit /B
  36.     )
  37.  
  38. :Choice
  39.     if %PURGE% equ 1 goto :Work
  40.     :PROMPT_ONE
  41.         cls
  42.         set /p PURGE=Delete orphaned installer files to Recycle Bin or just search for orphans? [0^|1]:
  43.         if /i "%PURGE%" equ "0" goto :Work
  44.         if /i "%PURGE%" equ "1" goto :Work
  45.         goto :PROMPT_ONE
  46.  
  47. :Work
  48.    REM Create intermediate directories for log directory if necessary.
  49.     if not exist "!LOG_DIRECTORY!" (
  50.         mkdir "!LOG_DIRECTORY!" 2>nul
  51.         if not exist "!LOG_DIRECTORY!" (
  52.             echo Directory could not be created: "!LOG_DIRECTORY!"
  53.             exit /b 1
  54.         )
  55.     )
  56.  
  57.     set TRACKED="!LOG_DIRECTORY!INSTALLER CLEANER - Tracked.log"
  58.     if exist %TRACKED% del %TRACKED%
  59.  
  60.     set MISSING="!LOG_DIRECTORY!INSTALLER CLEANER - Missing.log"
  61.     if exist %MISSING% del %MISSING%
  62.  
  63.     set ORPHANED="!LOG_DIRECTORY!INSTALLER CLEANER - Orphaned.log"
  64.     if exist %ORPHANED% del %ORPHANED%
  65.  
  66.     set INSTALLERS="!LOG_DIRECTORY!INSTALLER CLEANER - Installers.log"
  67.     if exist %INSTALLERS% del %INSTALLERS%
  68.  
  69.     echo Caching installer references...
  70.  
  71.     REM This will find both .msi and .msp entries.
  72.     set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
  73.     set VALUE_NAME=LocalPackage
  74.     for /f "usebackq tokens=1-2*" %%a in (`reg query "%KEY_NAME%" /s /v "%VALUE_NAME%" 2^>nul ^| findstr /i "%VALUE_NAME%"`) do (
  75.        REM Append installer path to file.
  76.         echo %%c>>%INSTALLERS%
  77.     )
  78.  
  79.     if /i "%PURGE%" equ "0" (
  80.         echo Looking for orphaned installers...
  81.     ) else (
  82.         echo Looking for orphaned installers to delete...
  83.     )
  84.  
  85.     for /f "delims=" %%a in ('dir /b /s %WINDIR%\Installer\*.msi %WINDIR%\Installer\*.msp') do (
  86.        REM Added /I flag (Specifies that the search is not to be case-sensitive) to fix issue where M$ inconsistently lists installer paths in: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
  87.        REM The LocalPackage key can contain upper and lower-case variations, e.g.
  88.        REM     c:\Windows\Installer\177be02.msp
  89.        REM     C:\Windows\Installer\32dc90.msi
  90.        REM Its bad because a file can be incorrectly classified as orphaned because findstr is case-sensitive by default.
  91.         findstr /i /c:"%%a" %INSTALLERS% >nul
  92.  
  93.         if !errorlevel!==1 (
  94.             echo "%%a" ORPHANED>>%ORPHANED%
  95.             echo "%%a" ORPHANED
  96.  
  97.             if /i "%PURGE%" equ "1" (
  98.                 echo Deleted orphaned installer - %%a
  99.                REM  del "%%a"
  100.                REM
  101.                REM Using recycle.exe [http://www.geocities.ws/fp.westlake/xp/index.html] to move orphaned installer files to recycle bin instead of deleting.
  102.                 recycle /f "%%a"
  103.  
  104.                 if exist "%%a" (
  105.                     set FAILURE_TO_DELETE=1
  106.                 )
  107.             )
  108.         ) else (
  109.             echo "%%a" TRACKED>>%TRACKED%
  110.         )
  111.     )
  112.  
  113.     echo Looking for missing installers...
  114.  
  115.     for /f "delims=" %%a in ('type %INSTALLERS%') do (
  116.         if not exist %%a (
  117.             echo "%%a" MISSING>>%MISSING%
  118.             echo "%%a" MISSING
  119.             set MISSING_INSTALLER=1
  120.         )
  121.     )
  122.  
  123. :End
  124.     if %FAILURE_TO_DELETE% equ 1 pause
  125.     if %MISSING_INSTALLER% equ 1 (
  126.         echo -----
  127.         echo Search this registry key with the installer path to determine its DisplayName:
  128.         echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
  129.         echo Find an installer online with that name and download it.
  130.         echo Your looking for installers that offer a Repair option the first time you run them.
  131.         echo Click Repair, click Browse to find the vc_red.msi file, and click Ok to reinstall.
  132.         echo -----
  133.         pause
  134.     )
  135.    REM if %NOPAUSE% equ 1 goto :eof
  136.     timeout /t 7 /nobreak
  137.     goto :eof
Add Comment
Please, Sign In to add comment