@Echo Off
Setlocal EnableDelayedExpansion
color 0a
REM ======================================
REM  删除 Flash Player 程序 v2.1，2018.12.06
REM  1、支持删除 win XP/7/8/8.1/10 系统的 Flash Player 程序；
REM  2、删除包括系统预安装的Flash组件，用户安装的Flash Player，
REM     已经被强制安装的大陆特供版程序）
REM  v1.0 20170602,删除系统预安装的 Flash 组件
REM ======================================

pushd "%~dp0"
cd /d "%~pd0"

title 卸载 Win XP/7/8.1/10 中的 Flash Player 软件，v2.1，20181206

Reg.exe query "HKU\S-1-5-19\Environment" > nul 2>nul
If Not %ERRORLEVEL% EQU 0 (
    cls
	echo.
 	echo  --- 请右键以管理员身份运行本程序，按任意键继续 ...
 	echo. 
 	pause>nul
 	exit /b
)

echo.
echo. =================================================================
echo  本程序将检测并卸载系统上所安装的 Flash Player 软件，包括：
echo.
echo    1、Win 8.1/10 系统上的预安装 Flash Player 组件（ActiveX）
echo.
echo    2、Win XP/7/8.1/10 系统上的用户单独安装的 Flash Player 软件
echo       （ActiveX、NPAPI 和 PPAPI）
echo.
echo    3、国内用户被强制升级的大陆特供版 Flash Player 软件
echo       （ActiveX、NPAPI 和 PPAPI）
echo. =================================================================
echo.
echo  - 卸载 Flash 之前，请先关闭浏览器等使用 Flash Player 的软件
echo.
echo  - 关闭后，请按任意键开始检测并卸载 Flash Player 软件 ...
pause>nul

set "win_ver="
ver | find "5.1." > NUL && set "win_ver=xp_win7"
ver | find "6.1." > NUL && set "win_ver=xp_win7"
ver | find "6.3." > NUL && set "win_ver=win8_10"
ver | find "10." > NUL && set "win_ver=win8_10"

Set "xOS=x64"& If "%PROCESSOR_ARCHITECTURE%"=="x86" If Not Defined PROCESSOR_ARCHITEW6432 Set "xOS=x32"

set "FlashRoot64=%Windir%\SysWOW64\Macromed"
set "FlashRoot32=%Windir%\System32\Macromed"
set "FlashAppDataRoot=%USERPROFILE%\AppData\Roaming\Macromedia"
set "OfficialUninstaller=uninstall_flash_player.exe"
	
if "%xOS%"=="x32" (
  set SetACL="RegACL32.exe"
  set "FlashRoot=%FlashRoot32%\Flash"
) else (
  set SetACL="RegACL64.exe"
  set "FlashRoot=%FlashRoot64%\Flash"
)

call :CheckFlashPlayerVersion

if defined use_dism_uninstaller call :DeleteActiveXFlashPlayer

if defined use_official_uninstaller call :UninstallFlashPlayer

if not defined activex_ver_deleted_error if not defined flash_deleted_error (
	call :DeleteFlashPlayerRoot %FlashRoot32%
	call :DeleteFlashPlayerRoot %FlashAppDataRoot%

	if "%xOS%"=="x64" (
	  call :DeleteFlashPlayerRoot %FlashRoot64%
	)
	if not defined mainland_ver_found (
		echo.
		echo. [*] === 重要提示 ===
		echo.
		echo.    检测并卸载 Flash Player 完毕！
		echo.
		echo.    卸载 Flash Player 后，不可再另行安装 Flash Player 软件，
		echo.    否则还会引入 Flash 的安全问题.		
		echo.	
	)
)

if not defined flash_deleted_error (
	if defined mainland_ver_found (
		echo.
		echo. [*] === 重要提示 ===
		echo.
		echo.    由于系统已被强制安装了大陆特供版 Flash Player，
		echo.    尽管已被卸载，但慎重起见，建议还是要重做系统：
		echo.		
		echo.    1、若保留以前的干净系统备份，可用备份恢复系统；
		echo.
		echo.    2、或者从新安装并设置系统。
		echo.    
		echo.    重做系统后，请断开网络，再次运行本程序卸载系统自带的 Flash Player 软件.
		echo.
		echo.    卸载 Flash Player 后，不可再另行安装 Flash Player 软件，
		echo.    否则还会引入 Flash 的安全问题.		
		echo.
	)
)

if defined activex_ver_deleted_error (
		echo.
		echo 【3/4】+ 系统预安装的 Flash Player（ActiveX）卸载失败
		echo.       -------------------------------------------------------------------
		echo.       请重启系统后再次操作 ...		
		echo.
)
if defined flash_deleted_error (
		echo.
		echo 【3/4】+ 用户单独安装的 Flash Player（NPAPI或PPAPI）卸载失败
		echo.       --------------------------------------------------------------------
		echo.       请再次运行卸载程序卸载，或在控制面板中手工卸载 ...		
		echo.
)

echo. 
echo.    请按任意键退出程序 ...				
echo.	
		
pause>nul

exit /b

rem -------------------------------------
rem 检测系统中所安装的 Flash Player 版本
rem -------------------------------------
:CheckFlashPlayerVersion

	set "mainland_ver_found="
	set "activex_ver_found="
	set "activex_flash_component_name="
	set "PPAPI_ver_found="
	set "NPAPI_ver_found="
	set "use_dism_uninstaller="
	set "use_official_uninstaller="
	
	for /f "usebackq tokens=8 delims=\" %%a in (`reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages" ^| find /i "Adobe-Flash-For-Windows-Package"`) do (
	  set "activex_flash_component_name=%%a"
	)
	
	if exist "!FlashRoot!\activex.vch" (
		set "activex_ver_found=1"
	) else (
		if defined activex_flash_component_name set "activex_ver_found=1"
	)
		
	if defined activex_ver_found (
		if /i "!win_ver!" == "xp_win7" set "use_official_uninstaller=1"
		if /i "!win_ver!" == "win8_10" set "use_dism_uninstaller=1"
		echo.
		echo 【1/4】+ 找到系统预安装的组件版 Flash Player（ActiveX）
		echo.
	)  else (
		echo.
		echo 【1/4】+ 系统没有预安装的 Flash Player（ActiveX）或已被卸载！
		echo.
	)
	if exist "!FlashRoot!\FlashHelperService.exe" (
		set "mainland_ver_found=1"
		set "use_official_uninstaller=1"
		echo.
		echo 【1/4】+ 找到大陆特供版 Flash Player。
		echo.		
	)
	if exist "!FlashRoot!\plugin.vch" (
		set "NPAPI_ver_found=1"
		set "use_official_uninstaller=1"
		echo.
		echo 【1/4】+ 找到火狐浏览器插件版 Flash Player（NPAPI）。
		echo.
	)
	if exist "!FlashRoot!\pepper.vch" (
		set "PPAPI_ver_found=1"
		set "use_official_uninstaller=1"
		echo.
		echo 【1/4】+ 找到谷歌浏览器插件版 Flash Player（PPAPI）。
		echo.		
	)
	
	exit /b

rem -----------------------------------	
rem 检测并删除预安装的Flash组件
rem -----------------------------------
:DeleteActiveXFlashPlayer
	
	if not exist "%SetACL%" (
	  echo.
	  echo  - 错误：权限设置文件 %SetACL% 不存在！	  
	  echo. - 预安装 Flash 组件未卸载！
	  echo.
	  exit /b
	)
	
	if not defined activex_flash_component_name (
	  echo.
	  echo 【2/4】- 系统没有预安装 Flash Player 组件或已被卸载！
	  echo.
	  exit /b
	) else (	  
	  echo.
	  echo 【2/4】- 找到系统预安装的 Flash Player 组件，正在卸载该组件 ...
	  echo.
	)
		
	set "flash=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages\%activex_flash_component_name%"

	%SetACL% -reg:"%flash%" -do:grantall

	Reg.exe add "%flash%" /v "Visibility" /t REG_DWORD /d "1" /f
	Reg.exe add "%flash%" /v "DefVis" /t REG_DWORD /d "2" /f
	Reg.exe delete "%flash%\Owners" /f >nul 2>nul

	dism.exe /Online /Remove-Package /PackageName:%activex_flash_component_name%

	rem 检测是否完整卸载组件
	set "activex_ver_deleted_error="
	set "activex_flash_component_name="

	for /f "usebackq tokens=8 delims=\" %%a in (`reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages" ^| find /i "Adobe-Flash-For-Windows-Package"`) do (
		set "activex_flash_component_name=%%a"
	)	
		
	if exist "!FlashRoot!\activex.vch" (
		set "activex_ver_deleted_error=1"
	) else (
		if defined activex_flash_component_name set "activex_ver_deleted_error=1"
	)
	
	if defined activex_ver_deleted_error (
		echo.
		echo 【2/4】+ 系统预安装的组件版 Flash Player（ActiveX）卸载失败。
		echo.	
	) else (
		echo.
		echo 【2/4】+ 系统预安装的组件版 Flash Player（ActiveX）卸载成功！
		echo.
	)
	
	exit /b
	
rem -----------------------------------	
rem 卸载用户自己安装的Flash（包括大陆特供版）
rem -----------------------------------
:UninstallFlashPlayer
	
	if not exist "%OfficialUninstaller%" (
	  echo.
	  echo  - 错误：官方卸载程序 %OfficialUninstaller% 不存在！	  	  
	  echo.
	  exit /b
	)

	echo.
	echo 【3/4】- 开始卸载用户单独安装的 Flash Player 软件...
	echo.       ========================================================
	echo        + 请在弹出对话框中点击“卸载”按钮开始卸载 Flash Player
	echo.       ========================================================	    
	echo.
	%OfficialUninstaller%
	
	set flash_deleted_error=
	
	if /i "!win_ver!"=="xp_win7" if defined activex_ver_found (
		if exist "!FlashRoot!\activex.vch" (
			set "flash_deleted_error=1"		
			echo.
			echo [*] - IE浏览器 Flash Player 组件版（ActiveX）未正确卸载！
			echo.				
		) else (
			echo.
			echo [*] + IE浏览器 Flash Player 组件版（ActiveX）已成功卸载！
			echo.				
		)	
	)
	
	if defined mainland_ver_found (
		if exist "!FlashRoot!\FlashHelperService.exe" (
			set "flash_deleted_error=1"
			echo.
			echo [*] - 大陆特供版 Flash Player 未正确卸载！
			echo.		
		) else (
			echo.
			echo [*] + 大陆特供版 Flash Player 已成功卸载！
			echo.
		)
	)
	
	if defined NPAPI_ver_found (
		if exist "!FlashRoot!\plugin.vch" (
			set "flash_deleted_error=1"		
			echo.
			echo [*] - 火狐浏览器 Flash Player 插件版（NPAPI）未正确卸载！
			echo.				
		) else (
			echo.
			echo [*] + 火狐浏览器 Flash Player 插件版（NPAPI）已成功卸载！
			echo.				
		)
	)
	
	if defined PPAPI_ver_found (
		if exist "!FlashRoot!\pepper.vch" (
			set "flash_deleted_error=1"		
			echo.
			echo [*] - 谷歌浏览器 Flash Player 插件版（PPAPI）未正确卸载！
			echo.				
		) else (
			echo.
			echo [*] + 谷歌浏览器 Flash Player 插件版（PPAPI）已成功卸载！
			echo.					
		)	
	)
	
	exit /b
rem -----------------------------------		
rem 删除flash的安装文件夹
rem 参数：%1 = flash 的文件夹
rem -----------------------------------
:DeleteFlashPlayerRoot

	if exist "%1" (
		ver | find "5.1." > NUL || takeown /f "%1" /r /d y >nul && icacls "%1" /grant administrators:F /t >nul
		rd /s /q "%1" > nul 2> nul

		echo.
		echo 【4/4】- Flash Player 软件的安装文件夹已删除：%1
		echo.		
	)
	
  	exit /b

