VBscript中的语法分析
我在下面粘贴了一个文本文件转储,我只需要显示磁盘的大小,例如在msgbox中。在下面的示例中,它将显示500.1GB 3次,每个硬盘大小显示一次。我不太清楚怎么做。没有我可以划定的固定间距。我已将代码粘贴到下面。任何帮助都将非常感激VBscript中的语法分析,vbscript,string-parsing,Vbscript,String Parsing,我在下面粘贴了一个文本文件转储,我只需要显示磁盘的大小,例如在msgbox中。在下面的示例中,它将显示500.1GB 3次,每个硬盘大小显示一次。我不太清楚怎么做。没有我可以划定的固定间距。我已将代码粘贴到下面。任何帮助都将非常感激 <DISK LIST> Disk State Disk Type Port Type Speed Size Free CA -
<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)
多谢各位 这应该适合你。其思想是使用破折号分隔线来定位与大小相关的字符位置
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
由于您的输入文件似乎具有固定格式,数据值中没有空格,我可能会这样做:
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