查找文本并使用VBscript分配给变量

时间:2013-10-29 20:43:36

标签: variables search vbscript

我有一个程序给我输出如下。

Listing 3 device(s) for setup class "Ports" (Ports (COM & LPT)).
USB\VID_067B&PID_2303\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM3)
USB\VID_068B&PID_2304\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM4)
USB\VID_069B&PID_2305\5&164808BA&0&3                        : Prolific USB-to-Serial Comm Port (COM6)

我想获取COM#值并使用vbscript将其分配给变量 像这样

P1=COM3
P2=COM4
P3=COM6

此链接http://vbcity.com/forums/t/154919.aspx的角色正在使用该行获取我想要的输出。

System.IO.Ports.SerialPort.GetPortNames 

但我不知道如何在我的脚本中实现这一点。

谢谢

2 个答案:

答案 0 :(得分:1)

我把你的输出保存在一个名为OUTPUT.TXT的文本文件中,所以我用来运行它的命令是:

TYPE OUTPUT.TXT

但是,您可以随意用实际程序替换TYPE OUTPUT.TXT。以下VBScript显示了如何运行此命令并捕获并解析VBScript中的输出。我使用INSTRMID来查找和提取我需要的子字符串:

Dim objShell
Set objShell = CreateObject("WScript.Shell")
Dim objExec
Set objExec = objShell.Exec("CMD /C TYPE OUTPUT.TXT")
Dim lines
lines = objExec.StdOut.ReadAll
Dim line
Dim P(10)
Dim num
num = 0
For Each line in split(lines, vbCrLf)
  Dim idx
  idx = InStr(line, "Prolific USB-to-Serial Comm Port (")
  If idx > 0 Then
    idx = idx + 34
    Dim idx2
    idx2 = InStr(idx, line, ")")
    num = num + 1
    P(num) = Mid(line, idx, idx2 - idx)
    WScript.Echo "P" & num & "=" & P(num)
  End If
Next

答案 1 :(得分:0)

我在这里看到了你的另一个问题。您应该能够从Win32_SerialPort类的WMI中获取此信息。这有帮助吗?

On Error Resume Next

strComputer = "."

Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
Set colInstances = objWMIService.ExecQuery( "SELECT * FROM Win32_SerialPort" )

If colInstances.Count = 1 Then
    WScript.Echo "1 instance:" & vbCrLf
Else
    WScript.Echo colInstances.Count & " instances:" & vbCrLf
End If

For Each objInstance In colInstances
    WScript.Echo "Availability                : " & objInstance.Availability
    WScript.Echo "Binary                      : " & objInstance.Binary
    WScript.Echo "Capabilities                : " & Join( objInstance.Capabilities, "," )
    WScript.Echo "CapabilityDescriptions      : " & Join( objInstance.CapabilityDescriptions, "," )
    WScript.Echo "Caption                     : " & objInstance.Caption
    WScript.Echo "Description                 : " & objInstance.Description
    WScript.Echo "DeviceID                    : " & objInstance.DeviceID
    WScript.Echo "Name                        : " & objInstance.Name
    WScript.Echo "PNPDeviceID                 : " & objInstance.PNPDeviceID
    WScript.Echo "ProviderType                : " & objInstance.ProviderType
    WScript.Echo
Next