如何遍历某文件夹下的某类文件

kojiang 2006-04-26 08:50:39
例如,要获取c:\的所有*.txt的文件
...全文
147 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hangzhou6274 2006-04-27
  • 打赏
  • 举报
回复
居然连代码都贴出来了啊
kojiang 2006-04-27
  • 打赏
  • 举报
回复
强,感谢!!!
hygougou 2006-04-26
  • 打赏
  • 举报
回复
代码实现取到指定目录下的所有文件列表(目录下包括子文件夹和文件)
//AddFile(treeview m_tree,string as_path,long al_handle) return none
//as_path为传递过来的目录层次,本次函数调用中搜索的文件都是它的下一层的。
//al_handle为传递过来的Tree节点,本次函数调用中添加的Tree节点都是它的子节点。

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"



稍微修改一下

1,077

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧