Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wgettxt.vbs

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

  1. Option Explicit
  2.  
  3. Dim blnSaveToFile
  4. Dim intArgs
  5. Dim objADODB, objIE
  6. Dim strFile, strText, strURL
  7.  
  8. Const ForAppending          =  8
  9. Const ForReading            =  1
  10. Const ForWriting            =  2
  11. Const TristateFalse         =  0
  12. Const TristateMixed         = -2
  13. Const TristateTrue          = -1
  14. Const TristateUseDefault    = -2
  15. Const adTypeBinary          =  1
  16. Const adTypeText            =  2
  17. Const adSaveCreateNotExist  =  1
  18. Const adSaveCreateOverWrite =  2
  19.  
  20. blnSaveToFile = False
  21.  
  22. ' Check command line arguments
  23. If WScript.Arguments.Unnamed.Count > 0 Then Syntax
  24. With WScript.Arguments.Named
  25. 	intArgs = 0
  26. 	If .Exists("U") Then
  27. 		strURL  = .Item("U")
  28. 		intArgs = intArgs + 1
  29. 	End If
  30. 	If .Exists("F") Then
  31. 		strFile = .Item("F")
  32. 		intArgs = intArgs + 1
  33. 		If Trim( strFile ) = "" Then
  34. 			strFile = WScript.ScriptFullName & "\..\WGetTxt.txt"
  35. 		End If
  36. 		blnSaveToFile = True
  37. 	End If
  38. 	If intArgs <> .Count Then Syntax
  39. 	If intArgs  =      0 Then Syntax
  40. 	If intArgs  >      2 Then Syntax
  41. End With
  42.  
  43. ' Use IE to retrieve the web page and convert it to unformated text
  44. Set objIE = CreateObject( "InternetExplorer.Application" )
  45. With objIE
  46. 	.Navigate strURL
  47. 	.Visible = False
  48. 	Do While .Busy
  49. 		WScript.Sleep 200
  50. 	Loop
  51. 	strText = .Document.Body.InnerText
  52. 	Do While .Busy
  53. 		WScript.Sleep 200
  54. 	Loop
  55. 	.Quit
  56. End With
  57. Set objIE = Nothing
  58.  
  59. If blnSaveToFile Then
  60. 	' Use ADODB stream to convert to and save as ASCII
  61. 	Set objADODB = CreateObject( "ADODB.Stream" )
  62. 	With objADODB
  63. 		.Open
  64. 		.Type = adTypeText
  65. 		.CharSet = "us-ascii"
  66. 		.WriteText strText
  67. 		.SaveToFile strFile, adSaveCreateOverWrite
  68. 		.Close
  69. 	End With
  70. 	Set objADODB = Nothing
  71. Else
  72. 	WScript.Echo strText
  73. End If
  74.  
  75. ' Help screen
  76. Sub Syntax
  77. 	Dim strMsg
  78. 	strMsg = vbcrlf _
  79. 	       & "WGetTxt.vbs,  Version 2.00" _
  80. 	       & vbCrLf _
  81. 	       & "Display only the text of a web page or save it as an ASCII text file" _
  82. 	       & vbCrLf & vbCrLf _
  83. 	       & "Usage:" & vbtab & "CSCRIPT  //NoLogo  WGETTXT.VBS  /U:""url""  [ /F:""file"" ]" _
  84. 	       & vbCrLf & vbCrLf _
  85. 	       & "Where:" & vbtab & """url"""  & vbtab & "is the URL of the web page to be retrieved" _
  86. 	       & vbCrLf _
  87. 	       & "      " & vbtab & """file""" & vbtab & "is the (optional) file where the text should be written to" _
  88. 	       & vbCrLf _
  89. 	       & "      " & vbtab & "      "   & vbtab & "(will be overwritten if it exists, or created if not)" _
  90. 	       & vbCrLf & vbCrLf _
  91. 	       & "Notes:" & vbTab & "If /F is used without a file name, the result will be written to a" _
  92. 	       & vbCrLf _
  93. 	       & "      " & vbTab & "file named WGetTxt.txt, located in this script's parent directory." _
  94. 	       & vbCrLf _
  95. 	       & "      " & vbTab & "If /F is not used, the result will be displayed on screen." _
  96. 	       & vbCrLf & vbCrLf _
  97. 	       & "Written by Rob van der Woude" _
  98. 	       & vbCrLf _
  99. 	       & "http://www.robvanderwoude.com"
  100. 	WScript.Echo strMsg
  101. 	WScript.Quit 1
  102. End Sub
  103.  

page last uploaded: 2017-07-06, 12:37