
用VB6.0如何获得某一文件下的所有txt的名称?
比如我要获得文件“D:\DATA”中所有的TXT文件的名称,并把这些TXT文件的名称添加到list1中,例如list1中显示SYS.txtCOM.txt...如何实现,代...
比如我要获得文件“D:\DATA”中所有的TXT文件的名称,并把这些TXT文件的名称添加到list1中,例如list1中显示
SYS.txt
COM.txt
.
.
.
如何实现,代码执行要求效率高,因为txt文件可能较多。谢谢!
最好不要用FileSystemObject
如解决我追加50分 展开
SYS.txt
COM.txt
.
.
.
如何实现,代码执行要求效率高,因为txt文件可能较多。谢谢!
最好不要用FileSystemObject
如解决我追加50分 展开
展开全部
'递归法搜索,速度很快的.
Public Function GetExtName(strFileName As String) As String
Dim strTmp As String
Dim strByte As String
Dim i As Long
For i = Len(strFileName) To 1 Step -1
strByte = Mid(strFileName, i, 1)
If strByte <> "." Then
strTmp = strByte + strTmp
Else
Exit For
End If
Next i
GetExtName = strTmp
End Function
Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
Dim strFileDir() As String
Dim strFile As String
Dim i As Long
Dim lDirCount As Long
On Error GoTo MyErr
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
While strFile <> "" '搜索当前目录
DoEvents
If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到的是目录
If strFile <> "." And strFile <> ".." Then '排除掉父目录(..)和当前目录(.)
lDirCount = lDirCount + 1 '将目录数增1
ReDim Preserve strFileDir(lDirCount) As String
strFileDir(lDirCount - 1) = strFile '用动态数组保存当前目录名
End If
Else
If strSearch = "" Then
Form1.List1.AddItem strPath + strFile
ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then
'满足搜索条件,则处理该文件
Form1.List1.AddItem strPath + strFile '将文件全名保存至列表框List1中
End If
End If
strFile = Dir
Wend
For i = 0 To lDirCount - 1
Form1.Label3.Caption = strPath + strFileDir(i)
Call search(strPath + strFileDir(i), strSearch) '递归搜索子目录
Next
ReDim strFileDir(0) '将动态数组清空
search = True '搜索成功
Exit Function
MyErr:
search = False '搜索失败
End Function
Private Sub Command1_Click()
search "D:\DATA\", "*.txt"
End Sub
Public Function GetExtName(strFileName As String) As String
Dim strTmp As String
Dim strByte As String
Dim i As Long
For i = Len(strFileName) To 1 Step -1
strByte = Mid(strFileName, i, 1)
If strByte <> "." Then
strTmp = strByte + strTmp
Else
Exit For
End If
Next i
GetExtName = strTmp
End Function
Public Function search(ByVal strPath As String, Optional strSearch As String = "") As Boolean
Dim strFileDir() As String
Dim strFile As String
Dim i As Long
Dim lDirCount As Long
On Error GoTo MyErr
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
strFile = Dir(strPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
While strFile <> "" '搜索当前目录
DoEvents
If (GetAttr(strPath + strFile) And vbDirectory) = vbDirectory Then '如果找到的是目录
If strFile <> "." And strFile <> ".." Then '排除掉父目录(..)和当前目录(.)
lDirCount = lDirCount + 1 '将目录数增1
ReDim Preserve strFileDir(lDirCount) As String
strFileDir(lDirCount - 1) = strFile '用动态数组保存当前目录名
End If
Else
If strSearch = "" Then
Form1.List1.AddItem strPath + strFile
ElseIf LCase(GetExtName(strPath + strFile)) = LCase(GetExtName(strSearch)) Then
'满足搜索条件,则处理该文件
Form1.List1.AddItem strPath + strFile '将文件全名保存至列表框List1中
End If
End If
strFile = Dir
Wend
For i = 0 To lDirCount - 1
Form1.Label3.Caption = strPath + strFileDir(i)
Call search(strPath + strFileDir(i), strSearch) '递归搜索子目录
Next
ReDim strFileDir(0) '将动态数组清空
search = True '搜索成功
Exit Function
MyErr:
search = False '搜索失败
End Function
Private Sub Command1_Click()
search "D:\DATA\", "*.txt"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
'查找目录“D:\DATA”下的所有 txt 文件:
Private Sub Command1_Click()
Dim F As String, I As Long, nPath As String, nName As String
nName = Dir("D:\DATA\*.txt", 7)
List1.Clear
Do
If nName = "" Then Exit Do
List1.AddItem nName
nName = Dir()
Loop
MsgBox "找到 " & List1.ListCount & " 个文件", vbInformation
End Sub
Private Sub Command1_Click()
Dim F As String, I As Long, nPath As String, nName As String
nName = Dir("D:\DATA\*.txt", 7)
List1.Clear
Do
If nName = "" Then Exit Do
List1.AddItem nName
nName = Dir()
Loop
MsgBox "找到 " & List1.ListCount & " 个文件", vbInformation
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用drivelist dirlistbox,filelistbox三个控件配合可能是最简单的啦
呵呵,加一个filelistbox,一个listbox就好了
'===============================================================
Private Sub Form_Load()
Dim a As Integer
File1.Path = "d:\data"
File1.Pattern = "*.txt"
For a = 0 To File1.ListCount - 1
File1.ListIndex = a
List1.AddItem File1.FileName
Next
End Sub
呵呵,加一个filelistbox,一个listbox就好了
'===============================================================
Private Sub Form_Load()
Dim a As Integer
File1.Path = "d:\data"
File1.Pattern = "*.txt"
For a = 0 To File1.ListCount - 1
File1.ListIndex = a
List1.AddItem File1.FileName
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_Activate()
Set fs = CreateObject("Scripting.FileSystemObject")
strfile = Dir("d:\data\*.txt")
While strfile <> ""
Set f = fs.GetFile("d:\data\" & strfile)
List1.AddItem strfile
strfile = Dir()
Wend
End Sub
Set fs = CreateObject("Scripting.FileSystemObject")
strfile = Dir("d:\data\*.txt")
While strfile <> ""
Set f = fs.GetFile("d:\data\" & strfile)
List1.AddItem strfile
strfile = Dir()
Wend
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询