Vb6 符合多个条件的VisualBasic6
我有一个问题,我肯定我的标题是关闭的,但不知道如何用词的问题。我正在组装一个小工具,可以在文本框中查看串行控制台输出。我想查找非零大小的文件,并将目录和文件名添加到列表框中 我的文本框输出如下所示:Vb6 符合多个条件的VisualBasic6,vb6,Vb6,我有一个问题,我肯定我的标题是关闭的,但不知道如何用词的问题。我正在组装一个小工具,可以在文本框中查看串行控制台输出。我想查找非零大小的文件,并将目录和文件名添加到列表框中 我的文本框输出如下所示: Directory of system:/ 718 drwx 0 <no date> cme 2 -r-- 0 <no date> default
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
然后查找文件:
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组合起来,这样我就有了目录,如果它不是零,那么文件就添加到列表框中,但它只是循环并再次列出每个列出的目录的所有文件。一定有更好的方法来获得我想要的东西,一个看起来像
系统:正在运行配置
我的解决方案解决后,有点难看,但有效:
基本上把它分成两部分。首先,它列出所有目录,然后将目录添加到列表框disk0:、flash:、system:等中,然后通过迭代该文件列表来统计每个目录,收集文件并使用目录list1&filename将其添加到另一个lsitbox中,然后清除显示,然后转到列表中的下一个目录
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
您需要一次性解析输入,并确定哪些行是目录,哪些行是文件。我使用了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
您需要一次性解析输入,并确定哪些行是目录,哪些行是文件。我使用了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
我喜欢这比我的解决方案,我张贴我的,以显示我的工作以及,但这是教育我。谢谢。比起我的解决方案,我更喜欢这个,我也发布了我的作品来展示我的作品,但这对我来说是一个教育。非常感谢。
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