(view source code of updatecheck.vbs as plain text)
Option Explicit
Dim arrAllProgs
Dim blnAll, blnList, blnPopups, blnVerbose
Dim i, intValidArgs
Dim objFSO
Dim strINI, strProg
Dim urlDownload
' Check command line arguments
With WScript.Arguments
intValidArgs = 0
blnAll = False
blnList = False
blnPopups = True
blnVerbose = False
If .Unnamed.Count > 0 Then
strProg = .Unnamed(0)
intValidArgs = intValidArgs + 1
End If
If .Named.Exists( "A" ) Or .Named.Exists( "All" ) Then
blnAll = True
intValidArgs = intValidArgs + 1
End If
If .Named.Exists( "L" ) Or .Named.Exists( "List" ) Then
If .Count <> 1 Then Syntax
blnList = True
intValidArgs = intValidArgs + 1
End If
If .Named.Exists( "NP" ) Or .Named.Exists( "NoPopup" ) Then
blnPopups = False
intValidArgs = intValidArgs + 1
End If
If .Named.Exists( "V" ) Or .Named.Exists( "Verbose" ) Then
blnVerbose = True
intValidArgs = intValidArgs + 1
End If
If .Unnamed.Count > 0 And ( blnList Or blnAll ) Then Syntax
If .Unnamed.Count = 0 And Not ( blnAll Or blnList ) Then Syntax
If intValidArgs <> .Count Then Syntax
End With
' Check if UpdateCheck.ini exists
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
strINI = Replace( WScript.ScriptFullName, ".vbs", ".ini" )
If Not objFSO.FileExists( strINI ) Then
WScript.Echo vbCrLf & "ERROR:" & vbTab & "INI file not found."
Set objFSO = Nothing
Syntax
End If
Set objFSO = Nothing
If blnList Then
ListProgs strINI, True
WScript.Quit 0
Else
If blnAll Then
arrAllProgs = ListProgs( strINI, False )
For i = 0 To UBound( arrAllProgs )
CheckProg arrAllProgs(i)
Next
Else
CheckProg strProg
End If
End If
Sub CheckProg( myProg )
Dim objFSO, wshShell
Dim strExec, strExec2, strLatest, strName, strPath, strPrompt, strRegPath, strRegPath2, strRegVersion, strRegVersion2, strTitle, strVersion
Set wshShell = CreateObject( "Wscript.Shell" )
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
' Read values for specified program from UpdateCheck.ini
strName = ReadIni( strINI, myProg, "ProgName" )
strRegPath = wshShell.ExpandEnvironmentStrings( ReadIni( strINI, myProg, "RegPath" ) )
strRegPath2 = wshShell.ExpandEnvironmentStrings( ReadIni( strINI, myProg, "RegPath2" ) )
strRegVersion = ReadIni( strINI, myProg, "RegVersion" )
strRegVersion2 = ReadIni( strINI, myProg, "RegVersion2" )
strExec = wshShell.ExpandEnvironmentStrings( ReadIni( strINI, myProg, "Executable" ) )
strExec2 = wshShell.ExpandEnvironmentStrings( ReadIni( strINI, myProg, "Executable2" ) )
urlDownload = ReadIni( strINI, myProg, "WebSiteDownload" )
If Not objFSO.FileExists( strExec ) And strExec2 <> "" Then strExec = strExec2
If strExec = "" Then strExec = strExec2
If strRegPath = "" Then strRegPath = strRegPath2
If strRegVersion = "" Then strRegVersion = strRegVersion2
If strRegVersion = "" Then
If strRegPath <> "" Then
On Error Resume Next
strPath = wshShell.RegRead( strRegPath )
If Err Then
strPath = wshShell.RegRead( strRegPath2 )
End If
On Error Goto 0
strExec = objFSO.BuildPath( strPath, strExec )
If Not objFSO.FileExists( strExec ) And strExec2 <> "" Then strExec = objFSO.BuildPath( strPath, strExec2 )
End If
If strExec = "" Then
WScript.Echo vbCrLf & "ERROR:" & vbTab & "Program path of """ & strName & """ not specified."
Set objFSO = Nothing
Exit Sub
End If
If Not objFSO.FileExists( strExec ) Then
WScript.Echo vbCrLf & "ERROR:" & vbTab & "Program executable of """ & strName & """ not found."
Set objFSO = Nothing
Exit Sub
End If
strVersion = objFSO.GetFileVersion( strExec )
Else
On Error Resume Next
strVersion = wshShell.RegRead( strRegVersion )
If Err Then
strVersion = wshShell.RegRead( strRegVersion2 )
End If
On Error Goto 0
End If
If Strip( strVersion ) = "" Then
WScript.Echo vbCrLf & "ERROR:" & vbTab & "Unable to detect the current version of """ & strName & """ ."
Set wshShell = Nothing
Set objFSO = Nothing
Exit Sub
End If
strLatest = GetVersion( myProg )
' /V switch requires display of intermediate results
If blnVerbose Then
WScript.Echo vbCrLf _
& "Currently installed version : " & strVersion & vbCrLf _
& "Latest available version : " & strLatest
End If
' Rather complex looking comparison, because sometimes an extra ".0" is appended, and we don't want to make that break the comparison
If Left( strVersion, Min( Len( strLatest ), Len( strVersion ) ) ) = Left( strLatest, Min( Len( strLatest ), Len( strVersion ) ) ) Then
WScript.Echo """" & strName & """ " & strVersion & " is up-to-date"
Else
If blnVerbose Then
WScript.Echo "UPDATE:" & vbTab & "A new version is available for """ & strName & """"
Else
WScript.Echo "UPDATE:" & vbTab & "A new version (" & strLatest & ") is available for """ & strName & """ (" & strVersion & " installed)"
End If
If blnPopups Then
strPrompt = "Version " & strLatest & " of """ & strName & """ is newer than the installed " & strVersion & " version." _
& vbCrLf & vbCrLf _
& "Do you want to download the latest version?"
strTitle = "Update available"
If MsgBox( strPrompt, vbYesNoCancel, strTitle ) = vbYes Then
wshShell.Run urlDownload
End If
End If
End If
Set wshShell = Nothing
Set objFSO = Nothing
End Sub
' Get the latest available version
Function GetVersion( myProg )
Dim objHTTP, objMatch, objRE
Dim strHTML, strPattern, strUserAgent, strVersion
Dim urlCheck
' Initial return string, in case an error occurs
strVersion = "999999"
' Read the required web page URL and regex pattern from the INI file
urlCheck = ReadIni( strINI, myProg, "WebSiteVersion" )
strPattern = ReadIni( strINI, myProg, "RegExPattern" )
' Use WinHTTP to read the text from the specified web page
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", urlCheck, False
strUserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"
objHTTP.SetRequestHeader "UserAgent", strUserAgent
objHTTP.Send
If objHTTP.Status = 200 Then
' If the page was returned, use a regular expression to extract the program's current version number
strHTML = objHTTP.ResponseText
Set objRE = New RegExp
objRE.Pattern = strPattern
objRE.IgnoreCase = False
objRE.Global = True
Set objMatch = objRE.Execute( strHTML )
If objMatch.Count > 0 Then
For Each objMatch In objMatch.Item(0).Submatches
strVersion = objMatch
Next
End If
' Remove leading zeroes
strVersion = Replace( strVersion, "(", "" )
strVersion = Replace( strVersion, ")", "" )
strVersion = Replace( strVersion, " ", "." )
strVersion = Replace( strVersion, ".0", "." )
strVersion = Replace( strVersion, "..", ".0." )
strVersion = Replace( strVersion, "..", ".0." )
If Right( strVersion, 1 ) = "." Then strVersion = strVersion & "0"
Set objMatch = Nothing
Set objRE = Nothing
Set objHTTP = Nothing
Else
If blnVerbose Then
WScript.Echo "ERROR:" & vbTab & "Version check on the web page returned status " & objHTTP.Status & vbCrLf & vbTab & vbTab & urlCheck
Else
WScript.Echo "ERROR:" & vbTab & "Version check on the web page returned status " & objHTTP.Status
End If
Set objHTTP = Nothing
Exit Function
End If
' Return the result
GetVersion = strVersion
End Function
Function ListProgs( myINIFile, myDoDisplay )
Dim arrListProgs( ), i
Dim objFSO, objFile, objMatches, objMatch, objRE
Dim strAllINIText, strProgName
Const ForReading = 1
ReDim arrListProgs(0)
arrListProgs(0) = vbNull
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFSO.OpenTextFile( myINIFile, ForReading, False )
strAllINIText = objFile.ReadAll( )
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = False
objRE.Pattern = "(?:^|\n|\r)\[([^\n\r\]]+)\](?:\n|\r|$)"
Set objMatches = objRE.Execute( strAllINIText )
If objMatches.Count = 0 Then
WScript.Echo "ERROR:" & vbTab & "No program sections found in INI file."
Set objMatches = Nothing
Set objRE = Nothing
Exit Function
Else
If myDoDisplay Then
WScript.Echo "Name:" & vbTab & vbTab & "Description:" & vbCrLf _
& "=====" & vbTab & vbTab & "============"
End If
For Each objMatch In objMatches
strProgName = ReadIni( myINIFile, Strip( objMatch.Value ), "ProgName" )
If myDoDisplay Then WScript.Echo Strip( objMatch.Value ) & vbTab & vbTab & strProgName
If UBound( arrListProgs ) = 0 And arrListProgs(0) = vbNull Then
arrListProgs(0) = Strip( objMatch.Value )
Else
i = UBound( arrListProgs ) + 1
ReDim Preserve arrListProgs(i)
arrListProgs(i) = Strip( objMatch.Value )
End If
Next
End If
ListProgs = arrListProgs
End Function
Function Max( num1, num2 )
If num1 > num2 Then
Max = num1
Else
Max = num2
End If
End Function
Function Min( num1, num2 )
If num1 < num2 Then
Min = num1
Else
Min = num2
End If
End Function
Function ReadIni( myINIFile, mySection, myKey )
Dim objFSO, objFile, objMatches, objRE
Dim strAllINIText, strPattern, strValue
Const ForReading = 1
ReadIni = ""
strValue = ""
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objFile = objFSO.OpenTextFile( myINIFile, ForReading, False )
strAllINIText = objFile.ReadAll( )
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = True
objRE.Pattern = "\[" & mySection & "\]"
If objRE.Test( strAllINIText ) Then
objRE.Pattern = "\[" & mySection & "\][\n\r]+(?:[^\n\r=]+=[^\n\r]*[\n\r]+)*?" & myKey & "=([^\n\r]*)(?:\n|\r|$)"
Set objMatches = objRE.Execute( strAllINIText )
If objMatches.Count > 0 Then
strValue = Strip( objMatches.Item(0).SubMatches(0) )
End If
Set objMatches = Nothing
Set objRE = Nothing
Else
WScript.Echo "ERROR:" & vbTab & "Program """ & mySection & """ not found in INI file." & vbCrLf _
& vbTab & vbTab & "Use /List switch to list known programs."
Set objRE = Nothing
Syntax
End If
ReadIni = strValue
End Function
Function Strip( myString )
Dim strString
strString = Trim( myString )
Do While Left( strString, 1 ) = " " Or Left( strString, 1 ) = Chr(9) Or Left( strString, 1 ) = """" Or Left( strString, 1 ) = "[" Or Left( strString, 1 ) = vbCr Or Left( strString, 1 ) = vbLf
strString = Mid( strString, 2 )
Loop
Do While Right( strString, 1 ) = " " Or Right( strString, 1 ) = Chr(9) Or Right( strString, 1 ) = """" Or Right( strString, 1 ) = "]" Or Right( strString, 1 ) = vbCr Or Right( strString, 1 ) = vbLf
strString = Mid( strString, 1, Len( strString ) - 1 )
Loop
Strip = strString
End Function
Sub Syntax
Dim strMsg
strMsg = vbCrLf _
& "UpdateCheck.vbs, Version 1.08" _
& vbCrLf _
& "Check if a newer version of the specified program is available" _
& vbCrLf & vbCrLf _
& "Usage: UpdateCheck.vbs progname [ /Verbose ] [ /NoPopup ]" _
& vbCrLf & vbCrLf _
& " or: UpdateCheck.vbs /All [ /Verbose ] [ /NoPopup ]" _
& vbCrLf & vbCrLf _
& " or: UpdateCheck.vbs /List" _
& vbCrLf & vbCrLf _
& "Where: progname is the program to be checked" _
& vbCrLf _
& " /Verbose displays intermediate results too" _
& vbCrLf _
& " /All check all programs found in the INI file" _
& vbCrLf _
& " /List lists all programs found in the INI file" _
& vbCrLf _
& " /NoPopup no popup if an update is available" _
& vbCrLf & vbCrLf _
& "Notes: This script uses an INI file to specify HOW to check a program's" _
& vbCrLf _
& " current version and the latest available version on the web." _
& vbCrLf _
& " You may have to edit the INI file each time the web pages' URL" _
& vbCrLf _
& " or layout changes." _
& vbCrLf _
& " Switches may be abbreviated, e.g. /A /NP instead of /All /NoPopup." _
& vbCrLf & vbCrLf _
& "Written by Rob van der Woude" _
& vbCrLf _
& "http://www.robvanderwoude.com"
WScript.Echo strMsg
WScript.Quit 1
End Sub
page last modified: 2024-04-16; loaded in 0.0086 seconds