1.Win 32 API 和 结构体
API 函数我都封装在n_win32_api类里面, 如:g_win32_api.of_createdirectory..., 这部分可以参考b 目录遍历(原作者:itwenping)。
2.有些函数是比较简单的自定义函数,没有一一列出,如:f_isempty(实际上是判断字符串为空的函数), 大家自己决定怎么写,下面是主要代码:
global type f_copyfiles from function_object
end type
forward prototypes
global function integer f_copyfiles (string as_directoryname, string as_filetype, string as_targdir, boolean ab_replace)
end prototypes
global function integer f_copyfiles (string as_directoryname, string as_filetype, string as_targdir, boolean ab_replace);// Description: 复制文件到指定路径
//------------------------------------------------------------------------------
// Arguments:
// value string as_directoryname 目录全路径:如“c:/”
// value string as_filetype 文件类型:若为*则复制所有文件和文件夹,其他类型为文件后缀名
// value string as_targdir 目标路径
//------------------------------------------------------------------------------
// Returns: string 格式:file1name/file2name...filenname/
// 失败返回 null
//------------------------------------------------------------------------------
string filename
long ll_rtn , ll_find
s_find_data FindFileData
ulong ll_FileAttributes
string ls_FileAttributes= " " , ls_currpath, ls_target_childforder, ls_temp
//判断文件夹是否存在
as_directoryname = trim(as_directoryname)
if right(as_directoryname, 1) <> '\' then as_directoryname = as_directoryname + "\"
//目标路径检查
if f_isempty(as_targdir) then return -1
if not g_win32_api.of_pathisdirectory(as_targdir) > 0 then
if not g_win32_api.of_createdirectory(as_targdir) then
MessageboxEx("System Error", "Create directory '" +as_targdir + "' failed", stopsign!)
return -1
end if
end if
ls_temp= Reverse (as_targdir )
ll_find = pos(ls_temp, "\")
if ll_find > 0 then
ls_target_childforder = Reverse(left(ls_temp, ll_find -1))
end if
ll_FileAttributes=g_win32_api.of_GetFileAttributes(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
RETURN -1
END IF
//获得文件列表
filename= trim(as_directoryname) + "*." + trim(as_filetype)
ll_rtn = g_win32_api.of_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 g_win32_api.of_FindNextFileW(ll_rtn,FindFileData) = 0 then
g_win32_api.of_FindClose(ll_rtn)
exit
else
filename=FindFileData.filename
filename=trim(filename)
if ls_target_childforder = filename then continue //目标文件夹就是源文件夹,跳过
if filename <> ".." then //as_filetype = * 遍历整个目录
//复制文件或创建文件夹到目标路径
ls_currpath = ""
ls_currpath = as_directoryname + filename
if g_win32_api.of_pathisdirectory(ls_currpath) > 0 then
// if f_isempty(ls_srcforder) and then ls_srcforder
if not g_win32_api.of_pathisdirectory(as_targdir + "\" + filename) > 0 then
//目标路径不存在该子文件夹
if not g_win32_api.of_createdirectory(as_targdir + "\" + filename) then
MessageboxEx("System Error", "Create directory '" +ls_currpath + "' failed", stopsign!)
return -1
end if
//遍历子文件夹
if f_copyfiles(as_directoryname + filename, as_filetype, as_targdir + "\" + filename, ab_replace) < 0 then
return -1
end if
end if
else
FileCopy (ls_currpath, as_targdir + "\" + filename , ab_replace )
end if
end if
end if
loop
******声明:
上面代码部分参考pb 目录遍历(原作者:itwenping)。有任何问题, 可以联系我或原作者.
return 1
end function