@echo off
setlocal EnableExtensions
title Test EXE file association

set "REPORT=%~dp0EXE-Association-Test-Result.txt"
if /i "%~1"=="--console" set "REPORT=%TEMP%\EXE-Association-Test-Result-%RANDOM%.txt"
set "ASSOC_VALUE="
set "FTYPE_VALUE="
set "USER_CHOICE="
set "USER_DOT_EXE="
set "RESULT=PASS"
for /f "delims=" %%A in ('assoc .exe 2^>nul') do set "ASSOC_VALUE=%%A"
for /f "delims=" %%A in ('ftype exefile 2^>nul') do set "FTYPE_VALUE=%%A"
for /f "tokens=2,*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.exe\UserChoice" /v "Progid" 2^>nul ^| find /i "Progid"') do set "USER_CHOICE=%%B"
for /f "tokens=2,*" %%A in ('reg query "HKCU\Software\Classes\.exe" /ve 2^>nul ^| find /i "REG_SZ"') do set "USER_DOT_EXE=%%B"
if /i not "%ASSOC_VALUE%"==".exe=exefile" set "RESULT=WARNING"
if not defined FTYPE_VALUE set "RESULT=WARNING"
if defined USER_CHOICE if /i not "%USER_CHOICE%"=="exefile" set "RESULT=WARNING"
if defined USER_DOT_EXE if /i not "%USER_DOT_EXE%"=="exefile" set "RESULT=WARNING"
>"%REPORT%" echo EXE Association Test Result
>>"%REPORT%" echo ===========================
>>"%REPORT%" echo RESULT=%RESULT%
>>"%REPORT%" echo.
>>"%REPORT%" echo Effective association: %ASSOC_VALUE%
>>"%REPORT%" echo Effective open command: %FTYPE_VALUE%
>>"%REPORT%" echo Per-user Open With choice: %USER_CHOICE%
>>"%REPORT%" echo Per-user .exe class: %USER_DOT_EXE%
>>"%REPORT%" echo.
if "%RESULT%"=="PASS" (
  >>"%REPORT%" echo The EXE association appears normal.
) else (
  >>"%REPORT%" echo The EXE association is abnormal or incomplete.
  >>"%REPORT%" echo Run Repair-EXE-Association.cmd, then test again.
)
type "%REPORT%"
if /i "%~1"=="--console" del "%REPORT%" >nul 2>&1
echo.
if /i not "%~1"=="--console" pause
if "%RESULT%"=="PASS" (
  echo [PASS] EXE files should open as Windows programs.
  exit /b 0
) else (
  echo [WARNING] Run Repair-EXE-Association.cmd, then test again.
  exit /b 2
)
