//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")