VB读取指定目录下所有文件名问题

发布网友

我来回答

1个回答

热心网友

控件:按钮一个
列表框一个

function
searchfiles(path
as
string,
filetype
as
string)
dim
files()
as
string
'文件路径
dim
folder()
as
string
'文件夹路径
dim
a,
b,
c
as
long
dim
spath
as
string
spath
=
dir(path
&
filetype)
'查找第一个文件
do
while
len(spath)
'循环到没有文件为止

a
=
a
+
1

redim
preserve
files(1
to
a)

files(a)
=
path
&
spath
'将文件目录和文件名组合,并存放到数组中

list1.additem
files(a)
'加入list控件中

spath
=
dir
'查找下一个文件

doevents
'让出控制权
loop
spath
=
dir(path
&
"\",
vbdirectory)
'查找第一个文件夹
do
while
len(spath)
'循环到没有文件夹为止

if
left(spath,
1)
<>
"."
then
'为了防止重复查找

if
getattr(path
&
"\"
&
spath)
and
vbdirectory
then
'如果是文件夹则。。。。。。

b
=
b
+
1

redim
preserve
folder(1
to
b)

folder(b)
=
path
&
spath
&
"\"
'将目录和文件夹名称组合形成新的目录,并存放到数组中

end
if

end
if
spath
=
dir
'查找下一个文件夹
doevents
'让出控制权
loop
for
c
=
1
to
b
'使用递归方法,遍历所有目录

searchfiles
folder(c),
filetype
next
end
function
private
sub
command1_click()
'调用
searchfiles
"e:\",
"*.*"
end
sub

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com