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

String 从字符串输出中查找子字符串,string,vbscript,substring,String,Vbscript,Substring,我有一个脚本(如下所示),用于查找已安装的Microsoft Office的当前版本,并将其作为字符串输出。例如,字符串输出为15.0.4771.1000。我不在乎15岁以后的事。我的目标是验证Office的版本是否为15。我想取第一个(句点)之前的digt的子字符串,并将其与中的值进行比较。我该怎么做 下面的脚本甚至与比较值的通配符都不起作用 Dim oRegistry Dim oFSO Dim sKey Dim sAppExe Dim sValue Dim sAppVersion Cons

我有一个脚本(如下所示),用于查找已安装的Microsoft Office的当前版本,并将其作为字符串输出。例如,字符串输出为15.0.4771.1000。我不在乎15岁以后的事。我的目标是验证Office的版本是否为15。我想取第一个
(句点)之前的digt的子字符串,并将其与中的值进行比较。我该怎么做

下面的脚本甚至与比较值的通配符都不起作用

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

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

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")

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

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")

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

一种方法是
如果左(值3)=“15”。…
。另一个是版本字符串使用点作为分隔符的事实:
如果拆分(值“.”(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
注释

  • 养成在每个脚本中使用
    选项Explicit
    的习惯
  • 不要费心把东西设成什么都不做,这真的不值得
  • 路径末尾的反斜杠是必需的,它使
    Shell.regrad()
    返回该键的默认值
  • 我非常确信,与依赖应用程序路径从Office软件包中获取单个可执行文件相比,通过迭代
    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
    的子项并从中读取Office版本,可以更安全地找到这些信息

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

一种方法是
如果左(值3)=“15”。…
。另一个是版本字符串使用点作为分隔符的事实:
如果拆分(值“.”(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
注释

  • 养成在每个脚本中使用
    选项Explicit
    的习惯
  • 不要费心把东西设成什么都不做,这真的不值得
  • 路径末尾的反斜杠是必需的,它使
    Shell.regrad()
    返回该键的默认值
  • 我非常确信,与依赖应用程序路径从Office软件包中获取单个可执行文件相比,通过迭代
    HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
    的子项并从中读取Office版本,可以更安全地找到这些信息

您可以这样尝试:

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

您可以这样尝试:

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

它必须是VBScript吗?您可以使用PowerShell、KiXtart等执行相同的操作,并实现相同的目标。是否必须使用VBScript?你可以用PowerShell、KiXtart等做同样的事情,实现同样的目标。你费心解释而不是仅仅发布代码get是我的投票。你费心解释而不是仅仅发布代码get是我的投票。