Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bkalldrv.kix

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

  1. ; Is help requested?
  2. If $help
  3. 	Syntax( )
  4. 	Exit 1
  5. EndIf
  6.  
  7. ; Enable Ctrl+Break
  8. Break On
  9.  
  10. ; Check If inactive drivers should be backuped too
  11. If $all
  12. 	$finddev = "FindAll"
  13. Else
  14. 	$finddev = "Find"
  15. EndIf
  16.  
  17. ; Check If DEVCON is available, if not prompt for download
  18. Shell "%COMSPEC% /C DEVCON.EXE /? >NUL 2>&1"
  19. If @Error
  20. 	? "This batch file requires Microsoft's DEVCON utility."
  21. 	? "Do you want to download it now? [y/N] "
  22. 	Get $answer
  23. 	If $answer = "Y"
  24. 		Run '%COMSPEC% /C START "" "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"'
  25. 		?
  26. 		? "Install the downloaded files and make sure DEVCON.EXE is in the PATH."
  27. 		? "Then try again."
  28. 	EndIf
  29. 	Exit 1
  30. EndIf
  31.  
  32. ; Initialize variables
  33. $tmpfile   = "%Temp%.\" + SubStr( @ScriptName, 1, Len( @ScriptName ) - 4 ) + ".dat"
  34. $tmpfil2   = "%Temp%.\" + SubStr( @ScriptName, 1, Len( @ScriptName ) - 4 ) + ".da2"
  35. $devcount  = 0
  36. $filecount = 0
  37. $errors    = 0
  38.  
  39. ; Create backup "root" directory
  40. If Exist( "@CurDir\Drivers" ) = 0
  41. 	MD "@CurDir\Drivers"
  42. EndIf
  43.  
  44. ; Display welcome message
  45. ? "Gathering info, please wait..." ?
  46.  
  47. ; Store a list of all devices
  48. Shell '%COMSPEC% /C DEVCON.EXE FindAll * | FIND.EXE "\" > $tmpfile 2>&1'
  49.  
  50. CLS
  51.  
  52. ; Process the list of devices
  53. If Open( 3, $tmpfile ) = 0
  54. 	$line = ReadLine( 3 )
  55. 	While @Error = 0
  56. 		If InStr( $line, ":" )
  57. 			$hwid = Trim( SubStr( $line, 1, InStr( $line, ":" ) - 1 ) )
  58. 			$name = Trim( SubStr( $line, InStr( $line, ":" ) + 1 ) )
  59. 			$tdir = Esc( $name )
  60. 			$x = GetDevice( $hwid, $name, $tdir )
  61. 		EndIf
  62. 		$line = ReadLine( 3 )
  63. 	Loop
  64. 	$x = Close( 3 )
  65. Else
  66. 	"Error opening $tmpfile" ?
  67. 	"    @SError" ?
  68. 	Exit 1
  69. EndIf
  70.  
  71. ? "Backuped $filecount files for $devcount devices." ?
  72. If $errors
  73. 	? "$errors errors were encountered while copying"
  74. 	? "Check the screen output for error messages..."
  75. EndIf
  76.  
  77. ; Clean up
  78. If Exist( $tmpfile )
  79. 	DEL $tmpfile
  80. EndIf
  81. If Exist( $tmpfil2 )
  82. 	DEL $tmpfil2
  83. EndIf
  84.  
  85.  
  86. Function GetDevice( $myDevID, $myDevName, $myDevDir )
  87. 	$class = SubStr( $myDevID, 1, InStr( $myDevID, "\" ) - 1 )
  88. 	Shell '%COMSPEC% /C DEVCON.EXE DriverFiles "@@$myDevID" > $tmpfil2 2>&1'
  89. 	If Open( 4, $tmpfil2 ) = 0
  90. 		$line = ReadLine( 4 )
  91. 		While @Error = 0
  92. 			If InStr( $line, ":\" )
  93. 				"Backing up driver for $myDevName" ?
  94. 				$devcount = $devcount + 1
  95. 				; Create "class" directory
  96. 				If Exist( "@CurDir\Drivers\$class" ) = 0
  97. 					MD "@CurDir\Drivers\$class"
  98. 				EndIf
  99. 				; INF file
  100. 				If Left( $line, 25 ) = "    Driver installed from"
  101. 					$inf = SubStr( $line, 27, InStr( $line, " [" ) - 27 )
  102. 					; Create driver directory
  103. 					If Exist( "@CurDir\Drivers\$class\$myDevDir" ) = 0
  104. 						MD "@CurDir\Drivers\$class\$myDevDir"
  105. 					EndIf
  106. 					; Copy INF file to driver directory
  107. 					COPY "$inf" "@CurDir\Drivers\$class\$myDevDir" /H
  108. 					$filecount = $filecount + 1
  109. 				EndIf
  110. 				; Driver file
  111. 				If Left( $line, 8 ) = "        "
  112. 					$file = SubStr( $line, 9 )
  113. 					If Exist( "@CurDir\Drivers\$class\$myDevDir" ) = 0
  114. 						MD "@CurDir\Drivers\$class\$myDevDir"
  115. 					EndIf
  116. 					; Copy INF file to driver directory
  117. 					COPY "$file" "@CurDir\Drivers\$class\$myDevDir" /H
  118. 					$filecount = $filecount + 1
  119. 				EndIf
  120. 			EndIf
  121. 			$line = ReadLine( 4 )
  122. 		Loop
  123. 		$x = Close( 4 )
  124. 	Else
  125. 		"Error backing up $myDevName" ?
  126.                 "    @SError" ?
  127. 		$errors = $errors + 1
  128. 	EndIf
  129. 	$GetDevice = ""
  130. EndFunction
  131.  
  132.  
  133. Function Esc( $myStr )
  134. 	While InStr( $myStr, "/" )
  135. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, "/" ) - 1 ) + "_" + SubStr( $myStr, InStr( $myStr, "/" ) + 1 )
  136. 	Loop
  137. 	While InStr( $myStr, "," )
  138. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, "," ) - 1 ) + " " + SubStr( $myStr, InStr( $myStr, "," ) + 1 )
  139. 	Loop
  140. 	While InStr( $myStr, ";" )
  141. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, ";" ) - 1 ) + " " + SubStr( $myStr, InStr( $myStr, ";" ) + 1 )
  142. 	Loop
  143. 	While InStr( $myStr, "(" )
  144. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, "(" ) - 1 ) + "[" + SubStr( $myStr, InStr( $myStr, "(" ) + 1 )
  145. 	Loop
  146. 	While InStr( $myStr, ")" )
  147. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, ")" ) - 1 ) + "]" + SubStr( $myStr, InStr( $myStr, ")" ) + 1 )
  148. 	Loop
  149. 	While InStr( $myStr, "&" )
  150. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, "&" ) - 1 ) + " and " + SubStr( $myStr, InStr( $myStr, "&" ) + 1 )
  151. 	Loop
  152. 	While InStr( $myStr, "  " )
  153. 		$myStr = SubStr( $myStr, 1, InStr( $myStr, "  " ) - 1 ) + SubStr( $myStr, InStr( $myStr, "  " ) + 1 )
  154. 	Loop
  155. 	$Esc = $myStr
  156. EndFunction
  157.  
  158.  
  159. Function Syntax( )
  160. 	? "BkAllDrv.kix,  Version 1.00"
  161. 	? "Backup all Windows device drivers, optionally including even inactive ones" ?
  162. 	? "Usage:  KIX32.EXE  BKALLDRV.KIX  [ $$All=1 ]  [ $$Help=1 ]" ?
  163. 	? "Where:  $$All  forces backup of drivers for both active and inactive devices"
  164. 	? "              (default is active devices only)"
  165. 	? "        $$Help displays this help screen" ?
  166. 	? "Notes:  [1]   This KiXtart script requires Microsoft's DEVCON.EXE, available at"
  167. 	? "              http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  168. 	? "              You will be prompted for download if it isn't found."
  169. 	? "        [2]   Drivers will be backupped in a directory named Drivers, located"
  170. 	? "              in the current directory."
  171. 	? "        [3]   Devices with duplicate names will overwrite each other's files." ?
  172. 	? "Written by Rob van der Woude"
  173. 	? "http://www.robvanderwoude.com" ?
  174. EndFunction
  175.  

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