module1.bas文件:
public const max_path = 260
public const file_attribute_archive = &h20
public const file_attribute_hidden = &h2
public const invalid_handle_value = -1
public type filetime
dwlowdatetime as long
dwhighdatetime as long
end type
public type win32_find_data
dwfileattributes as long
ftcreationtime as filetime
ftlastaccesstime as filetime
ftlastwritetime as filetime
nfilesizehigh as long
nfilesizelow as long
dwreserved0 as long
dwreserved1 as long
cfilename as string * max_path
calternate as string * 14
end type
public declare function findfirstfile lib "kernel32" alias "findfirstfilea" (byval lpfilename as string, lpfindfiledata as win32_find_data) as long
public declare function findnextfile lib "kernel32" alias "findnextfilea" (byval hfindfile as long, lpfindfiledata as win32_find_data) as long
public declare function findclose lib "kernel32" (byval hfindfile as long) as long
public function getfullallfilefilter(directory as string) as string
getfullallfilefilter = directory + "\*.*"
end function
frmmain.frm文件:
version 5.00
begin vb.form frmmain
borderstyle = 3 fixed dialog
caption = "enum files demo"
clientheight = 4770
clientleft = 3210
clienttop = 1905
clientwidth = 4920
beginproperty font
name = "宋体"
size = 9
charset = 134
weight = 400
underline = 0 false
italic = 0 false
strikethrough = 0 false
endproperty
linktopic = "form1"
maxbutton = 0 false
minbutton = 0 false
scaleheight = 4770
scalewidth = 4920
showintaskbar = 0 false
begin vb.commandbutton cmdlist
caption = "&enumfiles"
height = 375
left = 3600
tabindex = 3
top = 1800
width = 1215
end
begin vb.dirlistbox dir
height = 1140
left = 0
tabindex = 2
top = 480
width = 4935
end
begin vb.drivelistbox drive
height = 300
left = 0
tabindex = 1
top = 120
width = 4935
end
begin vb.listbox lstfilenames
height = 2400
itemdata = "frmmain.frx":0000
left = 0
list = "frmmain.frx":0002
tabindex = 0
top = 2280
width = 4935
end
begin vb.label label1
caption = "click enumfiles button to list all files in specified directory."
height = 495
left = 120
tabindex = 4
top = 1680
width = 3375
end
end
attribute vb_name = "frmmain"
attribute vb_globalnamespace = false
attribute vb_creatable = false
attribute vb_predeclaredid = true
attribute vb_exposed = false
option explicit
private sub cmdlist_click()
dim lpfiledata as win32_find_data 保存找到的文件的信息
dim hfindfile as long 文件查找句柄
dim findpattern as string 查询的文件模式
dim tmp as boolean
获得模式
findpattern = getfullallfilefilter(dir.path)
查找第一个文件
hfindfile = findfirstfile(findpattern, lpfiledata)
如果没有找到
if hfindfile = invalid_handle_value then
exit sub
end if
清除列表
lstfilenames.clear
设置临时变量
tmp = true
do while tmp
如果找到的是文件而不是目录
(如果连隐藏文件都要一并找出,该条件改为:
if lpfiledata.dwfileattributes and (file_attribute_archive or file_attribute_hidden) then ...)
if lpfiledata.dwfileattributes and file_attribute_archive then
将文件名添加到列表中
lstfilenames.additem lpfiledata.cfilename
end if
查找下一个文件
tmp = findnextfile(hfindfile, lpfiledata)
loop
全部查询完毕,关闭句柄
findclose hfindfile
end sub
private sub drive_change()
dir.path = drive.drive
end sub
private sub form_load()
dir.path = drive.drive
end sub
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




