VB点击按钮读取一个文件夹中所有子文件的名称
要实现这样一个功能,一个按钮command1,一个list1控件,我要通过点击command1按钮实现将"D:\图片"中所有的子文件的名称读出来,并显示在list1控件中...
要实现这样一个功能,一个按钮command1,一个list1控件,我要通过点击command1按钮实现将"D:\图片"中所有的子文件的名称读出来,并显示在list1控件中(图片文件夹中有未知数量的jpg文件,要先获取文件的数量,在显示在list1中,最终显示的格式为"xxxx.jpg")
谢谢
尽量用最简单的函数和语句,谢谢! 展开
谢谢
尽量用最简单的函数和语句,谢谢! 展开
2个回答
展开全部
Private i%, myfile$
Private Sub Command1_Click()
Dim m, spt
On Error Resume Next
i = 0
myfile = ""
OutFile "D:\图片\", "jpg"
If MsgBox("一共找到 " & i & " 个文件.是否载入列表?", vbYesNo) = vbYes Then
spt = Split(myfile, vbCrLf)
For m = 0 To UBound(spt) - 1
List1.AddItem Mid(spt(m), InStrRev(spt(m), "\") + 1)
Next
End If
End Sub
Private Sub OutFile(ByVal MyFolder As String, ByVal MyFileName As String)
Dim fs, F, f1, S, sf, f2, mf
Dim L As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
Set F = fs.GetFolder(MyFolder)
Set sf = F.SubFolders
For Each f1 In sf
OutFile f1, MyFileName
DoEvents
Next
L = Len(MyFileName)
Set mf = F.Files
For Each f2 In mf
If Right(f2, L) = MyFileName Then
i = i + 1
myfile = myfile & f2 & vbCrLf
End If
DoEvents
Next
End Sub
Private Sub Command1_Click()
Dim m, spt
On Error Resume Next
i = 0
myfile = ""
OutFile "D:\图片\", "jpg"
If MsgBox("一共找到 " & i & " 个文件.是否载入列表?", vbYesNo) = vbYes Then
spt = Split(myfile, vbCrLf)
For m = 0 To UBound(spt) - 1
List1.AddItem Mid(spt(m), InStrRev(spt(m), "\") + 1)
Next
End If
End Sub
Private Sub OutFile(ByVal MyFolder As String, ByVal MyFileName As String)
Dim fs, F, f1, S, sf, f2, mf
Dim L As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
Set F = fs.GetFolder(MyFolder)
Set sf = F.SubFolders
For Each f1 In sf
OutFile f1, MyFileName
DoEvents
Next
L = Len(MyFileName)
Set mf = F.Files
For Each f2 In mf
If Right(f2, L) = MyFileName Then
i = i + 1
myfile = myfile & f2 & vbCrLf
End If
DoEvents
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询