要实现这样一个功能,一个按钮command1,一个list1控件,我要通过点击command1按钮实现将"D:\图片"中所有的子文件的名称读出来,并显示在list1控件中(图片文件夹中有未知数量的jpg文件,要先获取文件的数量,在显示在list1中,最终显示的格式为"xxxx.jpg")
谢谢
尽量用最简单的函数和语句,谢谢!
VB点击按钮读取一个文件夹中所有子文件的名称
答案:3 悬赏:50
解决时间 2021-11-16 17:13
- 提问者网友:醉人眸
- 2021-11-15 23:03
最佳答案
- 二级知识专家网友:傲娇菇凉
- 2021-11-15 23:49
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
全部回答
- 1楼网友:冷眼_看世界
- 2021-11-16 02:51
VB自带标准控件【FileListBox 】
将其Pattern属性设置为*.jpg就好了
有现成的控件,不用写那么多代码了吧
- 2楼网友:瘾与深巷
- 2021-11-16 01:28
option explicit
option base 1
sub find()
dim mydir, match as string
dim sf(1 to 100) as string
dim cf as integer, cjpg as integer
mydir = "d:\tuev\documents and settings\he-zh\my documents\he"
chdrive left(mydir, 1)
chdir mydir
match = dir$("*.jpg")
do until len(match) = 0
cjpg = cjpg + 1
match = dir$
loop
match = dir$("", vbdirectory)
do until len(match) = 0
if match <> "." and match <> ".." then
cf = cf + 1
sf(cf) = match
end if
match = dir$
loop
如果你要的是每个子文件夹下的jpg个数,代码得修改。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯