Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for bkalldrv.rex

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

  1. /* Backup all Windows drivers with DEVCON */
  2.  
  3. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  4. /* Sample DEVCON output:                                                               */
  5. /*                                                                                     */
  6. /* PCI\VEN_127A&DEV_1035&SUBSYS_1035127A&REV_08\4&11CD5334&0&48F0                      */
  7. /*     Name: CXT1035 - HCF PCI Modem                                                   */
  8. /*     Driver installed from c:\winnt\inf\oem8.inf [Modem]. 2 file(s) used by driver:  */
  9. /*         C:\WINNT\system32\DRIVERS\winachcf.sys                                      */
  10. /*         C:\WINNT\system32\cxt1035.cty                                               */
  11. /* 1 matching device(s) found.                                                         */
  12. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  13.  
  14. /* Hide command output and error codes */
  15. "@ECHO OFF"
  16. Trace Off
  17.  
  18. /* Initialize variables */
  19. error = 0
  20.  
  21. /* Check OS type (should be 32 bits Windows)*/
  22. Parse Upper Source os .
  23. If os <> "WIN32" Then Call Syntax
  24.  
  25. /* Check command line arguments */
  26. Parse Arg test
  27. If test <> "" Then Call Syntax
  28.  
  29. /* Initialize RexxUtil library */
  30. If RxFuncQuery( "sysloadfuncs" ) <> 0 Then Do
  31. 	initRc = RxFuncAdd( "sysloadfuncs", "RexxUtil", "sysloadfuncs" )
  32. 	If initRc = 0 Then Do
  33. 		Call sysloadfuncs
  34. 	End
  35. 	Else Do
  36. 		Say
  37. 		Say "Error "||initRc||" while trying to initialize RegUtil (RexxUtil)"
  38. 		Say
  39. 		Say "This script requires Patrick TJ McPhee's RegUtil library,"
  40. 		Say "available at http://www.interlog.com/~ptjm/"
  41. 		Say
  42. 		Say "Do yo want to download it now? [y/N]"
  43. 		Say
  44. 		download = Translate( SysGetKey( "NOECHO" ) )
  45. 		If download = "Y" Then Do
  46. 			Address SYSTEM "http://www.interlog.com/~ptjm/"
  47. 			Say "Install the downloaded library file and try again."
  48. 		End
  49. 		Else Do
  50. 			Say "Download and install the library and try again."
  51. 		End
  52. 		Exit 1
  53. 	End
  54. End
  55.  
  56. /* Check DEVCON's availability */
  57. Address SYSTEM "DEVCON.EXE /? >NUL 2>&1"
  58. If rc <> 0 Then Do
  59. 	Say
  60. 	Say "This script requires Microsoft's DEVCON.EXE, available at"
  61. 	Say "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  62. 	Say
  63. 	Say "Do yo want to download it now? [y/N]"
  64. 	Say
  65. 	download = Translate( SysGetKey( "NOECHO" ) )
  66. 	If download = "Y" Then Do
  67. 		Address SYSTEM "http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  68. 		Say "Install the downloaded file and try again."
  69. 	End
  70. 	Else Do
  71. 		Say "Download the tool and try again."
  72. 	End
  73. 	Exit 1
  74. End
  75.  
  76. /* Use DEVCON to retrieve info, and store it in an array */
  77. Address SYSTEM 'DEVCON.EXE DriverFiles * 2>NUL | RXQUEUE'
  78. empty = 0
  79. Do i = 1 By 1 Until Queued( ) = 0
  80. 	Parse Pull line.i
  81. 	line.i = Strip( line.i, "B", " " )
  82. 	If line.i = "" Then Do
  83. 		empty = empty + 1
  84. 		if empty > 5 Then Leave
  85. 	End
  86. 	line.0 = i
  87. End
  88.  
  89. /* Interpret DEVCON's output line by line */
  90. nextLine  = "ID"
  91. fileCount = 0
  92. driver.   = ""
  93. driver.0  = 0
  94. j = 0
  95. Do i = 1 By 1 To line.0
  96. 	Select
  97. 		When Pos( "matching device(s) found.", line.i ) > 0 Then Leave i       /* Last line of output   */
  98. 		When nextLine = "ID" Then Do                                           /* First line for driver */
  99. 			Parse Value line.i With root"\"str1"&".
  100. 			If str1 <> "" Then Do
  101. 				j = j + 1
  102. 				driver.0       = j
  103. 				driver.j.0     = 0
  104. 				driver.j.class = root
  105. 				nextLine       = "Name"
  106. 			End
  107. 		End
  108. 		When nextLine = "Name" Then Do                                         /* Line with device name */
  109. 			Parse Value line.i With "Name: "drvname
  110. 			If drvname <> "" Then Do
  111. 				k             = 0
  112. 				nextLine      = "inf"
  113. 				driver.j.name = EscName( drvname )
  114. 			End
  115. 		End
  116. 		When nextLine = "inf" Then Do                                          /* Line with inf file name */
  117. 			Parse Value line.i With "Driver installed from "inf" ["key"]. "numFiles" file"s" used by driver:"
  118. 			If numFiles <> "" Then Do
  119. 				k          = k + 1
  120. 				driver.j.k = inf
  121. 				driver.j.0 = k
  122. 				nextLine   = "files"
  123. 				If Translate( numFiles ) = "NO" Then Do
  124. 					numFiles = 0
  125. 					nextLine = "ID"
  126. 				End
  127. 			End
  128. 		End
  129. 		When nextLine = "files" Then Do                                        /* Actual driver files */
  130. 			Parse Value line.i With file
  131. 			If file <> "" Then Do
  132. 				If k = numFiles Then nextLine = "ID"
  133. 				k          = k + 1
  134. 				driver.j.k = file
  135. 				driver.j.0 = k
  136. 			End
  137. 		End
  138. 		Otherwise Nop
  139. 	End
  140. End
  141.  
  142. /* Create a target "root" directory */
  143. If SysMkDir( "Drivers" ) <> 0 Then Do
  144. 	Call SysFileTree "Drivers", "exist.", "D"
  145. 	If exist.0 <> 1 Then Do
  146. 		Say "Error creating directory Drivers"
  147. 		Say
  148. 		Say "Aborting..."
  149. 		Exit 1
  150. 	End
  151. End
  152.  
  153. /* Create "class" directories */
  154. Do i = 1 By 1 To driver.0
  155. 	/* Create a subdirectory with the device class */
  156. 	If SysMkDir( "Drivers\"||driver.i.class ) <> 0 Then Do
  157. 		Call SysFileTree "Drivers\"||driver.i.class, "exist.", "D"
  158. 		If exist.0 <> 1 Then Do
  159. 			Say "Error creating directory Drivers\"||driver.i.class
  160. 			Say
  161. 			Say "Aborting..."
  162. 			Exit 1
  163. 		End
  164. 	End
  165. End
  166.  
  167. /* Create individual driver directories */
  168. Do i = 1 By 1 To driver.0
  169. 	/* Create a subdirectory with the device name */
  170. 	If SysMkDir( "Drivers\"||driver.i.class||"\"||driver.i.name ) <> 0 Then Do
  171. 		Call SysFileTree "Drivers\"||driver.i.class||"\"||driver.i.name, "exist.", "D"
  172. 		If exist.0 <> 1 Then Do
  173. 			Say "Error creating directory Drivers\"||driver.i.class||"\"||driver.i.name
  174. 			Say
  175. 			Say "Aborting..."
  176. 			Exit 1
  177. 		End
  178. 	End
  179. End
  180.  
  181. /* Copy the driver files based on the values stored in the array */
  182. Do i = 1 By 1 To driver.0
  183. 	Say "Backing up drivers for "||driver.i.name
  184. 	Do j = 1 To driver.i.0
  185. 		driverFile = driver.i.j
  186. 		Do Until driverFile = ""
  187. 			Parse Value driverFile With dummy"\"driverFile
  188. 		End
  189. 		driverFile = dummy
  190. 		copyRc = SysCopyObject( driver.i.j, "Drivers\"||driver.i.class||"\"||driver.i.name||"\"||driverFile )
  191. 		If copyRc <> 0 Then Do
  192. 			Say "Error "||copyRc||" while copying "||driver.i.j
  193. 			error = 1
  194. 		End
  195. 	End
  196. End
  197.  
  198. /* Summarize copy errors */
  199. If error = 1 Then Do
  200. 	Say
  201. 	Say "Some error(s) occurred while copying files,"
  202. 	Say "check screen output for error messages..."
  203. End
  204.  
  205. /* A goodbye message */
  206. Say
  207. Say "Your drivers are now stored in the directory Drivers"
  208.  
  209. /* Done */
  210. Exit error
  211.  
  212.  
  213. EscName: procedure
  214. 	namestr = Arg( 1 )
  215. 	namestr = Translate( namestr, "",  "?" )
  216. 	namestr = Translate( namestr, "",  ":" )
  217. 	namestr = Translate( namestr, " ",  ";" )
  218. 	namestr = Translate( namestr, " ", "," )
  219. 	namestr = Translate( namestr, "_",  "|" )
  220. 	namestr = Translate( namestr, "_",  "/" )
  221. 	namestr = Translate( namestr, "_",  "\" )
  222. 	namestr = Translate( namestr, "[", "(" )
  223. 	namestr = Translate( namestr, "]", ")" )
  224. 	namestr = Translate( namestr, " and ", "&" )
  225. 	namestr = Translate( namestr, " ", "  " )
  226. Return namestr
  227.  
  228.  
  229. Syntax:
  230. 	Say
  231. 	Say "BkAllDrv.rex,  Version 1.02 for Windows 2000 / XP"
  232. 	Say "Backup all Windows device drivers"
  233. 	Say
  234. 	Say "Usage:  BKALLDRV.REX"
  235. 	Say
  236. 	Say "Note:   This script requires Microsoft's DEVCON.EXE, available at"
  237. 	Say "        http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q311272"
  238. 	Say "        Also required is Patrick TJ McPhee's RexxUtil,"
  239. 	Say "        available as RegUtil at http://www.interlog.com/~ptjm/"
  240. 	Say "        You will be prompted for download if either isn't found."
  241. 	Say
  242. 	Say "Written by Rob van der Woude"
  243. 	Say "http://www.robvanderwoude.com"
  244. 	Exit 1
  245. Return
  246.  

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