@echo off
copy StartTaskmgrMin.bat %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
setlocal enabledelayedexpansion
cls

:start
set "found="

:: Check if COM4 exists
for /f "tokens=1 delims=:" %%j in ('devcon findall * ^| findstr /i "COM4"') do (
    echo Found COM4: %%j
    set "found=1"
)

:: If COM4 does not exist, perform a remove and rescan
if not defined found (
    echo Removing COM1...
    for /f "tokens=1 delims=:" %%i in ('devcon findall * ^| findstr /i "COM1"') do (
        set A="@%%i"
        echo Removing: %%i
        call devcon remove "%%A%%"
        timeout /t 1 /nobreak >nul
    )
    echo Rescanning hardware...
    devcon rescan
    timeout /t 2 /nobreak >nul
    goto start
)

:: COM4 exists, execute the installation
echo Reset COM Port successfully.
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows NT\CurrentVersion\Winlogon" /v SyncForegroundPolicy /t REG_DWORD /d 1 /f
cls
SetupChipset2.exe
