pb 目录遍历

该博客介绍了一种使用API(如GetFileAttributesA、FindFirstFile、FindNextFile等)在Windows系统中遍历指定目录,获取特定类型文件列表的方法。通过结构体s_find_data和s_filetime来存储文件信息,并根据文件属性筛选文件。
//api 声明
Function   ulong   GetFileAttributesA(string   lpFileName)   LIBRARY   "kernel32.dll "
FUNCTION ulong FindFirstFile(ref string lpFileName,ref s_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindFirstFileA"  
FUNCTION ulong FindNextFile(ulong hFindFile,ref s_FIND_DATA lpFindFileData) LIBRARY "kernel32.dll" ALIAS FOR "FindNextFileA"
FUNCTION Long FindClose ( Long hFindFile) LIBRARY "kernel32"
Function Integer GetLastError ( ) Library "kernel32"
 
//定义结构体s_find_data
global   type   s_find_data   from   structure
unsignedlong fileattribus
s_filetime creationtime
s_filetime lastaccesstime
s_filetime lastwritetime
unsignedlong filesizehigh
unsignedlong filesizelow
unsignedlong reserved0
unsignedlong reserved1
character filename[260]
character alternatefilename[14]
end   type

//定义结构体s_filetime
global   type   s_filetime   from   structure
unsignedlong lowdatetime
unsignedlong highdatetime
end   type
 
//==============================================================================
//   Function:   gf_getfilelist()
//------------------------------------------------------------------------------
//   Description:   获得某个目录下某种类型文件的列表
//------------------------------------------------------------------------------
//   Arguments:
//   value   string           as_directoryname         目录全路径:如“c:/”
//   value   string           as_filetype              文件类型:若为*则返回所以文件和文件夹,其他类型为文件后缀名
//------------------------------------------------------------------------------
//   Returns:     string   格式:file1name/file2name...filenname/
//                       失败返回   null
//------------------------------------------------------------------------------
string   filename,filename_list
long   ll_rtn
s_find_data   FindFileData
ulong   ll_FileAttributes
string   ls_FileAttributes= " "
//判断文件夹是否存在
ll_FileAttributes=GetFileAttributesA(as_directoryname)
IF   ll_FileAttributes=4294967295   THEN
 SetNull(ls_FileAttributes)
ELSE
 IF   Mod(ll_FileAttributes,   2)   >     0   THEN   ls_FileAttributes   +=   "R "
 IF   Mod(ll_FileAttributes,   4)   >     1   THEN   ls_FileAttributes   +=   "H "
 IF   Mod(ll_FileAttributes,   8)   >     3   THEN   ls_FileAttributes   +=   "S "
 IF   Mod(ll_FileAttributes,32)   >   15   THEN   ls_FileAttributes   +=   "D "
 IF   Mod(ll_FileAttributes,64)   >   31   THEN   ls_FileAttributes   +=   "A "
END   IF
IF   IsNull(ls_FileAttributes)   OR   Pos(ls_FileAttributes, "D ") <=0   THEN  
 SETNULL(filename_list)
 RETURN   filename_list
END   IF
 //获得文件列表
 filename= trim(as_directoryname) + "*." + trim(as_filetype)
 ll_rtn   =   FindFirstFile(filename,FindFileData)
 filename=FindFileData.filename
 filename=trim(filename)
 if trim(as_filetype) <> "*" and filename = "." then //as_filetype = * 遍历整个目录
  filename_list+=filename+ "/"
 end if
do   while  true
 if FindNextFile(ll_rtn,FindFileData) = 0  then
  FindClose(ll_rtn)
  return   filename_list
 else
  filename=FindFileData.filename
  filename=trim(filename)
  if filename <> ".." then //as_filetype = * 遍历整个目录
   filename_list+= filename+ "/"
  end if
 end if
loop
return   filename_list
 
//调用如:gf_getfilelist("c:/","txt")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值