@echo off
setlocal EnableExtensions
set "HardwareID=VID_05C6&PID_9008"
set "RegistryPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
set "ProductName=Qualcomm HS-USB QDLoader 9008"
set "DeviceFound=0"

cls
echo.
for /F "delims=" %%I in ('%SystemRoot%\System32\reg.exe QUERY "%RegistryPath%\%HardwareID%" 2^>nul') do call :GetPort "%%I"

if "%DeviceFound%" == "0" echo WARNING: Could not find any connected %ProductName%.

echo.
endlocal
pause
goto :EOF

:GetPort
set "RegistryKey=%~1"
if /I not "%RegistryKey:~0,71%" == "%RegistryPath%\%HardwareID%\" goto :EOF

for /F "skip=2 tokens=1,3" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1\Device Parameters" /v PortName 2^>nul') do (
    if /I "%%A" == "PortName" set "SerialPort=%%B" && goto OutputPort
)
goto :EOF

:OutputPort
%SystemRoot%\System32\reg.exe query HKLM\HARDWARE\DEVICEMAP\SERIALCOMM | %SystemRoot%\System32\findstr.exe /E /I /L /C:%SerialPort% >nul
if errorlevel 1 goto :EOF
set "DeviceFound=1"
set "DeviceNumber=%RegistryKey:~-1%"
echo %DeviceNumber%. %ProductName% is %SerialPort%.


fh_loader.exe --port=\\.\%SerialPort% --sendxml=EVW-0-044C-rawprogram0.xml --search_path=%cd% --noprompt --showpercentagecomplete --zlpawarehost=1
fh_loader.exe --port=\\.\%SerialPort% --sendxml=EVW-0-044C-patch0.xml --search_path=%cd% --noprompt --showpercentagecomplete --zlpawarehost=1

echo Waiting: Reset device...
timeout /t 2
fh_loader.exe --port=\\.\%SerialPort% --reset

echo Done
@echo off
pause


