(view source code of resetdevices.bat as plain text)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
PUSHD "%~dp0"
:: Check if DEVCON.EXE is available and if not, prompt for download
SET DevconAvailable=
SET Download=
DEVCON.EXE /? >NUL 2>&1
IF ERRORLEVEL 1 (
SET DevconAvailable=No
ECHO This batch file requires Microsoft's DEVCON untility,
ECHO part of Microsoft's Windows Driver Kit ^(WDK^).
SET /P Download=Do you want to download the WDK now? [y/N]
)
:: Start download if requested
IF /I "%Download%"=="Y" (
START "WDK" https://www.microsoft.com/en-us/download/details.aspx?id=11800
ECHO.
ECHO Install the downloaded WDK, or read
ECHO http://www.robvanderwoude.com/devcon.php
ECHO to find out how to install DEVCON.EXE only.
ECHO Make sure DEVCON.EXE is in the PATH, then try again.
)
:: Abort if DEVCON.EXE is not available yet
IF "%DevconAvailable%"=="No" (
ENDLOCAL
GOTO:EOF
)
:: Parse command line
SET Filter=
SET ListOnly=0
SET Debug=
ECHO.%* | FIND.EXE /I "/Debug" >NUL && SET Debug=ECHO
:Loop
IF "%~1"=="" GOTO Next
IF /I "%~1"=="/L" (
SET ListOnly=1
IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> ListOnly
) ELSE (
IF /I "%~1"=="/DEBUG" (
ECHO CommandLineArgument: "%~1" =^> Debug
) ELSE (
ECHO "%~1" | FINDSTR.EXE /R /B /C:".=" >NUL
IF NOT ERRORLEVEL 1 (
SET Filter=!Filter! "%~1"
IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Filter=!Filter:~1!
) ELSE (
ECHO "%~1" | FINDSTR.EXE /R /B /C:".@" >NUL
IF NOT ERRORLEVEL 1 (
SET Filter=!Filter! "%~1"
IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Filter=!Filter:~1!
) ELSE (
IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Unknown
GOTO Syntax
)
)
)
)
SHIFT
GOTO Loop
:Next
IF "%Debug%"=="ECHO" ECHO Filter=%Filter%
ECHO.%Filter% | FINDSTR.EXE /R /I /C:"[A-Z]" >NUL
IF ERRORLEVEL 1 (
SET Filter=*
IF "%Debug%"=="ECHO" ECHO Filter=!Filter!
)
DEVCON.EXE FindAll %Filter% 2>NUL | FIND.EXE /V " matching device" > "%~n0.All"
IF "%Debug%"=="ECHO" (
FOR /F %%A IN ('TYPE "%~n0.All" ^| FIND.EXE /C /V ""') DO (
ECHO All Devices: %%A
)
)
DEVCON.EXE Find %Filter% 2>NUL | FIND.EXE /V " matching device" > "%~n0.Connected"
IF "%Debug%"=="ECHO" (
FOR /F %%A IN ('TYPE "%~n0.Connected" ^| FIND.EXE /C /V ""') DO (
ECHO Connected Devices: %%A
)
)
TYPE NUL > "%~n0.Disconnected"
TYPE NUL > "%~n0.Removed"
FOR /F "delims=: " %%A IN ('TYPE "%~n0.All"') DO (
TYPE "%~n0.Connected" | FIND.EXE /I "%%~A" > NUL
IF ERRORLEVEL 1 (
TYPE "%~n0.All" | FIND.EXE /I "%%~A" >> "%~n0.Disconnected"
)
)
IF "%Debug%"=="ECHO" (
FOR /F %%A IN ('TYPE "%~n0.Disconnected" ^| FIND /C /V ""') DO (
ECHO Disconnected Devices: %%A
)
)
:: List or remove all matching disconnected devices
IF "%ListOnly%"=="1" (
ECHO.
ECHO Connected Devices:
ECHO.==================
TYPE "%~n0.Connected"
ECHO.
ECHO Disconnected Devices:
ECHO.=====================
TYPE "%~n0.Disconnected"
ECHO.
) ELSE (
FOR /F "delims=: " %%A IN ('TYPE "%~n0.Disconnected"') DO (
%Debug% DEVCON.EXE Remove "@%%~A"
TYPE "%~n0.Disconnected" | FIND.EXE /I "%%~A" >> "%~n0.Removed"
)
)
IF "%Debug%"=="ECHO" (
FOR /F %%A IN ('TYPE "%~n0.Removed" ^| FIND /C /V ""') DO (
ECHO Removed Devices: %%A
)
)
:: Done
POPD
ENDLOCAL
GOTO:EOF
:Syntax
ECHO.
ECHO ResetDevices.bat, Version 1.01 for Windows 7
ECHO Use DEVCON to remove all USB devices except the currently connected ones
ECHO.
ECHO Usage: RESETDEVICES [ "filters" ] [ /L ] [ /DEBUG ]
ECHO.
ECHO Where: "filters" limits the removal to the specified hardware class
ECHO and/or hardware ID^(s^) ^(see notes 1 and 2^)
ECHO /L List matching devices only, do not remove any
ECHO /DEBUG Show "intermediate results", do not remove devices
ECHO.
ECHO Notes: [1] Filters must all be doublequoted.
ECHO Hardware classes must be prefixed with an equal sign,
ECHO e.g. RESETDEVICES "=Mouse".
ECHO Hardware IDs must be prefixed with an "at" sign ^(@^),
ECHO e.g. RESETDEVICES "@HID\VID_093A&PID_2521\6&6C639D3&0&0000".
ECHO [2] Wildcards are allowed in hardware IDs,
ECHO e.g. RESETDEVICES "@HID\VID_093A&*".
ECHO [3] This batch file requires Microsoft's DEVCON utility,
ECHO part of the Windows Driver Kit ^(WDK^), available at
ECHO https://www.microsoft.com/en-us/download/details.aspx?id=11800
ECHO You will be prompted for download if it isn't found.
ECHO [4] This batch file requires elevated privileges.
ECHO [5] I only tested this batch file on my own PC, in Windows 7.
ECHO I cannot guarantee flawless operation on any other PC.
ECHO Use this batch file entirely at your own risk.
ECHO Make sure you have a recent and verified backup available in
ECHO case something might go wrong.
ECHO [6] Each removal is logged in a file named "%~n0.Removed".
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
POPD
ENDLOCAL
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0065 seconds