Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bkalldrv.bat

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

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF "%OS%"=="Windows_NT" (SETLOCAL) ELSE (GOTO Syntax)
  4. VER | FIND "Windows NT" >NUL && GOTO Syntax
  5.  
  6. :: Check command line arguments
  7. IF NOT "%~1"=="" IF /I NOT "%~1"=="/A" GOTO Syntax
  8.  
  9. :: Check if DEVCON.EXE is available and if not, offer to download it
  10. SET DevconAvailable=
  11. SET Download=
  12. DEVCON.EXE /? >NUL 2>&1
  13. IF ERRORLEVEL 1 (
  14. 	SET DevconAvailable=No
  15. 	ECHO This batch file requires Microsoft's DEVCON utility.
  16. 	SET /P Download=Do you want to download it now? [y/N] 
  17. )
  18.  
  19. :: Start download if requested
  20. IF /I "%Download%"=="Y" (
  21. 	START "DevCon" "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  22. 	ECHO.
  23. 	ECHO Install the downloaded files and make sure DEVCON.EXE is in the PATH.
  24. 	ECHO Then try again.
  25. )
  26.  
  27. :: Abort if DEVCON.EXE is not available yet
  28. IF "%DevconAvailable%"=="No" GOTO End
  29.  
  30. :: Initialize variables
  31. SET Log="Drivers\%~n0.log"
  32. SET TmpFile="%Temp:"=%.\_DevCon.dat"
  33. SET Counter=0
  34. SET Error=0
  35.  
  36. :: Create backup "root" directory
  37. IF NOT EXIST Drivers\ MD Drivers
  38.  
  39. :: Remove old log file if it exists
  40. IF EXIST %Log% DEL %Log%
  41.  
  42. :: Display welcome message
  43. CLS
  44. ECHO Gathering info, please wait...
  45. ECHO.
  46.  
  47. :: Choose between all devices or just the active ones
  48. IF /I "%~1"=="/A" (SET FindDev=FindAll) ELSE (SET FindDev=Find)
  49.  
  50. :: Find all drivers, including inactive ones, and start backup subroutine for each one
  51. FOR /F "tokens=1* delims=: " %%A IN ('DEVCON %FindDev% * ^| FIND "\"') DO CALL :Driver "@%%A" "%%B"
  52.  
  53. :: Display summary
  54. ECHO.
  55. ECHO Backupped driver files for %Counter% devices
  56.  
  57. :: Log summary
  58. >> %Log% ECHO.
  59. >> %Log% ECHO Backupped driver files for %Counter% devices
  60.  
  61. :: Display warning message if copy errors were encountered
  62. IF %Error% GTR 0 (
  63. 	ECHO.
  64. 	IF %Error% EQU 1 (
  65. 		ECHO An error was encountered while copying,
  66. 	) ELSE (
  67. 		ECHO %Error% errors were encountered while copying,
  68. 	)
  69. 	ECHO Please check the log file %Log%
  70. )
  71.  
  72. :: Done
  73. ENDLOCAL
  74. GOTO End
  75.  
  76.  
  77. :: :: :: :: ::  Backup subroutine  :: :: :: :: ::
  78. :Driver
  79. :: Drivers without description ususally have no driver files
  80. :: defined, so abort if no description is available
  81. IF "%~2"=="" GOTO:EOF
  82. :: Remove old temporay file
  83. IF EXIST %TmpFile% DEL %TmpFile%
  84. :: Store DEVCON info in a temporary file to speed up processing
  85. DEVCON DriverFiles %1 > %TmpFile% 2>NUL
  86. :: Exit subroutine if there are no files to backup
  87. TYPE %TmpFile% | FIND ":\" >NUL
  88. IF ERRORLEVEL 1 GOTO:EOF
  89. :: Use variables that are local to the subroutine
  90. SETLOCAL
  91. :: On the first call to the subroutine, clear the screen
  92. IF %Counter% EQU 0 CLS
  93. :: Display and log which device is being processed
  94. ECHO Backing up driver for "%~2"
  95. >> %Log% ECHO Backing up driver for "%~2"
  96. :: "Escape" the device name, so it can be used as a directory name
  97. SET Name="%~2"
  98. SET Name=%Name:/=_%
  99. SET Name=%Name::=_%
  100. SET Name=%!!%
  101. SET Name=%Name:,=_%
  102. SET Name=%Name:(=[%
  103. SET Name=%Name:)=]%
  104. SET Name=%Name:&=_and_%
  105. SET Name=%Name:"=%
  106. :: Extract the "class" name from the device ID
  107. FOR /F "tokens=1 delims=\" %%K IN ('TYPE %TmpFile% ^| FIND "\" ^| FIND /V ":"') DO SET Class=%%K
  108. :: Extract the INF file name
  109. FOR /F "tokens=1 delims=[" %%K IN ('TYPE %TmpFile% ^| FIND /I "Driver installed from"') DO SET Inf=%%~K
  110. IF DEFINED Inf FOR /F "tokens=3* delims= " %%K IN ('ECHO.%Inf%') DO SET Inf=%%L
  111. :: Backup the INF file, and log and count any errors
  112. IF DEFINED Inf (
  113. 	XCOPY "%Inf:~0,-1%" "Drivers\%Class%\%Name%\*.*" /H /R /D >NUL 2>>%Log%
  114. 	IF ERRORLEVEL 1 SET /A Error = %Error% + 1
  115. )
  116. :: Backup the driver files, and log and count any errors
  117. FOR /F "tokens=* delims= " %%K IN ('TYPE %TmpFile% ^| FIND ":\" ^| FIND /I /V "Driver installed from"') DO (
  118. 	XCOPY "%%~K" "Drivers\%Class%\%Name%\*.*" /H /R /D >NUL 2>>%Log%
  119. 	IF ERRORLEVEL 1 SET /A Error = %Error% + 1
  120. )
  121. :: Increment the device counter
  122. SET /A Counter = %Counter% + 1
  123. :: Clean up
  124. IF EXIST %TmpFile% DEL %TmpFile%
  125. :: Exit the subroutine, dropping all local variables except the counters
  126. ENDLOCAL & (SET Counter=%Counter%) & (SET Error=%Error%)
  127. GOTO:EOF
  128.  
  129.  
  130. :Syntax
  131. ECHO.
  132. ECHO BkAllDrv.bat,  Version 1.06 for Windows 2000 / XP
  133. ECHO Backup all Windows device drivers, optionally including even inactive ones
  134. ECHO.
  135. ECHO Usage:  BKALLDRV  [ /A ]
  136. ECHO.
  137. ECHO Where:  /A  forces backup of drivers for both active and inactive devices
  138. ECHO             (default is active devices only)
  139. ECHO.
  140. ECHO Notes:  [1] This batch file requires Microsoft's DEVCON.EXE, available at
  141. ECHO             http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272
  142. ECHO             You will be prompted for download if it isn't found.
  143. ECHO         [2] Drivers will be backupped in a directory named Drivers, located in
  144. ECHO             the current directory.
  145. ECHO         [3] Devices with duplicate names will overwrite each other's files.
  146. ECHO         [4] If errors are encountered you will be prompted to check a log file.
  147. ECHO         [5] This batch file is much slower than its Perl or Rexx equivalents.
  148. ECHO.
  149. ECHO Written by Rob van der Woude
  150. ECHO http://www.robvanderwoude.com
  151.  
  152. :End
  153. IF "%OS%"=="Windows_NT" ENDLOCAL
  154.  

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