Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for resetdevices.bat

(view source code of resetdevices.bat as plain text)

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. PUSHD "%~dp0"
  4.  
  5. :: Check if DEVCON.EXE is available and if not, prompt for download
  6. SET DevconAvailable=
  7. SET Download=
  8. DEVCON.EXE /? >NUL 2>&1
  9. IF ERRORLEVEL 1 (
  10. 	SET DevconAvailable=No
  11. 	ECHO This batch file requires Microsoft's DEVCON untility,
  12. 	ECHO part of Microsoft's Windows Driver Kit ^(WDK^).
  13. 	SET /P Download=Do you want to download the WDK now? [y/N] 
  14. )
  15.  
  16. :: Start download if requested
  17. IF /I "%Download%"=="Y" (
  18. 	START "WDK" https://www.microsoft.com/en-us/download/details.aspx?id=11800
  19. 	ECHO.
  20. 	ECHO Install the downloaded WDK, or read
  21. 	ECHO http://www.robvanderwoude.com/devcon.php
  22. 	ECHO to find out how to install DEVCON.EXE only.
  23. 	ECHO Make sure DEVCON.EXE is in the PATH, then try again.
  24. )
  25.  
  26. :: Abort if DEVCON.EXE is not available yet
  27. IF "%DevconAvailable%"=="No" (
  28. 	ENDLOCAL
  29. 	GOTO:EOF
  30. )
  31.  
  32. :: Parse command line
  33. SET Filter=
  34. SET ListOnly=0
  35. SET Debug=
  36. ECHO.%* | FIND.EXE /I "/Debug" >NUL && SET Debug=ECHO
  37. :Loop
  38. IF "%~1"=="" GOTO Next
  39. IF /I "%~1"=="/L" (
  40. 	SET ListOnly=1
  41. 	IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> ListOnly
  42. ) ELSE (
  43. 	IF /I "%~1"=="/DEBUG" (
  44. 		ECHO CommandLineArgument: "%~1" =^> Debug
  45. 	) ELSE (
  46. 		ECHO "%~1" | FINDSTR.EXE /R /B /C:".=" >NUL
  47. 		IF NOT ERRORLEVEL 1 (
  48. 			SET Filter=!Filter! "%~1"
  49. 			IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Filter=!Filter:~1!
  50. 		) ELSE (
  51. 			ECHO "%~1" | FINDSTR.EXE /R /B /C:".@" >NUL
  52. 			IF NOT ERRORLEVEL 1 (
  53. 				SET Filter=!Filter! "%~1"
  54. 				IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Filter=!Filter:~1!
  55. 			) ELSE (
  56. 				IF "%Debug%"=="ECHO" ECHO CommandLineArgument: "%~1" =^> Unknown
  57. 				GOTO Syntax
  58. 			)
  59. 		)
  60. 	)
  61. )
  62. SHIFT
  63. GOTO Loop
  64.  
  65. :Next
  66. IF "%Debug%"=="ECHO" ECHO Filter=%Filter%
  67. ECHO.%Filter% | FINDSTR.EXE /R /I /C:"[A-Z]" >NUL
  68. IF ERRORLEVEL 1 (
  69. 	SET Filter=*
  70. 	IF "%Debug%"=="ECHO" ECHO Filter=!Filter!
  71. )
  72.  
  73. DEVCON.EXE FindAll %Filter% 2>NUL | FIND.EXE /V " matching device" > "%~n0.All"
  74. IF "%Debug%"=="ECHO" (
  75. 	FOR /F %%A IN ('TYPE "%~n0.All" ^| FIND.EXE /C /V ""') DO (
  76. 		ECHO All Devices: %%A
  77. 	)
  78. )
  79. DEVCON.EXE Find    %Filter% 2>NUL | FIND.EXE /V " matching device" > "%~n0.Connected"
  80. IF "%Debug%"=="ECHO" (
  81. 	FOR /F %%A IN ('TYPE "%~n0.Connected" ^| FIND.EXE /C /V ""') DO (
  82. 		ECHO Connected Devices: %%A
  83. 	)
  84. )
  85. TYPE NUL > "%~n0.Disconnected"
  86. TYPE NUL > "%~n0.Removed"
  87. FOR /F "delims=: " %%A IN ('TYPE "%~n0.All"') DO (
  88. 	TYPE "%~n0.Connected" | FIND.EXE /I "%%~A" > NUL
  89. 	IF ERRORLEVEL 1 (
  90. 		TYPE "%~n0.All" | FIND.EXE /I "%%~A" >> "%~n0.Disconnected"
  91. 	)
  92. )
  93. IF "%Debug%"=="ECHO" (
  94. 	FOR /F %%A IN ('TYPE "%~n0.Disconnected" ^| FIND /C /V ""') DO (
  95. 		ECHO Disconnected Devices: %%A
  96. 	)
  97. )
  98.  
  99. :: List or remove all matching disconnected devices
  100. IF "%ListOnly%"=="1" (
  101. 	ECHO.
  102. 	ECHO Connected Devices:
  103. 	ECHO.==================
  104. 	TYPE "%~n0.Connected"
  105. 	ECHO.
  106. 	ECHO Disconnected Devices:
  107. 	ECHO.=====================
  108. 	TYPE "%~n0.Disconnected"
  109. 	ECHO.
  110. ) ELSE (
  111. 	FOR /F "delims=: " %%A IN ('TYPE "%~n0.Disconnected"') DO (
  112. 		%Debug% DEVCON.EXE Remove "@%%~A"
  113. 		TYPE "%~n0.Disconnected" | FIND.EXE /I "%%~A" >> "%~n0.Removed"
  114. 	)
  115. )
  116. IF "%Debug%"=="ECHO" (
  117. 	FOR /F %%A IN ('TYPE "%~n0.Removed" ^| FIND /C /V ""') DO (
  118. 		ECHO Removed Devices: %%A
  119. 	)
  120. )
  121.  
  122. :: Done
  123. POPD
  124. ENDLOCAL
  125. GOTO:EOF
  126.  
  127. :Syntax
  128. ECHO.
  129. ECHO ResetDevices.bat,  Version 1.01 for Windows 7
  130. ECHO Use DEVCON to remove all USB devices except the currently connected ones
  131. ECHO.
  132. ECHO Usage:  RESETDEVICES  [ "filters" ]  [ /L ]  [ /DEBUG ]
  133. ECHO.
  134. ECHO Where:  "filters"     limits the removal to the specified hardware class
  135. ECHO                       and/or hardware ID^(s^) ^(see notes 1 and 2^)
  136. ECHO         /L            List matching devices only, do not remove any
  137. ECHO         /DEBUG        Show "intermediate results", do not remove devices
  138. ECHO.
  139. ECHO Notes:  [1] Filters must all be doublequoted.
  140. ECHO             Hardware classes must be prefixed with an equal sign,
  141. ECHO             e.g. RESETDEVICES "=Mouse".
  142. ECHO             Hardware IDs must be prefixed with an "at" sign ^(@^),
  143. ECHO             e.g. RESETDEVICES "@HID\VID_093A&PID_2521\6&6C639D3&0&0000".
  144. ECHO         [2] Wildcards are allowed in hardware IDs,
  145. ECHO             e.g. RESETDEVICES "@HID\VID_093A&*".
  146. ECHO         [3] This batch file requires Microsoft's DEVCON utility,
  147. ECHO             part of the Windows Driver Kit ^(WDK^), available at
  148. ECHO             https://www.microsoft.com/en-us/download/details.aspx?id=11800
  149. ECHO             You will be prompted for download if it isn't found.
  150. ECHO         [4] This batch file requires elevated privileges.
  151. ECHO         [5] I only tested this batch file on my own PC, in Windows 7.
  152. ECHO             I cannot guarantee flawless operation on any other PC.
  153. ECHO             Use this batch file entirely at your own risk.
  154. ECHO             Make sure you have a recent and verified backup available in
  155. ECHO             case something might go wrong.
  156. ECHO         [6] Each removal is logged in a file named "%~n0.Removed".
  157. ECHO.
  158. ECHO Written by Rob van der Woude
  159. ECHO http://www.robvanderwoude.com
  160.  
  161. POPD
  162. ENDLOCAL
  163. EXIT /B 1
  164.  

page last modified: 2024-04-16; loaded in 0.0065 seconds