Find substring from string output

左心房为你撑大大i 提交于 2020-03-25 03:39:27

问题


I have a script (below) that finds the current version of Microsoft Office Installed and outputs it as a string. The string output for example would be 15.0.4771.1000. I don't care about anything past the 15. My goal is to verify that the version of Office is 15. I want to take the substring of the digts before the 1st . (period) and compare it to the value in questioned. How would I do this?

The script below doesn't work even with a wild card for the compared value.

Dim oRegistry
Dim oFSO
Dim sKey
Dim sAppExe
Dim sValue
Dim sAppVersion

Const HKEY_LOCAL_MACHINE = &H80000002

Set oRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}//./root/default:StdRegProv")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sKey = "Software\Microsoft\Windows\CurrentVersion\App Paths"
oRegistry.GetStringValue HKEY_LOCAL_MACHINE, sKey & "\" & sAppExe, "", sValue
MsgBox oFSO.GetFileVersion(sValue)
If oFSO.GetFileVersion(sValue)="15.*" Then
  WScript.Echo("Office 2013 is installed")
Else
  WScript.Echo("You do not have Office 2013 installed")
End If
Set oFSO = Nothing
Set oRegistry = Nothing

回答1:


There are at least half a dozen ways to check a part of the string, but "wildcards" do not exist in VBScript.

One way would be If Left(value, 3) = "15." .... Another one would rely on the fact that a version string uses dots as separators: If Split(value, ".")(0) = 15 ....

The following uses Split() and cuts out the WMI; there is an easier way to read the registry in VBScript:

Option Explicit

Dim Shell, FSO, path, version
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

path = TryRegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe\")
If FSO.FileExists(path) Then version = FSO.GetFileVersion(path)

If version = "" Then
    WScript.Echo "You do not have Office installed at all"
ElseIf Split(version, ".")(0) = 15 Then
    WScript.Echo "You have Office 2013 installed"
Else
    WScript.Echo "You have a different version of Office (" + version + ")"
End If

Function TryRegRead(key)
    On Error Resume Next
    TryRegRead = Shell.RegRead(key)
End Function

Notes

  • get into the habit of using Option Explicit in every script
  • don't bother setting stuff to Nothing, it really is not worth it
  • The backslash at the end of the path is required, it makes Shell.RegRead() return the default value for that key.
  • I'm pretty sure reading iterating the children of HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and reading the Office version from there is a safer way of finding that information, compared to relying on the App Path for a single executable from the Office package.



回答2:


There are several ways to do this. For instance you could split the string at dots and take the first field of the resulting array:

version = oFSO.GetFileVersion(sValue)
majorVersion = Split(version, ".")(0)

Other options would be to extract the substring up to the first dot with string functions:

version = oFSO.GetFileVersion(sValue)
majorVersion = Left(version, InStr(version, "."))

or with a regular expression:

Set re = New RegExp
re.Pattern = "^(\d+)\..*"

version = oFSO.GetFileVersion(sValue)
majorVersion = re.Replace(version, "$1")



回答3:


You can try like this :

Option Explicit
Dim strComputer,objWMIService,colOperatingSystems,objOperatingSystem
Dim colSoft,objItem,OfficeVersion
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo objOperatingSystem.Caption
    Exit For
Next
Set colSoft = objWMIService.ExecQuery("SELECT * FROM Win32_Product WHERE Name Like '%Microsoft Office%'")
If colSoft.Count = 0 Then
    wscript.echo "NO OFFFICE INSTALLED" 
else
    For Each objItem In colSoft
        OfficeVersion = Left(objItem.Version, InStr(1,objItem.Version,".")-1)   
        Wscript.echo objitem.caption & ", Version " & OfficeVersion
        If OfficeVersion = 15 Then
            WScript.Echo "You have Office 2013 installed"
        Else
            WScript.Echo "You have a different version of Office (" & OfficeVersion & ")"
        End If
        exit for
    Next
End If


来源:https://stackoverflow.com/questions/34051999/find-substring-from-string-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!