我遇到了问题,我确定我的标题已关闭,但不确定如何解决问题。我正在整理一个小工具,它将在文本框中查看串行控制台输出。我想查找非零大小的文件,并将目录和文件名添加到列表框中。
我的文本框输出如下所示:
Directory of system:/
718 drwx 0 <no date> cme
2 -r-- 0 <no date> default-running-config
725 dr-x 0 <no date> fpm
3 drwx 0 <no date> its
105 dr-x 0 <no date> memory
1 -rw- 867 <no date> running-config
104 dr-x 0 <no date> vfiles
No space information available
Directory of tmpsys:/
6 drw- 0 <no date> eem_lib_system
5 drw- 0 <no date> eem_lib_user
21 -rw- 0 <no date> eem_pnt_2
23 -rw- 0 <no date> eem_pnt_3
25 -rw- 0 <no date> eem_pnt_4
No space information available
Directory of flash:/
3 -rw- 822 Jan 27 2014 22:15:16 +00:00 TESTFILE1.TEST
4 -rw- 822 Jan 27 2014 22:15:22 +00:00 TESTFILE2.TEST
5 -rw- 1644 Jan 27 2014 22:15:30 +00:00 TESTFILE3.TEST
6 -rw- 2466 Jan 27 2014 22:15:38 +00:00 TESTFILE4.TEST
7 -rw- 4110 Jan 27 2014 22:15:48 +00:00 TESTFILE5.TEST
到目前为止,我的代码可以找到非零大小的文件,并将它们列在列表框中,我有代码可以列出'目录',但我不能让两者一起工作。我使用函数调用GetEverythingBetween来查找我的文件,这是
Function GetEveryThingBetween(ByVal Str As String, ByVal strStart As String, ByVal strEnd As String, Optional ByVal st As Long = 0) As String()
Dim foundstr As String
Dim s1, s2 As String
Dim xStart As Integer
Dim xEnd As Integer
Dim bolKeepGoing As Boolean
Dim RetStr() As String
Dim StrCount As Integer
bolKeepGoing = True
s1 = InStr(st + 1, Str, strStart, vbTextCompare)
If s1 > 0 Then
Do Until bolKeepGoing = False
xStart = s1 + Len(strStart) + 1
xEnd = InStr(xStart, Str, strEnd, vbTextCompare)
If xEnd > 0 And xEnd > xStart Then
'We have found an item
foundstr = Mid(Str, xStart, xEnd - xStart)
ReDim Preserve RetStr(StrCount)
RetStr(StrCount) = foundstr
StrCount = StrCount + 1
s1 = InStr(xEnd + Len(strEnd), Str, strStart, vbTextCompare)
If s1 < xStart Then
bolKeepGoing = False
End If
Else
'No more Items
bolKeepGoing = False
End If
Loop
End If
GetEveryThingBetween = RetStr
End Function
然后我找到目录的代码:
Dim Stuff() As String
Dim i As Long
Dim dir As String
On Error Resume Next
Stuff = GetEveryThingBetween(txtDisplay.Text, "Directory of", "/")
For i = 0 To UBound(Stuff)
dir = Stuff(i)
List1.AddItem dir
Next i
然后找到文件:
Dim Stuff() As String
Dim i As Long
Dim filename As String
On Error Resume Next
Stuff = GetEveryThingBetween(txtDisplay.Text, " ", Chr(13))
For i = 0 To UBound(Stuff)
Exists = InStr(Stuff(i), "0") And InStr(Stuff(i), "<no date>") <> 0
filename = Stuff(i)
If Not Exists <> 0 Then List1.AddItem filename
Next i
我试图将2组合在一起,这样我就有了目录,然后将文件(如果它的非零)添加到列表框中,但它只是循环并为每个列出的目录再次列出所有文件。必须有一个更好的方法来获得我想要的,一个看起来像
的列表框条目“系统:运行配置”
我的解决方案在解决之后,有点难看,但有效:
基本上将其分为两部分。首先,它列出所有directorys,然后将目录添加到列表框(disk0:,flash:,system:etc),然后通过遍历该列表中的文件来统计每个目录,收集文件并将其添加到具有目录的另一个lsitbox( list1&amp; filename),然后清除显示,然后转到列表中的下一个目录。
Dim Stuff() As String
Dim filename As String
Dim i As Long
Dim ii As Integer
Dim z As Integer
Dim Dir As String
Dim Exxists, Exists
On Error Resume Next
MSComm1.Output = "dir all" & Chr(13)
Do
DoEvents
Exists = InStr(Me.txtDisplay, "bytes free") <> 0
Pause 250
If Exists <> 0 Then GoTo NextStep
Loop 'let the dir command finish before issuing more commands
NextStep:
MSComm1.Output = Chr(13)
Pause 2000 'Add Directories to List1
Stuff = GetEveryThingBetween(txtDisplay.Text, "Directory of", "/")
For i = 0 To UBound(Stuff)
Dir = Stuff(i)
List1.AddItem Dir
Next i
txtDisplay.Text = "" 'clear dispaly
For z = List1.ListCount - 1 To 0 Step -1 'Directory names in list 1
txtDisplay.Text = ""
Pause 250
MSComm1.Output = "dir " & List1.List(z) & ":" & Chr(13) 'displsys files in directory
Do
DoEvents
Exists = InStr(Me.txtDisplay, "bytes free") <> 0
Exxists = InStr(Me.txtDisplay, "No space") <> 0
Pause 250
If Exists Or Exxists <> 0 Then GoTo Step2
Loop 'wait for command to finish listing files
Step2: 'get the file names and add directory and file name to lsit2
Stuff = GetEveryThingBetween(txtDisplay.Text, "> ", Chr(13))
For ii = 0 To UBound(Stuff)
filename = Stuff(ii)
List2.AddItem List1.List(z) & filename
Next ii
Pause 1000
List1.RemoveItem (z)
Next z 'go to next directory name in list1
答案 0 :(得分:4)
您需要在一次传递中解析输入并确定哪些行是目录,哪些行是文件。我使用你的GetEveryThingBetween
方法来解析目录名称,但我的文件名有问题,所以我添加了自己的函数。我认为你可以很好地理解我的ParseDirOutput
方法,你可以根据需要调整它。
Private Sub ParseDirOutput()
Dim strLine As String
Dim strDirMarker As String
Dim strFileMarker As String
Dim aryLines() As String
Dim i As Integer
Dim strDirectory As String
Dim strFile As String
Dim stuff() As String
List1.Clear
strDirMarker = "Directory of"
strFileMarker = " "
aryLines = Split(txtDisplay.Text, vbCrLf)
For i = LBound(aryLines) To UBound(aryLines)
strLine = aryLines(i)
If Len(strLine) > 0 Then
If InStr(strLine, strDirMarker) > 0 Then
stuff = GetEveryThingBetween(strLine, strDirMarker, "/")
strDirectory = stuff(LBound(stuff))
ElseIf InStr(strLine, strFileMarker) > 0 Then
strFile = ExtractFileName(strLine)
If Len(strFile) > 0 Then
List1.AddItem strDirectory & strFile
End If
End If
End If
Next i
End Sub
Private Function ExtractFileName(ByVal vString As String) As String
Dim strReversed As String
Dim i As Integer
Dim strReturn As String
If InStr(vString, " 0 ") = 0 And InStr(vString, "<no date>") > 0 Then
strReversed = StrReverse(vString)
i = InStr(strReversed, " ")
strReturn = Mid$(vString, Len(vString) - i + 2)
End If
ExtractFileName = strReturn
End Function