Monday, December 20, 2021

Pick the next version msp from current version of a file and install that

 For that used vbscript.

Let's say I have multiple msp files,

 SizingSuite_2.0.2.2_Update

SizingSuite_2.0.2.3_Update

SizingSuite_2.0.2.6_Update

SizingSuite_2.0.9.2_Update


I need to pick the next highest version installed on my machine from the version of exe, say (2.0.2.4), then above should pick 2.0.2.6


The vbscript I have for this:



Dim oFso,oShell,sSetup,sParams,iRet,sLine,sLogName,sLogDir,sMasterLog,oMasterLog,sSuccessfulCodes,sd,mspname,msppath,current_version

Set oFso = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

Const iTidyDivLen = 127

sd = oFso.GetParentFolderName(WScript.ScriptFullName)

current_version=oFso.GetFileVersion("C:\Program Files (x86)\Spirax Sarco\Sizing Suite\SizingSuite.exe" )
'MsgBox(current_version)
'MsgBox(CompareFileVersion(current_version, "3.0.16.0"))

mspfilelist = getFileList("msp")
'DisplayArray mspfilelist
mspVersionlist = getMSPVersions(mspfilelist)
'DisplayArray mspVersionlist
SortArray(mspVersionlist)
'DisplayArray mspVersionlist

mspname = GetFileName(FetchNearestVersion(current_version,mspVersionlist),mspfilelist)
msppath = sd&"\"&mspname
'MsgBox(msppath)
RunInstallation "msiexec.exe","/p "&msppath&" /qn","0,3010",True


Public Function GetFileName(name,aArray)
   for Each fileName in aArray
     if InStr(1,fileName,name,1)>0 Then
	   GetFileName=fileName
	 end if
   Next
End Function

Sub SortArray(p_Array)
    Dim i
    Dim j
    Dim varTemp
     
    For i = UBound(p_Array) To LBound(p_Array) Step -1
        For j = LBound(p_Array) + 1 To i
            If p_Array(j - 1) > p_Array(j) Then
                varTemp = p_Array(j - 1)
                p_Array(j - 1) = p_Array(j)
                p_Array(j) = varTemp
            End If
        Next
    Next
End Sub

Public Function FetchNearestVersion(current_version,aArray)
 for Each fileVersion in aArray
   if not Len(fileVersion)>0 then
      'MsgBox("Empty record")
   elseif CompareFileVersion(fileVersion,current_version)=1 Then
	 'MsgBox("Higher:" & fileVersion)
	 FetchNearestVersion = fileVersion
	 Exit For
   Else
     'MsgBox(Len(fileVersion))
     'MsgBox("not higher version:" & fileVersion)
   end if
 Next

End Function

Public Sub DisplayArray(aArray)
  MsgBox(Join(aArray, vbNewLine))
End Sub

Public Function getMSPVersions(mspfilelist)
 maxSize = UBound(mspfilelist)+1
  redim Preserve a(maxSize)
 Dim sCount:sCount = -1
 For Each sValue in mspfilelist
  sCount = sCount + 1
  a(sCount) = ExtractVersionFromMSP(sValue)
 Next
 getMSPVersions=a
End Function


Public Function ExtractVersionFromMSP(txt)
'txt="SizingSuite_2.0.9.4_Update.msp"
txt1 = Replace(txt,"SizingSuite_","")
txt1 = Replace(txt1,"_Update","")
txt1 = Replace(txt1,".msp","")
'wscript.Echo(Replace(txt1,"SizingSuite",""))
ExtractVersionFromMSP=txt1
End Function

Public Function getFileList(sext)
objStartFolder = oFso.GetFile(Wscript.ScriptFullName).ParentFolder.Path
Set objFolder = oFso.GetFolder(objStartFolder)
Dim a(),count
ReDim a(-1) 
count = 0
Set colFiles = objFolder.Files
For Each objFile in colFiles
    
    If LCase(oFso.GetExtensionName(objFile.Name)) = sext Then
    count = count + 1
    redim Preserve a(count-1)
    a(count-1) =  objFile.Name
    'Wscript.Echo objFile.Name
    End If
Next
getFileList = a
End Function

Public Function CompareFileVersion(strFileVersion1, strFileVersion2)

    ' Our result
    ' -1 = File Version 2 is greater than File Version 1
    ' 0 = Versions are the same
    ' 1 = File version 1 is greater than File Version 2
    Dim intResult

    Dim strAryFileVersion1
    Dim strAryFileVersion2

   ' Let's initialize our result with 0
    intResult = 0

    'Split the two supplied file versions by the "." character
    strAryFileVersion1 = Split(strFileVersion1, ".")
    strAryFileVersion2 = Split(strFileVersion2, ".")

    For i = 0 To UBound(strAryFileVersion1)

        If strAryFileVersion1(i) > strAryFileVersion2(i) Then

            intResult = 1

        ElseIf strAryFileVersion1(i) < strAryFileVersion2(i) Then

            intResult = -1

        End If

        'If we have found that the result is not > or <, no need to proceed
        If intResult <> 0 Then Exit For

    Next

    If UBound(strAryFileVersion2) > UBound(strAryFileVersion1) _
    And strAryFileVersion2(UBound(strAryFileVersion2)) <> 0 Then intResult = -1

    CompareFileVersion = intResult

End Function


Function RunInstallation(sSetup, sParams, sSuccessfulCodes, bExitOnFailure)
	Log ""
	Log String(iTidyDivLen, "-")
	sSetup = Trim(sSetup)
	sParams = Trim(sParams)
	If sSetup = "" Then
		Log "Setup not specified. Aborting..."
		WScript.Quit(1)
	End If

	If (LCase(sSetup) = "msiexec.exe") Or (LCase(sSetup) = "cmd.exe")  Or (LCase(sSetup) = "wscript.exe")  Or (LCase(sSetup) = "cscript.exe") Then
		sSetup = oShell.ExpandEnvironmentStrings("%WinDir%") & "\System32\" & sSetup
	ElseIf (Mid(sSetup, 2, 2) <> ":\") And (Left(sSetup, 2) <> "\\") Then
		sSetup = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\")) & sSetup
	End If

	If Not oFso.FileExists(sSetup) Then
		Log sSetup & " not found. Aborting..."
		WScript.Quit(1)
	End If

	If InStr(sSetup, " ") <> 0 Then sSetup = """" & sSetup & """"

	If IsEmpty(sParams) Then
		Log "Running Command: " & sSetup
		iRet = oShell.Run(sSetup, 0, True)
	Else
		Log "Running Command: " & sSetup & " " & sParams
		iRet = oShell.Run(sSetup & " " & sParams, 0, True)
	End If

	Log String(iTidyDivLen, "-")
	Log "Return Code from " & sSetup & ": " & iRet
	Log String(iTidyDivLen, "-")

	If InStr("," & sSuccessfulCodes & ",", "," & iRet & ",") = 0 Then
		Log "Return code category: Failure."
		If bExitOnFailure Then
			WScript.Quit(iRet)
		End If
	Else
		Log "Return code category: Successful"
	End If

	Log String(iTidyDivLen, "-")
	Log ""

	RunInstallation = iRet
End Function

Sub Log(sLine)
	On Error Resume Next
	If Session.Property("ProductCode") = "" Then
		sLogDir = "" & sLogDir
		sLogName = "" & sLogName
		If sLogDir = "" Then sLogDir = oFso.GetParentFolderName(WScript.ScriptFullName)
		If Not oFso.FolderExists(sLogDir) Then
			oShell.Run "cmd.exe /c MD """ & sLogDir & """", 0, True
			If Not oFso.FolderExists(sLogDir) Then
				sLogDir = oShell.ExpandEnvironmentStrings("%TEMP%")
			End If
		End If
		If sLogName = "" Then sLogName = Left(WScript.ScriptName, Len(WScript.ScriptName) - 4) & "_Master_" & Replace(Replace(Replace(Now, " ", "_"), ":", "."), "/", ".") & ".log"
		sMasterLog = sLogDir & "\" & sLogName
		Set oMasterLog = oFso.OpenTextFile(sMasterLog, 8, True)
		Err.Clear
		oMasterLog.WriteLine Now & " : " & sLine
		oMasterLog.Close
		If Err.Number <> 0 Then WScript.Quit
		oMasterLog.Close
		Set oMasterLog = Nothing
	Else
		Dim oRec
		Set oRec = Session.Installer.CreateRecord(1)
		oRec.StringData(1) = Now & " : " & sLine
		Session.Message &H04000000, oRec
	End If
End Sub

No comments:

Post a Comment