constant long INVALID_HANDLE_VALUE = -1
constant long FILE_ATTRIBUTE_DIRECTORY = 16 //目录
String FName, DirName;
long hFindFile,new_handle
long BeWorking
long ll_file_attribute
treeviewitem NewItem;
WIN32_FIND_DATA lpFindFileData
if isnull(as_path) or trim(as_path)='' then return
DirName = as_path+"\*.*";
m_Tree.SetRedraw(False)
hFindFile =FindFirstFile(DirName,lpFindFileData)
do while ( hFindFile>0 ) //BeWorking非零,指找到了文件或目录
//查找同级的目录
BeWorking=FindNextFile(hFindFile,lpFindFileData)
if BeWorking<=0 then exit
FName=as_path+'\'+lpFindFileData.cfilename
ll_file_attribute=GetFileAttributes(FName)
//如果查找的结果是目录又不是".."或"."
if ll_file_attribute=FILE_ATTRIBUTE_DIRECTORY and trim(lpFindFileData.cfilename)<>'..' and trim(lpFindFileData.cfilename)<>'.'then
//向Tree1中添加目录;
NewItem.label=lpFindFileData.cfilename
NewItem.data=FName
NewItem.pictureindex=1
NewItem.statepictureindex=0
NewItem.selectedpictureindex=1
NewItem.children=true
//NewItem取得节点,其目的是为了下一层中添加节点方便,递归时把它传过去。
new_handle = m_Tree.InsertItemlast( al_handle, NewItem );
//进入下一层递归调用。
if as_type='all' then wf_AddFile(m_tree,FName, new_handle,as_type)
//wf_add_coldata(lv_1,lpFindFileData,as_path)
elseif trim(lpFindFileData.cfilename)<>'..' and trim(lpFindFileData.cfilename)<>'.' then
//退出递归时,到了这里,如果查找结果是文件,向Tree中添加文件
NewItem.label=lpFindFileData.cfilename
//mle_result.text += lpFindFileData.cfilename + '~r~n'
NewItem.data=FName
NewItem.pictureindex=4
NewItem.statepictureindex=0
NewItem.selectedpictureindex=3
NewItem.children=false
m_Tree.InsertItemlast( al_handle, NewItem )
// wf_add_coldata(lv_1,lpFindFileData,as_path)
end if
loop// end of while O
m_Tree.SetRedraw(True)
if hFindFile>0 then FindClose(hFindFile)
//记着用完CFileFild实例要关闭
API用到的结构
win32_find_data from structure
unsignedlong dwfileattributes
filetime ftcreationtime
filetime ftlastaccesstime
filetime ftlastwritetime
unsignedlong nfilesizehigh
unsignedlong nfilesizelow
unsignedlong dwreserved0
unsignedlong dwreserved1
character cfilename[260]
character calternatefilename[14]
filetime from structure
long htime
long ltime
API声明
FUNCTION ulong FileTimeToLocalFileTime(ref FILETIME lpFileTime,ref FILETIME lpLocalFileTime) LIBRARY "kernel32.dll"
FUNCTION long FindFirstFile(ref string lpFileName,ref WIN32_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindFirstFileA"
FUNCTION long FindNextFile(long hFindFile,ref WIN32_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindNextFileA"
FUNCTION long GetFileAttributes(ref string lpFileName) LIBRARY "kernel32.dll" ALIAS FOR "GetFileAttributesA"
FUNCTION ulong FindClose(long hFindFile) LIBRARY "kernel32.dll"