:: -------------------------------------------------------
@echo off
cd /d %~dp0
setlocal enabledelayedexpansion
set LOGFILE=.\vcom.log
echo ++++++++++ Start %date% %time% ++++++++++ >>  %LOGFILE%

net session > nul 2>&1
if errorlevel 1 (
  echo.
  echo Run as administrator.
  echo Run as administrator. >>  %LOGFILE%
  echo.
  echo +++++++++++++++++++++++++++++++++++++++++ >>  %LOGFILE%
  pause
  endlocal
  exit /b
)
:: -------------------------------------------------------
for /f "usebackq" %%i in (`findstr /m /r /i /c:"^ctusbdr.sys$" c:\windows\inf\oem*.inf`) do set vbus=%%i
for /f "usebackq" %%j in (`findstr /m /r /i /c:"^ctvrcom.sys$" c:\windows\inf\oem*.inf`) do set vcom=%%j
echo [before] ctusbdr.sys - %vbus% >> %LOGFILE%
echo [before] ctvrcom.sys - %vcom% >> %LOGFILE%
:: -------------------------------------------------------
if "%vbus%" == "" (
  echo [ctusbdr.sys] not exec pnputil >> %LOGFILE%
) else (
  echo [ctusbdr.sys] exec pnputil >> %LOGFILE%
  pnputil -f -d %vbus% >> %LOGFILE%
  echo. >> %LOGFILE%
  ping localhost -n 8 >> %LOGFILE%
)
if "%vcom%" == "" (
  echo [ctvrcom.sys] not exec pnputil >> %LOGFILE%
) else (
  echo [ctvrcom.sys] exec pnputil >> %LOGFILE%
  pnputil -f -d %vcom% >> %LOGFILE%
  echo. >> %LOGFILE%
  ping localhost -n 8 >> %LOGFILE%
)
:: -------------------------------------------------------
for /f "usebackq" %%i in (`findstr /m /r /i /c:"^ctusbdr.sys$" c:\windows\inf\oem*.inf`) do set vbus2=%%i
for /f "usebackq" %%j in (`findstr /m /r /i /c:"^ctvrcom.sys$" c:\windows\inf\oem*.inf`) do set vcom2=%%j
echo [after] ctusbdr.sys - %vbus2% >> %LOGFILE%
echo [after] ctvrcom.sys - %vcom2% >> %LOGFILE%
:: -------------------------------------------------------
for /f "usebackq" %%k in (`devcon hwids ctvrcom* ^| find /c "ROOT"`) do set count=%%k
echo Count_ROOTPORT - %count% >> %LOGFILE%
for /l %%l in (1,1,%count%) do (
  for /f "usebackq" %%m in (`devcon hwids ctvrcom* ^| find "ROOT"`) do set hwid=%%m
  echo hwid - !hwid! >> %LOGFILE%
  for /f "usebackq" %%n in (`devcon remove @"!hwid!"`) do set result=%%n
  echo devcon - !result! >> %LOGFILE%
)
:: -------------------------------------------------------
if exist "%windir%\System32\drivers\ctvrcom.sys" (
  for /f "usebackq" %%o in (`del /f %windir%\System32\drivers\ctvrcom.sys`) do set delres=%%o
  echo del ctvrcom.sys - !delres! >> %LOGFILE%
)
:: -------------------------------------------------------
C:\Windows\ctvrcom\setup.exe /u
:: -------------------------------------------------------
:end
echo +++++++++++++++++++++++++++++++++++++++++ >>  %LOGFILE%
endlocal
@echo on
