【问题标题】:Parsing in VBscript在 VBscript 中解析
【发布时间】:2013-12-26 17:50:12
【问题描述】:

我在下面粘贴了一个文本文件转储,我只需要显示磁盘的大小,例如在 msgbox 中。在下面的这种情况下,它将显示 (500.1GB) 3 次,每个 HDD 一个尺寸。我不确定该怎么做。没有我可以划定的固定间距。我已经粘贴了下面的代码。任何帮助将不胜感激。

<DISK LIST>                                      

Disk  State   Disk Type   Port Type      Speed   Size    Free     CA      
---- -------- -------- ---------------- ------- ------- ------- ------- 
0    Online   Disk     SATA-II/NCQ      6Gb/sec 500.1GB 492.0GB  -RW 
1    Online   Disk     SATA-II/NCQ      6GB/sec 500.1GB 492.0GB  -RW
2    Online   Disk     SATA-II/NCQ      6GB/sec 500.1GB 492.0GB  -RW


<ARRAY LIST>
...

这是我目前拥有的代码。

Const ForReading = 1
Const ForWriting = 2
Dim match, sizeCount, tempArray, foundSection
match = false
matchString = "Size"
foundSection = false

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\dump.txt", ForReading, True)

Do Until objTextFile.AtEndOfStream
  strLine = objTextFile.ReadLine

  if strLine <> "" then
    tempArray = Split(strLine," ",-1,1)
    match = true
      End If

If InStr(strLine, matchString) = 0 Then
  wscript.echo("found")
    foundSection = true
End If

if (match) then
  If (IsNumeric(tempArray(0))) Then
    sizeCount = sizeCount + 1
      disksize = disksize & Trim(tempArray(0)) & " "  
  End If
End if

If (tempArray(0)) = "<ARRAY" Then
  foundSection = false
End If
Loop
Wscript.Echo("disk size =" & disksize)

谢谢。

【问题讨论】:

    标签: vbscript string-parsing


    【解决方案1】:

    这应该适合你。这个想法是使用破折号的分隔线来定位与大小相关的字符位置。

    Const ForReading = 1
    Const ForWriting = 2
    
    Dim sizeStart, sizeEnd ' the character position at which Size starts and ends
    
    Main
    
    Sub Main()
        Dim bDisplay, strLine
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objTextFile = objFSO.OpenTextFile("C:\dump.txt", ForReading, True)
    
        bDisplay = False
        Do Until objTextFile.AtEndOfStream
            strLine = objTextFile.ReadLine
            ' Stop Display once no length line is met
            If Len(strLine) = 0 Then
                bDisplay = False
                sizeStart = 0
                sizeEnd = 0
            End If
            If bDisplay And sizeStart + sizeEnd > 0 Then
                Wscript.Echo "disk size = " & Mid(strLine, sizeStart, sizeEnd - sizeStart)
            End If
            If InStr(1, strLine, "--") > 0 Then
                GetSizeStartEndPositions strLine
                bDisplay = True
            End If
        Loop
        objTextFile.Close
    End Sub
    
    Sub GetSizeStartEndPositions(sLine)
        '  |Disk  State   Disk Type   Port Type      Speed   Size    Free     CA
        '  |---- -------- -------- ---------------- ------- ------- ------- -------
        'i |----1--------2--------3----------------4-------5-------6-------7-------
        '  |                                                ^     ^
        ' Size is between 5th and 6th space in the separater line with dashes
        '
        Const SpaceStart = 5
        Const SpaceEnd = 6
        Dim i, pos
        i = 1
        pos = 1
        Do
            pos = InStr(pos, sLine, " ", vbTextCompare) + 1
            If i = SpaceStart Then sizeStart = pos
            If i = SpaceEnd Then sizeEnd = pos - 1
            i = i + 1
        Loop Until pos >= Len(sLine)
    End Sub
    

    【讨论】:

      【解决方案2】:

      由于您的输入文件似乎具有固定格式,数据值中没有空格,因此我可能会执行以下操作:

      Set fso = CreateObject("Scripting.FileSystemObject")
      
      Set re = New RegExp
      re.Pattern = "\s+"
      re.Global  = True
      
      Set f = fso.OpenTextFile("C:\dump.txt")
      Do Until f.AtEndOfStream
        line = Trim(f.ReadLine)
        If line = "<DISK LIST>" Then        'process lines following "<DISK LIST>"
          For i=1 To 3 : f.SkipLine : Next  'skip header lines
      
          Do Until line = "" Or f.AtEndOfStream
            line = Trim(f.ReadLine)
            line = re.Replace(line, " ")    'collapse adjacent spaces to a single one
            arr  = Split(line)              'split line into array
            If UBound(arr) > -1 Then
              WScript.Echo arr(5)           'print 6th field (size)
            End If
          Loop
      
          Exit For  'no need to read the rest of the file
        End If
      Loop
      f.Close
      

      【讨论】:

        猜你喜欢
        • 2011-01-17
        • 1970-01-01
        • 2021-10-02
        • 2012-11-18
        • 2012-08-01
        • 1970-01-01
        • 2012-04-07
        • 2013-11-04
        • 1970-01-01
        相关资源
        最近更新 更多