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, intRC
  6. Dim colMatches, objFSO, objRE, wshShell
  7. Dim strCurrentVersion, strDownloadURL, strExecPath, strInstallFolder, strLatestVersion, strRegKey, strVersionText, strVersionURL
  8.  
  9. blnQuiet          = False
  10. blnUpdateRequired = False
  11. intRC             = 0
  12.  
  13. If WScript.Arguments.Unnamed.Count > 0 Then Syntax
  14. Select Case WScript.Arguments.Named.Count
  15. 	Case 0:
  16. 		' No action required
  17. 	Case 1:
  18. 		If WScript.Arguments.Named.Exists( "Q" ) Then
  19. 			blnQuiet = True
  20. 		Else
  21. 			Syntax
  22. 		End If
  23. 	Case Else:
  24. 		Syntax
  25. End Select
  26.  
  27. Set objFSO   = CreateObject( "Scripting.FileSystemObject" )
  28. Set wshShell = CreateObject( "WScript.Shell" )
  29. Set objRE    = New RegExp
  30.  
  31. strCurrentVersion = ""
  32. ' Read the Kindle Reader installation folder from the registry
  33. strRegKey         = "HKEY_LOCAL_MACHINE\SOFTWARE\Amazon\Kindle\Install\InstallDir"
  34. On Error Resume Next
  35. strInstallFolder = wshShell.RegRead( strRegKey )
  36. If Err Then
  37. 	' Try again, now for 64-bit systems
  38. 	strRegKey        = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Amazon\Kindle\Install\InstallDir"
  39. 	strInstallFolder = wshShell.RegRead( strRegKey )
  40. End If
  41. On Error Goto 0
  42. ' Get the Kindle Reader executable's full path
  43. strExecPath = objFSO.BuildPath( strInstallFolder, "kindle.exe" )
  44. ' Check if the Kindle Reader executable file can be found
  45. If objFSO.FileExists( strExecPath ) Then
  46. 	' Get the Kindle Reader executable file version
  47. 	strCurrentVersion = objFSO.GetFileVersion( strExecPath )
  48. End If
  49.  
  50. strDownloadURL   = "https://www.amazon.com/kindlepcdownload/ref=klp_hz_win"
  51. ' Read a Kindle Reader web page stating the latest available version
  52. strVersionURL    = "https://www.amazon.com/gp/help/customer/display.html?nodeId=201245960"
  53. strVersionText   = GetURLContent( strVersionURL )
  54. strLatestVersion = ""
  55. ' Use a RegExp to extract the latest available version from the web page
  56. objRE.Pattern    = "The latest version of Kindle for PC, (\d+(\.\d+)+), includes the following:"
  57. objRE.Global     = False
  58. objRE.IgnoreCase = False
  59. If objRE.Test( strVersionText ) Then
  60. 	Set colMatches = objRE.Execute( strVersionText )
  61. 	If colMatches.Count > 0 Then
  62. 		strLatestVersion = colMatches(0).SubMatches(0)
  63. 	End If
  64. 	Set colMatches = Nothing
  65. End If
  66.  
  67. ' Prepare version strings for comparison
  68. arrCurrentVersion = Split( strCurrentVersion, "." )
  69. arrLatestVersion  = Split( strLatestVersion,  "." )
  70. If Not CheckIfEmpty( arrCurrentVersion ) And Not CheckIfEmpty( strLatestVersion ) Then
  71. 	If UBound( arrCurrentVersion ) > 0 and UBound( arrLatestVersion ) > 0 Then
  72. 		If UBound( arrCurrentVersion ) > UBound( arrLatestVersion ) Then
  73. 			ReDim Preserve arrCurrentVersion( UBound( arrLatestVersion ) )
  74. 		ElseIf UBound( arrCurrentVersion ) < UBound( arrLatestVersion ) Then
  75. 			ReDim Preserve arrLatestVersion( UBound( arrCurrentVersion ) )
  76. 		End If
  77. 	End If
  78. 	' Check if latest version exceeds currently installed version
  79. 	For i = 0 To UBound( arrCurrentVersion )
  80. 		If CInt( arrLatestVersion(i) ) > CInt( arrCurrentVersion(i) ) Then
  81. 			blnUpdateRequired = True ' assuming current version can never exceed latest version
  82. 		End If
  83. 	Next
  84. 	' Display update status and start download if required
  85. 	If blnUpdateRequired Then
  86. 		' Display installed and latest versions
  87. 		WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _
  88. 		           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  89. 		           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  90. 		           & "An update is available, please download and install it..."
  91. 		intRC = 1
  92. 		wshShell.Run strDownloadURL
  93. 	Else
  94. 		If Not blnQuiet Then
  95. 			WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _
  96. 			           & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _
  97. 			           & "Latest available version   " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _
  98. 			           & "You have the latest version, no update required."
  99. 			intRC = 0
  100. 		End If
  101. 	End If
  102. Else
  103. 	WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _
  104. 	           & "Unable to compare versions."
  105. 	intRC = -1
  106. End If
  107.  
  108. Set objRE    = Nothing
  109. Set wshShell = Nothing
  110. Set objFSO   = Nothing
  111.  
  112.  
  113. Function CheckIfEmpty( myVar )
  114. 	CheckIfEmpty = IsEmpty( myVar )
  115. 	If IsNull( myVar )  Then CheckIfEmpty = True
  116. 	If IsArray( myVar ) Then
  117. 		If UBound( myVar ) = -1 Then CheckIfEmpty = True
  118. 	Else
  119. 		If Trim( myVar ) = "" Then CheckIfEmpty = True
  120. 	End If
  121. End Function
  122.  
  123.  
  124. Function GetURLContent( strURL )
  125. 	Dim objRequest
  126. 	GetURLContent = ""
  127. 	Set objRequest = CreateObject( "Microsoft.XMLHTTP" )
  128. 	objRequest.open "GET", strURL, False
  129. 	objRequest.send vbNull
  130. 	If objRequest.status = 200 Then GetURLContent = objRequest.responseText
  131. 	Set objRequest = Nothing
  132. End Function
  133.  
  134.  
  135. Sub Syntax
  136. 	Dim strMsg
  137. 	strMsg = "CheckKindleUpdate.vbs,  Version 1.02" _
  138. 	       & vbCrLf _
  139. 	       & "Check if an update is available for Amazon's Kindle Reader for PC" _
  140. 	       & vbCrLf & vbCrLf _
  141. 	       & "Usage:" & vbTab & "CheckKindleUpdate.vbs  [ /Q ]" _
  142. 	       & vbCrLf & vbCrLf _
  143. 	       & "Where:" & vbTab & "/Q" & vbTab & "Hides results unless an update is required" _
  144. 	       & vbCrLf & vbCrLf _
  145. 	       & "Return codes:" & vbTab & " 0" & vbTab & "the latest version is installed" _
  146. 	       & vbCrLf _
  147. 	       & "     " & vbTab & vbTab & " 1" & vbTab & "an update is available" _
  148. 	       & vbCrLf _
  149. 	       & "     " & vbTab & vbTab & "-1" & vbTab & "an error occurred" _
  150. 	       & vbCrLf & vbCrLf _
  151. 	       & "Written by Rob van der Woude" _
  152. 	       & vbCrLf _
  153. 	       & "http://www.robvanderwoude.com"
  154. 	WScript.Echo strMsg
  155. 	WScript.Quit 1
  156. End Sub
  157.  

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