Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for checkkindleupdate.vbs

(view source code of checkkindleupdate.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 Kindle Reader installation folder from the registry
  30. strRegKey         = "HKEY_LOCAL_MACHINE\SOFTWARE\Amazon\Kindle\Install\InstallDir"
  31. On Error Resume Next
  32. strInstallFolder = wshShell.RegRead( strRegKey )
  33. If Err Then
  34. 	' Try again, now for 64-bit systems
  35. 	strRegKey        = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Amazon\Kindle\Install\InstallDir"
  36. 	strInstallFolder = wshShell.RegRead( strRegKey )
  37. End If
  38. On Error Goto 0
  39. ' Get the Kindle Reader executable's full path
  40. strExecPath = objFSO.BuildPath( strInstallFolder, "kindle.exe" )
  41. ' Check if the Kindle Reader executable file can be found
  42. If objFSO.FileExists( strExecPath ) Then
  43. 	' Get the Kindle Reader executable file version
  44. 	strCurrentVersion = objFSO.GetFileVersion( strExecPath )
  45. End If
  46.  
  47. strDownloadURL   = "https://www.amazon.com/kindlepcdownload/ref=klp_hz_win"
  48. ' Read a Kindle Reader web page stating the latest available version
  49. strVersionURL    = "https://www.amazon.com/gp/help/customer/display.html?nodeId=201245960"
  50. strVersionText   = GetURLContent( strVersionURL )
  51. strLatestVersion = ""
  52. ' Use a RegExp to extract the latest available version from the web page
  53. objRE.Pattern    = "The latest version of Kindle for PC, (\d+\.\d+\.\d+), includes the following:"
  54. objRE.Global     = False
  55. objRE.IgnoreCase = False
  56. If objRE.Test( strVersionText ) Then
  57. 	Set colMatches = objRE.Execute( strVersionText )
  58. 	If colMatches.Count > 0 Then
  59. 		strLatestVersion = colMatches(0).SubMatches(0)
  60. 	End If
  61. 	Set colMatches = Nothing
  62. End If
  63.  
  64. ' Prepare version strings for comparison
  65. arrCurrentVersion = Split( strCurrentVersion, "." )
  66. arrLatestVersion  = Split( strLatestVersion,  "." )
  67. If UBound( arrCurrentVersion ) > 0 and UBound( arrLatestVersion ) > 0 Then
  68. 	If UBound( arrCurrentVersion ) > UBound( arrLatestVersion ) Then
  69. 		ReDim Preserve arrCurrentVersion( UBound( arrLatestVersion ) )
  70. 	ElseIf UBound( arrCurrentVersion ) < UBound( arrLatestVersion ) Then
  71. 		ReDim Preserve arrLatestVersion( UBound( arrCurrentVersion ) )
  72. 	End If
  73. End If
  74. ' Check if latest version exceeds currently installed version
  75. blnUpdateRequired = False
  76. For i = 0 To UBound( arrCurrentVersion )
  77. 	If CInt( arrLatestVersion(i) ) > CInt( arrCurrentVersion(i) ) Then
  78. 		blnUpdateRequired = True ' assuming current version can never exceed latest version
  79. 	End If
  80. Next
  81. ' Display update status and start download if required
  82. If blnUpdateRequired Then
  83. 	' Display installed and latest versions
  84. 	WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _
  85. 	           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  86. 	           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  87. 	           & "An update is available, please download and install it..."
  88. 	wshShell.Run strDownloadURL
  89. Else
  90. 	If Not blnQuiet Then
  91. 		WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _
  92. 		           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  93. 		           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  94. 		           & "You have the latest version, no update required."
  95. 	End If
  96. End If
  97.  
  98. Set objRE    = Nothing
  99. Set wshShell = Nothing
  100. Set objFSO   = Nothing
  101.  
  102.  
  103. Function GetURLContent( strURL )
  104. 	Dim objRequest
  105. 	GetURLContent = ""
  106. 	Set objRequest = CreateObject( "Microsoft.XMLHTTP" )
  107. 	objRequest.open "GET", strURL, False
  108. 	objRequest.send vbNull
  109. 	If objRequest.status = 200 Then GetURLContent = objRequest.responseText
  110. 	Set objRequest = Nothing
  111. End Function
  112.  
  113.  
  114. Sub Syntax
  115. 	Dim strMsg
  116. 	strMsg = "CheckKindleUpdate.vbs,  Version 1.00" _
  117. 	       & vbCrLf _
  118. 	       & "Check if an update is available for Amazon's Kindle Reader for PC" _
  119. 	       & vbCrLf & vbCrLf _
  120. 	       & "Usage:" & vbTab & "CheckKindleUpdate.vbs  [ /Q ]" _
  121. 	       & vbCrLf & vbCrLf _
  122. 	       & "Where:" & vbTab & "/Q" & vbTab & "Hides results unless an update is required" _
  123. 	       & vbCrLf & vbCrLf _
  124. 	       & "Written by Rob van der Woude" _
  125. 	       & vbCrLf _
  126. 	       & "http://www.robvanderwoude.com"
  127. 	WScript.Echo strMsg
  128. 	WScript.Quit 1
  129. End Sub
  130.  

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