2015-12-02 61 views
1

我有一個腳本(下面),它查找當前版本的Microsoft Office Installed並將其輸出爲字符串。例如,字符串輸出將是15.0.4771.1000。我不關心15歲以後的任何事情。我的目標是驗證Office的版本是15.我想在第一個.(期間)之前取出digts的子字符串,並將其與受質疑的值進行比較。我將如何做到這一點?從字符串輸出中查找子串

即使通配符爲比較值,下面的腳本也不起作用。

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 
+0

['Split'功能](https://msdn.microsoft.com/en-us/library/0764e5w5(V = vs.84)。 aspx) – JosefZ

+0

是否必須是VBScript?你可以用PowerShell,KiXtart等做同樣的事情,並達到同樣的目標。 – Leptonator

回答

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版本是一種更安全的方式來查找該信息,而不是依靠Office程序包中單個可執行文件的App Path。
+1

事實上,你打算解釋它,而不是隻是張貼代碼得到我的投票。 – Lankymart

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") 
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 
相關問題