Option Explicit Dim arrCurrentVersion, arrLatestVersion Dim blnQuiet, blnUpdateRequired Dim i, intRC Dim colMatches, objFSO, objRE, wshShell Dim strCurrentVersion, strDownloadURL, strExecPath, strInstallFolder, strLatestVersion, strRegKey, strVersionText, strVersionURL blnQuiet = False blnUpdateRequired = False intRC = 0 If WScript.Arguments.Unnamed.Count > 0 Then Syntax Select Case WScript.Arguments.Named.Count Case 0: ' No action required Case 1: If WScript.Arguments.Named.Exists( "Q" ) Then blnQuiet = True Else Syntax End If Case Else: Syntax End Select Set objFSO = CreateObject( "Scripting.FileSystemObject" ) Set wshShell = CreateObject( "WScript.Shell" ) Set objRE = New RegExp strCurrentVersion = "" ' Read the Kindle Reader installation folder from the registry strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Amazon\Kindle\Install\InstallDir" On Error Resume Next strInstallFolder = wshShell.RegRead( strRegKey ) If Err Then ' Try again, now for 64-bit systems strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Amazon\Kindle\Install\InstallDir" strInstallFolder = wshShell.RegRead( strRegKey ) End If On Error Goto 0 ' Get the Kindle Reader executable's full path strExecPath = objFSO.BuildPath( strInstallFolder, "kindle.exe" ) ' Check if the Kindle Reader executable file can be found If objFSO.FileExists( strExecPath ) Then ' Get the Kindle Reader executable file version strCurrentVersion = objFSO.GetFileVersion( strExecPath ) End If strDownloadURL = "https://www.amazon.com/kindlepcdownload/ref=klp_hz_win" ' Read a Kindle Reader web page stating the latest available version strVersionURL = "https://www.amazon.com/gp/help/customer/display.html?nodeId=201245960" strVersionText = GetURLContent( strVersionURL ) strLatestVersion = "" ' Use a RegExp to extract the latest available version from the web page objRE.Pattern = "The latest version of Kindle for PC, (\d+(\.\d+)+), includes the following:" objRE.Global = False objRE.IgnoreCase = False If objRE.Test( strVersionText ) Then Set colMatches = objRE.Execute( strVersionText ) If colMatches.Count > 0 Then strLatestVersion = colMatches(0).SubMatches(0) End If Set colMatches = Nothing End If ' Prepare version strings for comparison arrCurrentVersion = Split( strCurrentVersion, "." ) arrLatestVersion = Split( strLatestVersion, "." ) If Not CheckIfEmpty( arrCurrentVersion ) And Not CheckIfEmpty( strLatestVersion ) Then If UBound( arrCurrentVersion ) > 0 and UBound( arrLatestVersion ) > 0 Then If UBound( arrCurrentVersion ) > UBound( arrLatestVersion ) Then ReDim Preserve arrCurrentVersion( UBound( arrLatestVersion ) ) ElseIf UBound( arrCurrentVersion ) < UBound( arrLatestVersion ) Then ReDim Preserve arrLatestVersion( UBound( arrCurrentVersion ) ) End If End If ' Check if latest version exceeds currently installed version For i = 0 To UBound( arrCurrentVersion ) If CInt( arrLatestVersion(i) ) > CInt( arrCurrentVersion(i) ) Then blnUpdateRequired = True ' assuming current version can never exceed latest version End If Next ' Display update status and start download if required If blnUpdateRequired Then ' Display installed and latest versions WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _ & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _ & "Latest available version " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _ & "An update is available, please download and install it..." intRC = 1 wshShell.Run strDownloadURL Else If Not blnQuiet Then WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _ & "Currently installed version" & vbTab & ":" & vbTab & strCurrentVersion & vbCrLf _ & "Latest available version " & vbTab & ":" & vbTab & strLatestVersion & vbCrLf & vbCrLf _ & "You have the latest version, no update required." intRC = 0 End If End If Else WScript.Echo "Kindle Reader for PC update check" & vbCrLf & vbCrLf _ & "Unable to compare versions." intRC = -1 End If Set objRE = Nothing Set wshShell = Nothing Set objFSO = Nothing Function CheckIfEmpty( myVar ) CheckIfEmpty = IsEmpty( myVar ) If IsNull( myVar ) Then CheckIfEmpty = True If IsArray( myVar ) Then If UBound( myVar ) = -1 Then CheckIfEmpty = True Else If Trim( myVar ) = "" Then CheckIfEmpty = True End If End Function Function GetURLContent( strURL ) Dim objRequest GetURLContent = "" Set objRequest = CreateObject( "Microsoft.XMLHTTP" ) objRequest.open "GET", strURL, False objRequest.send vbNull If objRequest.status = 200 Then GetURLContent = objRequest.responseText Set objRequest = Nothing End Function Sub Syntax Dim strMsg strMsg = "CheckKindleUpdate.vbs, Version 1.02" _ & vbCrLf _ & "Check if an update is available for Amazon's Kindle Reader for PC" _ & vbCrLf & vbCrLf _ & "Usage:" & vbTab & "CheckKindleUpdate.vbs [ /Q ]" _ & vbCrLf & vbCrLf _ & "Where:" & vbTab & "/Q" & vbTab & "Hides results unless an update is required" _ & vbCrLf & vbCrLf _ & "Return codes:" & vbTab & " 0" & vbTab & "the latest version is installed" _ & vbCrLf _ & " " & vbTab & vbTab & " 1" & vbTab & "an update is available" _ & vbCrLf _ & " " & vbTab & vbTab & "-1" & vbTab & "an error occurred" _ & vbCrLf & vbCrLf _ & "Written by Rob van der Woude" _ & vbCrLf _ & "http://www.robvanderwoude.com" WScript.Echo strMsg WScript.Quit 1 End Sub