从字符串输出中查找子字符串 [英] Find substring from string output

查看:45
本文介绍了从字符串输出中查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本(如下),用于查找当前安装的 Microsoft Office 版本并将其作为字符串输出.例如,字符串输出将为 15.0.4771.1000.我不在乎 15 之后的任何东西.我的目标是验证 Office 的版本是 15.我想取第一个 .(句点)之前的 digts 的子字符串并进行比较到所质疑的价值.我该怎么做?

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

推荐答案

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

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

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

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 ....

下面使用了Split(),切掉了WMI;有一种更简单的方法可以在 VBScript 中读取注册表:

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

注意事项

  • 养成在每个脚本中使用Option Explicit的习惯
  • 不要把东西设置成Nothing,真的不值得
  • 路径末尾的反斜杠是必需的,它使 Shell.RegRead() 返回该键的默认值.
  • 我很确定阅读迭代 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall 的子级并从那里读取 Office 版本是一种更安全的方式来查找该信息,与依赖相比位于 Office 包中单个可执行文件的应用程序路径上.
  • 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.

这篇关于从字符串输出中查找子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆