@echo off
rem ===========================================
rem  Windows Update ドライバー除外設定の有効化
rem  ===========================================

rem where findstr でエラーが出た場合、 C:\WINDOWS\system32 を追加
where findstr >nul 2>nul || set "PATH=%PATH%;C:\WINDOWS\system32"

:: Windowsバージョンチェック
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%VERSION%" == "10.0" (
    echo I checked my Windows version。
) else (
    cls
    rem echo ========================================
    echo error  Unsupported OS
    rem echo ========================================
    echo.
    echo This tool is for Windows 10 or Windows 11 only.
    echo.
    echo Pressing any key will exit without making any changes.
    pause > nul
    exit /b 1
)

rem 管理者権限で実行されているかチェック
net session >nul 2>&1
if %errorLevel% neq 0 (
    color e0
    echo ERROR Running with general privileges
    echo Please run with administrator privileges.
    echo Pressing any key will exit without making any changes.
    pause > nul
    exit /b
)

cls
rem echo ===========================================
echo.
echo Enable Windows Update driver exclusions
rem echo ===========================================
echo.
rem echo Administrator privileges confirmed.
rem echo Starting setup
echo.

rem レジストリキーのパス
set "REG_PATH=HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
set "VALUE_NAME=ExcludeWUDriversInQualityUpdate"
set "VALUE_DATA=1"

rem レジストリキーが存在するか確認
reg query "%REG_PATH%" >nul 2>&1
if %errorLevel% neq 0 (
    echo Creating registry keys
    reg add "%REG_PATH%" /f >nul 2>&1
    if %errorLevel% equ 0 (
        echo A registry key has been created.
    )
) else (
    echo The registry key exists.
)

rem DWORD値を設定
echo Setting the registry value
reg add "%REG_PATH%" /v "%VALUE_NAME%" /t REG_DWORD /d %VALUE_DATA% /f >nul 2>&1
if %errorLevel% neq 0 (
    echo error Failed to set the registry value.
    goto :error
)

rem 設定確認
reg query "%REG_PATH%" /v "%VALUE_NAME%" | find "0x1" >nul 2>&1
if %errorLevel% equ 0 (
    echo.
    rem echo ========================================
    echo Configuration completed successfully
    rem echo ========================================
    echo.
    echo Setting details
    echo   Registry Key  %REG_PATH%
    echo   Value Name  %VALUE_NAME%
    echo   Value data  %VALUE_DATA%
    echo.
    echo Driver updates have been excluded from Windows Update.
    echo.
) else (
    echo error Registry value verification failed.
    goto :error
)

pause
exit /b 0

:error
color e0
echo.
echo An error has occurred.
echo Please check your settings.
echo.
pause
exit /b 1
