从字符串输出中查找子字符串

时间:2015-12-02 20:05:14

标签: string vbscript substring

我有一个脚本(下面),它找到当前版本的Microsoft Office Installed并将其作为字符串输出。例如,字符串输出为15.0.4771.1000。我不关心15之后的任何事情。我的目标是验证Office的版本是否为15.我想在第一个.(句点)之前获取挖掘的子字符串并将其与值进行比较在质疑。我该怎么做?

即使使用通配符作为比较值,下面的脚本也不起作用。

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

3 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点。例如,您可以在点处拆分字符串并获取结果数组的第一个字段:

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

其他选项是使用字符串函数将子字符串提取到第一个点:

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

或使用正则表达式:

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

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

答案 1 :(得分:2)

至少有六种方法可以检查字符串的一部分,但是"通配符"在VBScript中不存在。

一种方式是If Left(value, 3) = "15." ...。另一个是依赖于版本字符串使用点作为分隔符的事实:If Split(value, ".")(0) = 15 ...

以下使用Split()并删除WMI;有一种更简单的方法来阅读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

注释

  • 养成在每个脚本中使用Option Explicit的习惯
  • 不要为Nothing设置内容,这真的不值得
  • 路径末尾的反斜杠是必需的,它会使Shell.RegRead()返回该密钥的默认值。
  • 我非常确定阅读迭代HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall的子项并从那里读取Office版本是一种更安全的方法来查找该信息,相比之下,依赖于App Path来获取Office的单个可执行文件包。

答案 2 :(得分:0)

您可以尝试这样:

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