Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for gconv.vbs

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

  1. Option Explicit
  2.  
  3. Dim i, strQuery
  4.  
  5. With WScript.Arguments
  6. 	If .Named.Count   > 0 Then Syntax
  7. 	If .Unnamed.Count < 4 Then Syntax
  8. 	For i = 0 To .Unnamed.Count - 1
  9. 		strQuery = strQuery & "+" & .Unnamed(i)
  10. 	Next
  11. End With
  12.  
  13. strQuery = Mid( strQuery, 2 )
  14.  
  15. WScript.Echo GoogleUnitConversion( strQuery )
  16.  
  17.  
  18. Function GoogleUnitConversion( myQuery )
  19. ' Name:       GoogleUnitConversion
  20. ' Function:   Convert units using Google Calculater and Internet Explorer
  21. ' Usage:      WScript.Echo GoogleUnitConversion( "10+cm+in+inches" )
  22. ' Returns:    10 centimeters = 3.93700787 inches
  23. '
  24. ' Written by Rob van der Woude
  25. ' http://www.robvanderwoude.com
  26. 	Dim blnTimedOut, i, j, objIE, objMatches, objRE, strAllText, strMatch, strPattern, strURL
  27. 	' Open the appropriate URL in Internet Explorer
  28. 	strURL = "http://www.google.com/search?q=" & myQuery & "&pws=0&hl=en&num=1"
  29. 	strPattern = "\)\s*" & Left( myQuery, InStr( myQuery, "+" ) -1 ) &  "\s*[^=]+\s*=\s*[\d\., ]+(\s+[a-z]+)*\s*" _
  30. 	           & Right( myQuery, Len( myQuery ) - InStrRev( myQuery, "+" ) )
  31. 	Set objIE = CreateObject( "InternetExplorer.Application" )
  32. 	objIE.Visible = False
  33. 	objIE.Navigate2 strURL
  34. 	' Wait till IE is ready
  35. 	i = 0
  36. 	blnTimedOut = False
  37. 	Do While objIE.Busy
  38. 		WScript.Sleep 100
  39. 		i = i + 1
  40. 		' Time out after 10 seconds
  41. 		If i > 100 Then
  42. 			blnTimedOut = True
  43. 			Exit Do
  44. 		End If
  45. 	Loop
  46. 	' Retrieve the URL's text
  47. 	If Not blnTimedOut Then strAllText = objIE.Document.Body.InnerText
  48. 	' Close the Internet Explorer session
  49. 	objIE.Quit
  50. 	Set objIE = Nothing
  51. 	' Use a regular expression to extract the result from the web page
  52. 	Set objRE = New RegExp
  53. 	objRE.Global = True
  54. 	objRE.IgnoreCase = True
  55. 	objRE.Pattern = strPattern
  56. 	Set objMatches = objRE.Execute( strAllText )
  57. 	j = objMatches.Count - 1
  58. 	If j < 0 Then
  59. 		strMatch = ""
  60. 	Else
  61. 		strMatch = Trim( Mid( objMatches.Item(j).Value, 2 ) )
  62. 	End If
  63. 	Set objMatches = Nothing
  64. 	Set objRE = Nothing
  65. 	GoogleUnitConversion = strMatch
  66. End Function
  67.  
  68.  
  69. Sub Syntax( )
  70. 	Dim strMsg
  71. 	strMsg = "GConv.vbs,  Version 1.10" & vbCrLf _
  72. 	       & "Convert units using Google Calculater and Internet Explorer" _
  73. 	       & vbCrLf & vbCrLf _
  74. 	       & "Usage:    CSCRIPT  //NoLogo  GCONV.VBS  number  unit  IN  new_unit" _
  75. 	       & vbCrLf & vbCrLf _
  76. 	       & "E.g:      10  cm  in  inches" _
  77. 	       & vbCrLf & vbCrLf _
  78. 	       & "Returns:  10 centimeters = 3.93700787 inches" _
  79. 	       & vbCrLf & vbCrLf _
  80. 	       & "Written by Rob van der Woude" & vbCrLf _
  81. 	       & "http://www.robvanderwoude.com"
  82. 	WScript.Echo strMsg
  83. 	WScript.Quit 1
  84. End Sub
  85.  

page last uploaded: 2017-04-06, 13:33