Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for rmhiddev.bat

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

  1. @ECHO OFF
  2. :: Check DOS/Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Localize variables
  6. SETLOCAL
  7.  
  8. :: Check command line arguments
  9. IF NOT "%~1"=="" IF /I NOT "%~1"=="/D" IF /I NOT "%~1"=="/Y" GOTO Syntax
  10.  
  11. :: Check if debug mode is requested
  12. IF /I NOT "%Debug%"=="ECHO" SET Debug=
  13. ECHO.%* | FIND /I "/D" >NUL && SET Debug=ECHO
  14.  
  15. :: Check if confirmation is required
  16. SET Confirmed=N
  17. ECHO.%* | FIND /I "/Y" >NUL && SET Confirmed=Y
  18.  
  19. :: Check if DEVCON.EXE is available and if not, prompt for download
  20. SET DevconAvailable=
  21. SET Download=
  22. DEVCON.EXE /? >NUL 2>&1
  23. IF ERRORLEVEL 1 (
  24. 	SET DevconAvailable=No
  25. 	ECHO This batch file requires Microsoft's DEVCON untility.
  26. 	SET /P Download=Do you want to download it now? [y/N] 
  27. )
  28.  
  29. :: Start download if requested
  30. IF /I "%Download%"=="Y" (
  31. 	START "DevCon" "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  32. 	ECHO.
  33. 	ECHO Install the downloaded file and make sure DEVCON.EXE is in the PATH.
  34. 	ECHO Then try again.
  35. )
  36.  
  37. :: Abort if DEVCON.EXE is not available yet
  38. IF "%DevconAvailable%"=="No" GOTO End
  39.  
  40. :: Ask for confirmation, unless overruled by command line switch
  41. IF NOT "%Confirmed%"=="Y" (
  42. 	ECHO You are about to remove all inactive PnP devices from this computer.
  43. 	SET /P Confirmed=Are you sure you want to continue? [yN] 
  44. )
  45. IF /I NOT "%Confirmed%"=="Y" GOTO Syntax
  46.  
  47. :: Save a list of all active devices in a temporary file
  48. DEVCON Find * | FIND /I /V "matching device(s)" > "%Temp%\DevconFind.txt"
  49. IF NOT EXIST "%Temp%\DevconFind.txt" GOTO Syntax
  50.  
  51. :: List all devices, both hidden/inactive and active ones, and remove
  52. :: all devices that are not listed as active in the temporary file
  53. FOR /F "tokens=1 delims=: " %%A IN ('DEVCON FindAll * ^| FIND /I /V "matching device(s)"') DO (
  54. 	TYPE "%Temp%\DevconFind.txt" | FIND "%%~A" >NUL
  55. 	IF ERRORLEVEL 1 %Debug% DEVCON Remove "@%%~A"
  56. )
  57.  
  58. :: Remove the temporary file
  59. DEL "%Temp%\DevconFind.txt"
  60.  
  61. :: Done
  62. ENDLOCAL
  63. GOTO:EOF
  64.  
  65.  
  66. :Syntax
  67. ECHO.
  68. ECHO RmHidDev.bat,  Version 1.00 for Windows 2000 / XP
  69. ECHO Use DEVCON to remove all hidden/inactive PnP devices.
  70. ECHO.
  71. ECHO Usage:  RMHIDDEV  [ /D ]  [ /Y ]
  72. ECHO.
  73. ECHO Where:  /D  Will only ECHO the removal commands instead of executing them
  74. ECHO         /Y  Will remove devices without confirmation
  75. ECHO.
  76. ECHO Notes:  [1] This batch file requires Microsoft's DEVCON.EXE, available at
  77. ECHO             http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272
  78. ECHO             You will be prompted for download if it isn't found.
  79. ECHO         [2] This batch file was developed with "ghost devices" on Ghost images
  80. ECHO             in mind. It has been tested on Windows XP Ghost images only.
  81. ECHO             I cannot guarantee flawless operation on any other PC.
  82. ECHO             Use this batch file entirely at your own risk. Make sure you have
  83. ECHO             a recent full backup available in case something might go wrong.
  84. ECHO             And remember to test, test, test and test before using it in a
  85. ECHO             production environment.
  86. ECHO.
  87. ECHO Written by Rob van der Woude
  88. ECHO http://www.robvanderwoude.com
  89.  
  90. IF "%OS%"=="Windows_NT" ENDLOCAL
  91.  

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