Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for devcon_help.vbs

(view source code of devcon_help.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrCommands, arrLines, arrPATH
  4. Dim blnFirstLink
  5. Dim i, intPos
  6. Dim objExec, objFSO, objHTMLFile, objRE, wshShell
  7. Dim strCommand, strDEVCON, strHTMLCode, strHTMLFile, strLine, strMsg, strOutput, strPATH, strScriptFile, strScriptVer, strVersion
  8.  
  9. strScriptVer = "1.00"
  10. strMsg = ""
  11.  
  12. If WScript.Arguments.Named.Count   > 0 Then Syntax
  13. If WScript.Arguments.Unnamed.Count > 1 Then Syntax
  14.  
  15. Set wshShell = CreateObject( "WScript.Shell" )
  16. Set objExec  = wshShell.Exec( "DEVCON.EXE Help" )
  17. If objExec.ExitCode <> 0 Then
  18. 	strMsg = "ERROR: DEVCON not found, not in the current directory, nor in the PATH\n\n"
  19. 	Syntax
  20. End If
  21.  
  22. Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  23.  
  24. With objFSO
  25. 	If WScript.Arguments.Unnamed.Count = 1 Then
  26. 		If .FolderExists( .GetParentFolderName( WScript.Arguments.Unnamed(0) ) ) Then
  27. 			strHTMLFile = .GetAbsolutePathName( WScript.Arguments.Unnamed(0) )
  28. 		Else
  29. 			strMsg = "ERROR: invalid path for HTML output file\n\n"
  30. 			Syntax
  31. 		End If
  32. 	Else
  33. 		strScriptFile = WScript.ScriptFullName
  34. 		strHTMLFile   = .BuildPath( .GetParentFolderName( strScriptFile ), .GetBaseName( strScriptFile ) & ".html" )
  35. 	End If
  36. End With
  37.  
  38. strDEVCON  = ""
  39. strVersion = ""
  40. strPATH    = wshShell.ExpandEnvironmentStrings( wshShell.CurrentDirectory & ";%PATH%" )
  41. arrPATH    = Split( strPATH, ";" )
  42. For i = 0 To UBound( arrPATH )
  43. 	If Trim( arrPATH(i) ) <> "" Then
  44. 		If strDEVCON = "" Then
  45. 			With objFSO
  46. 				If .FileExists( .BuildPath( arrPATH(i), "DEVCON.EXE" ) ) Then
  47. 					strDEVCON  = .BuildPath( arrPATH(i), "DEVCON.EXE" )
  48. 					strVersion = .GetFileVersion( strDEVCON )
  49. 				End If
  50. 			End With
  51. 		End If
  52. 	End If
  53. Next
  54.  
  55. Set arrCommands = CreateObject( "System.Collections.ArrayList" )
  56. strHTMLCode  = "<!DOCTYPE HTML>\n<html>\n<head>\n<title>Help for DEVCON " & strVersion & "</title>\n</head>\n"
  57. strHTMLCode  = strHTMLCode & "<body>\n\n<div style=""max-width: 800px; text-align: center; margin-left: auto; margin-right: auto;"">\n\n<div style=""max-width: 800px; text-align: left;"">\n\n<h1>help for <a href=""http://www.robvanderwoude.com/devcon.php"">DEVCON</a> " & strVersion & "</h1>\n\n<pre>"
  58. strOutput    = objExec.StdOut.ReadAll
  59. arrLines     = Split( strOutput, vbCrLf )
  60. blnFirstLink = False
  61. For i = 0 To UBound( arrLines )
  62. 	strLine = Escape( arrLines(i) )
  63. 	If InStr( arrLines(i), Space( 9 ) ) > 2 And Left( arrLines(i), 4 ) <> "help" And Left( arrLines(i), 1 ) <> "-" Then
  64. 		intPos     = InStr( strLine, " " )
  65. 		strCommand = Left( strLine, intPos - 1 )
  66. 		arrCommands.Add strCommand
  67. 		strLine = "<a href=""#helptext_" & strCommand & """>" & strCommand & "</a>" & Escape( Mid( strLine, intPos ) )
  68. 		If Not blnFirstLink Then
  69. 			strLine      = "\n" & strLine
  70. 			blnFirstLink = True
  71. 		End If
  72. 	End If
  73. 	strHTMLCode = strHTMLCode & "\n" & strLine
  74. Next
  75. arrCommands.Sort
  76. strHTMLCode = SuperTrim( strHTMLCode ) & "</pre>\n\n"
  77. strHTMLCode = strHTMLCode & "<p>Type <code>DEVCON help <em>command</em></code> on the command line for detailed help on <em>command</em>.</p>\n\n"
  78. For Each strCommand In arrCommands
  79. 	Set objExec = wshShell.Exec( "DEVCON.EXE Help " & strCommand )
  80. 	If objExec.ExitCode = 0 Then
  81. 		strHTMLCode = strHTMLCode & "<h2 id=""helptext_" & strCommand & """>DEVCON " & strCommand & "</h2>\n\n<pre>"
  82. 		strHTMLCode = strHTMLCode & SuperTrim( Escape( objExec.StdOut.ReadAll ) )
  83. 		strHTMLCode = strHTMLCode & "</pre>\n\n<p style=""text-align: center;""><a href=""" & objFSO.GetFileName( strHTMLFile ) & """>Back to the top of the page</a></p>\n\n<p>&nbsp;</p>\n\n"
  84. 	End If
  85. Next
  86.  
  87. strHTMLCode = Replace( strHTMLCode & "</div>\n\n<p>&nbsp;</p>\n\n<p style=""text-align: center;"">HTML page generated by devcon_help.vbs, Version " & strScriptVer & "<br />\nWritten by Rob van der Woude<br />\n<a href=""http://www.robvanderwoude.com"">http://www.robvanderwoude.com</a></p>\n\n</div>\n\n</body>\n</html>", "\n", vbCrLf )
  88.  
  89. Set objHTMLFile = objFSO.CreateTextFile( strHTMLFile, True, True )
  90. objHTMLFile.Write( strHTMLCode )
  91. objHTMLFile.Close
  92.  
  93. wshShell.Run strHTMLFile, 1, False
  94.  
  95. Abort 0
  96.  
  97.  
  98. Sub Abort( intRC )
  99. 	On Error Resume Next
  100. 	Set wshShell    = Nothing
  101. 	Set objExec     = Nothing
  102. 	Set objFSO      = Nothing
  103. 	Set objRE       = Nothing
  104. 	Set objHTMLFile = Nothing
  105. 	On Error Goto 0
  106. 	WScript.Quit intRC
  107. End Sub
  108.  
  109.  
  110. Function Escape( strInput )
  111. 	Dim strResult
  112. 	strResult = Replace( strInput, "&", "&amp;" )
  113. 	strResult = Replace( strResult, ">", "&gt;" )
  114. 	strResult = Replace( strResult, "<", "&lt;" )
  115. 	Escape    = strResult
  116. End Function
  117.  
  118.  
  119. Function SuperTrim( strInput )
  120. 	Dim strResult
  121. 	strResult = strInput
  122. 	Set objRE = New RegExp
  123. 	objRE.Pattern = "\s+$"
  124. 	If objRE.Test( strResult ) Then strResult = objRE.Replace( strResult, "" )
  125. 	objRE.Pattern = "^[\n\r]+"
  126. 	If objRE.Test( strResult ) Then strResult = objRE.Replace( strResult, "" )
  127. 	SuperTrim = strResult
  128. End Function
  129.  
  130.  
  131. Sub Syntax( )
  132. 	strMsg = strMsg _
  133. 	       & "devcon_help.vbs, Version " & strScriptVer _
  134. 	       & "\nCreate an HTML help page for all commands for the current DEVCON version\n\n" _
  135. 	       & "Usage:  devcon_help.vbs  [ outputfile ]\n\n" _
  136. 	       & "Where:  outputfile   is the HTML file to be created (default: name\n" _
  137. 	       & "                     and location of this script, extension "".html"")\n\n" _
  138. 	       & "Note:   DEVCON is a free Microsoft utility, part of the Windows Driver Kit:\n" _
  139. 	       & "        https://www.microsoft.com/en-us/download/details.aspx?id=11800\n" _
  140. 	       & "        See http://www.robvanderwoude.com/devcon.php for help on\n" _
  141. 	       & "        getting it installed without the entire Windows Driver Kit.\n\n" _
  142. 	       & "Written by Rob van der Woude\nhttp://www.robvanderwoude.com"
  143. 	WScript.Echo Replace( strMsg, "\n", vbCrLf )
  144. 	Abort 1
  145. End Sub
  146.  

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