Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- REM Version 9
- title Installer Cleaner
- color 05
- 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!
- REM Where i first learned of the script: https://sourceforge.net/p/patchcleaner/discussion/general/thread/73e4f56a/?limit=25
- :: http://www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur
- setlocal enableextensions enabledelayedexpansion
- cd /d "%~dp0"
- if /i "%~1" == "nopause" (
- set NOPAUSE=1
- ) else (
- set NOPAUSE=0
- )
- if /i "%~2" == "purge" (
- set PURGE=1
- ) else (
- set PURGE=0
- )
- set LOG_DIRECTORY=%~dp0Logs\
- set FAILURE_TO_DELETE=0
- set MISSING_INSTALLER=0
- :CheckPermissions
- fltmc >nul 2>&1 || (
- echo Requesting administrative privileges...
- start cmd /c powershell.exe -Command "Start-Process cmd -Verb runas -ArgumentList '/c \"\"%~s0\"\" %*'"
- exit /B
- )
- :Choice
- if %PURGE% equ 1 goto :Work
- :PROMPT_ONE
- cls
- set /p PURGE=Delete orphaned installer files to Recycle Bin or just search for orphans? [0^|1]:
- if /i "%PURGE%" equ "0" goto :Work
- if /i "%PURGE%" equ "1" goto :Work
- goto :PROMPT_ONE
- :Work
- REM Create intermediate directories for log directory if necessary.
- if not exist "!LOG_DIRECTORY!" (
- mkdir "!LOG_DIRECTORY!" 2>nul
- if not exist "!LOG_DIRECTORY!" (
- echo Directory could not be created: "!LOG_DIRECTORY!"
- exit /b 1
- )
- )
- set TRACKED="!LOG_DIRECTORY!INSTALLER CLEANER - Tracked.log"
- if exist %TRACKED% del %TRACKED%
- set MISSING="!LOG_DIRECTORY!INSTALLER CLEANER - Missing.log"
- if exist %MISSING% del %MISSING%
- set ORPHANED="!LOG_DIRECTORY!INSTALLER CLEANER - Orphaned.log"
- if exist %ORPHANED% del %ORPHANED%
- set INSTALLERS="!LOG_DIRECTORY!INSTALLER CLEANER - Installers.log"
- if exist %INSTALLERS% del %INSTALLERS%
- echo Caching installer references...
- REM This will find both .msi and .msp entries.
- set KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
- set VALUE_NAME=LocalPackage
- for /f "usebackq tokens=1-2*" %%a in (`reg query "%KEY_NAME%" /s /v "%VALUE_NAME%" 2^>nul ^| findstr /i "%VALUE_NAME%"`) do (
- REM Append installer path to file.
- echo %%c>>%INSTALLERS%
- )
- if /i "%PURGE%" equ "0" (
- echo Looking for orphaned installers...
- ) else (
- echo Looking for orphaned installers to delete...
- )
- for /f "delims=" %%a in ('dir /b /s %WINDIR%\Installer\*.msi %WINDIR%\Installer\*.msp') do (
- 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
- REM The LocalPackage key can contain upper and lower-case variations, e.g.
- REM c:\Windows\Installer\177be02.msp
- REM C:\Windows\Installer\32dc90.msi
- REM Its bad because a file can be incorrectly classified as orphaned because findstr is case-sensitive by default.
- findstr /i /c:"%%a" %INSTALLERS% >nul
- if !errorlevel!==1 (
- echo "%%a" ORPHANED>>%ORPHANED%
- echo "%%a" ORPHANED
- if /i "%PURGE%" equ "1" (
- echo Deleted orphaned installer - %%a
- REM del "%%a"
- REM
- REM Using recycle.exe [http://www.geocities.ws/fp.westlake/xp/index.html] to move orphaned installer files to recycle bin instead of deleting.
- recycle /f "%%a"
- if exist "%%a" (
- set FAILURE_TO_DELETE=1
- )
- )
- ) else (
- echo "%%a" TRACKED>>%TRACKED%
- )
- )
- echo Looking for missing installers...
- for /f "delims=" %%a in ('type %INSTALLERS%') do (
- if not exist %%a (
- echo "%%a" MISSING>>%MISSING%
- echo "%%a" MISSING
- set MISSING_INSTALLER=1
- )
- )
- :End
- if %FAILURE_TO_DELETE% equ 1 pause
- if %MISSING_INSTALLER% equ 1 (
- echo -----
- echo Search this registry key with the installer path to determine its DisplayName:
- echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
- echo Find an installer online with that name and download it.
- echo Your looking for installers that offer a Repair option the first time you run them.
- echo Click Repair, click Browse to find the vc_red.msi file, and click Ok to reinstall.
- echo -----
- pause
- )
- REM if %NOPAUSE% equ 1 goto :eof
- timeout /t 7 /nobreak
- goto :eof
Add Comment
Please, Sign In to add comment