Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for checkcalibreupdate.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrCurrentVersion, arrLatestVersion
  4. Dim blnQuiet, blnUpdateRequired
  5. Dim i
  6. Dim colMatches, objFSO, objRE, wshShell
  7. Dim strCurrentVersion, strDownloadURL, strExecPath, strInstallFolder, strLatestVersion, strRegKey, strVersionText, strVersionURL
  8.  
  9. blnQuiet = False
  10. If WScript.Arguments.Unnamed.Count > 0 Then Syntax
  11. Select Case WScript.Arguments.Named.Count
  12. 	Case 0:
  13. 		' No action required
  14. 	Case 1:
  15. 		If WScript.Arguments.Named.Exists( "Q" ) Then
  16. 			blnQuiet = True
  17. 		Else
  18. 			Syntax
  19. 		End If
  20. 	Case Else:
  21. 		Syntax
  22. End Select
  23.  
  24. Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  25. Set wshShell = CreateObject( "WScript.Shell" )
  26. Set objRE    = New RegExp
  27.  
  28. strCurrentVersion = ""
  29. ' Read the Calibre installation folder from the registry
  30. strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\calibre 64bit\Installer\InstallPath"
  31. On Error Resume Next
  32. strInstallFolder = wshShell.RegRead( strRegKey )
  33. On Error Goto 0
  34. ' Get the Calibre executable's full path
  35. strExecPath = objFSO.BuildPath( strInstallFolder, "calibre.exe" )
  36. ' Check if the Calibre executable file can be found
  37. If objFSO.FileExists( strExecPath ) Then
  38. 	' Get the Calibre file version
  39. 	strCurrentVersion = objFSO.GetFileVersion( strExecPath )
  40. End If
  41.  
  42. strDownloadURL   = "https://calibre-ebook.com/dist/win64"
  43. ' Read a Calibre web page stating the latest available version
  44. strVersionURL    = "https://calibre-ebook.com/download_windows64"
  45. strVersionText   = GetURLContentIE( strVersionURL )
  46. strLatestVersion = ""
  47. ' Use a RegExp to extract the latest available version from the web page
  48. objRE.Pattern    = "<a[^>]*>\s*<img [^>]+>\s*Download\s+calibre\s+64bit\s*</a><br[^>]*>Version:\s+(\d+\.\d+\.\d+)\s*<a[^>]*>What's new</a>"
  49. objRE.Global     = False
  50. objRE.IgnoreCase = False
  51. If objRE.Test( strVersionText ) Then
  52. 	Set colMatches = objRE.Execute( strVersionText )
  53. 	If colMatches.Count > 0 Then
  54. 		strLatestVersion = colMatches(0).SubMatches(0)
  55. 	End If
  56. 	Set colMatches = Nothing
  57. End If
  58.  
  59. ' Prepare version strings for comparison
  60. arrCurrentVersion = Split( strCurrentVersion, "." )
  61. arrLatestVersion  = Split( strLatestVersion,  "." )
  62. If UBound( arrCurrentVersion ) > 0 and UBound( arrLatestVersion ) > 0 Then
  63. 	If UBound( arrCurrentVersion ) > UBound( arrLatestVersion ) Then
  64. 		ReDim Preserve arrCurrentVersion( UBound( arrLatestVersion ) )
  65. 	ElseIf UBound( arrCurrentVersion ) < UBound( arrLatestVersion ) Then
  66. 		ReDim Preserve arrLatestVersion( UBound( arrCurrentVersion ) )
  67. 	End If
  68. End If
  69. ' Check if latest version exceeds currently installed version
  70. blnUpdateRequired = False
  71. For i = 0 To Min( UBound( arrCurrentVersion ), UBound( arrLatestVersion ) )
  72. 	If CInt( arrLatestVersion(i) ) > CInt( arrCurrentVersion(i) ) Then
  73. 		blnUpdateRequired = True ' assuming current version can never exceed latest version
  74. 	End If
  75. Next
  76. ' Display update status and start download if required
  77. If blnUpdateRequired Then
  78. 	' Display installed and latest versions
  79. 	WScript.Echo "Calibre 64 bit update check" & vbCrLf & vbCrLf _
  80. 	           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  81. 	           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  82. 	           & "An update is available, please download and install it..."
  83. 	wshShell.Run strDownloadURL
  84. Else
  85. 	If Not blnQuiet Then
  86. 		WScript.Echo "Calibre 64 bit update check" & vbCrLf & vbCrLf _
  87. 		           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  88. 		           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  89. 		           & "You have the latest version, no update required."
  90. 	End If
  91. End If
  92.  
  93. Set objRE    = Nothing
  94. Set wshShell = Nothing
  95. Set objFSO   = Nothing
  96.  
  97.  
  98. ' Using IE because WinHTTP and XMLHTTP get 405 "not allowed" response
  99. Function GetURLContentIE( strURL )
  100.     Dim blnTimedOut, i, objIE, objMatch, objRE, strText
  101.     ' Return value if IP address couldn't be retrieved
  102.     GetURLContentIE = ""
  103.     ' Open the appropriate URL in Internet Explorer
  104.     Set objIE = CreateObject( "InternetExplorer.Application" )
  105.     objIE.Visible = False
  106.     objIE.Navigate2 strURL
  107.     ' Wait till IE is ready
  108.     i = 0
  109.     blnTimedOut = False
  110.     Do While objIE.Busy
  111.         WScript.Sleep 100
  112.         i = i + 1
  113.         ' Time out after 10 seconds
  114.         If i > 100 Then
  115.             blnTimedOut = True
  116.             Exit Do
  117.         End If
  118.     Loop
  119.     ' Retrieve the URL's text
  120.     If Not blnTimedOut Then GetURLContentIE = objIE.Document.Body.innerHTML
  121.     ' Close the Internet Explorer session
  122.     objIE.Quit
  123.     Set objIE = Nothing
  124. End Function
  125.  
  126.  
  127. Function Min( num1, num2 )
  128. 	If num1 < num2 Then
  129. 		Min = num1
  130. 	Else
  131. 		Min = num2
  132. 	End If
  133. End Function
  134.  
  135.  
  136. Sub Syntax
  137. 	Dim strMsg
  138. 	strMsg = "CheckCalibreUpdate.vbs,  Version 1.01" _
  139. 	       & vbCrLf _
  140. 	       & "Check if an update is available for Calibre eBook Management" _
  141. 	       & vbCrLf & vbCrLf _
  142. 	       & "Usage:" & vbTab & "CheckCalibreUpdate.vbs  [ /Q ]" _
  143. 	       & vbCrLf & vbCrLf _
  144. 	       & "Where:" & vbTab & "/Q" & vbTab & "Hides results unless an update is required" _
  145. 	       & vbCrLf & vbCrLf _
  146. 	       & "Written by Rob van der Woude" _
  147. 	       & vbCrLf _
  148. 	       & "http://www.robvanderwoude.com"
  149. 	WScript.Echo strMsg
  150. 	WScript.Quit 1
  151. End Sub
  152.  

page last uploaded: 2017-08-21, 14:26