function CopyDir(sDirName:String; sToDirName:string):Boolean; begin if Length(sDirName)<=0 then exit; //拷贝... Result:=DoCopydir(sDirName,sToDirName); end;
function DoCopyDir(sDirName:String; sToDirName:String):Boolean; var hFindFile:Cardinal; t,tfile :String; sCurDir:String[255]; FindFileData:WIN32_FIND_DATA; begin //先保存当前目录 sCurDir:=GetCurrentDir; ChDir(sDirName); hFindFile:=FindFirstFile(*.*,FindFileData); if hFindFile>0 then begin if not DirectoryExists(sToDirName) then ForceDirectories(sToDirName); repeat tfile :=FindFileData.cFileName; if (tfile=.) or (tfile=..) then Continue; if FindFileData.dwFileAttributes= FILE_ATTRIBUTE_DIRECTORY then begin t:=sToDirName+\+tfile; if not DirectoryExists(t) then ForceDirectories(t); if sDirName[Length(sDirName)]<>'\ 'then DoCopyDir(sDirName+\+tfile,t) else DoCopyDir(sDirName+tfile,sToDirName+tfile); end else begin t:=sToDirName+\+tFile; CopyFile(PChar(tfile),PChar(t),True); end; until FindNextFile(hFindFile,FindFileData)=false; FindClose(hFindFile); end else begin ChDir(sCurDir); result:=false; exit; end; //回到原来的目录下 ChDir(sCurDir); result:=true; end;
function DoCopyDir(sDirName:String; sToDirName:String):Boolean; var sr:TsearchRec; hFindFile:Cardinal; t,tfile :String; sCurDir:String[255]; FindFileData:WIN32_FIND_DATA; Attrs:Integer; begin //先保存当前目录 sCurDir:=GetCurrentDir; try ChDir(sDirName);//改变当前目录为‘SDirName’ except exit; end; //showmessage(sdirname); hFindFile:=FindFirstfile('*.*',FindFileData);//把找到的第一个文件赋给hfindFile if hFindFile<>INVALID_HANDLE_VALUE then begin if not DirectoryExists(sToDirName) then ForceDirectories(sToDirName); //判断目标目录是否存在,如果不存在,则创建该多级目录 repeat tfile :=FindFileData.cFileName; //搜索所在目录下的所有文件 if (tfile='.') or (tfile='..')then Continue; if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) =FILE_ATTRIBUTE_DIRECTORY then begin t:=sToDirName+'\'+tfile; if not DirectoryExists(t) then ForceDirectories(t); //判断目录是否存在,如果没有则建立目录 //if sDirName[Length(sDirName)]<>'\' then // begin DoCopyDir(sDirName+'\'+tfile,t);//如果路径后不带'\',则需加上 Attrs:=FileGetAttr(sDirName+'\'+tfile); FileSetAttr(t,Attrs); // end // else // begin // DoCopyDir(sDirName+tfile,sToDirName+tfile); // Attrs:=FileGetAttr(sDirName+tfile); // FileSetAttr(t,Attrs); // end; end else begin t:=sToDirName+'\'+tFile; CopyFile(PChar(tfile),PChar(t),True); end; until FindNextFile(hFindFile,FindFileData)=false; //FindClose(hFindFile); end else begin ChDir(sCurDir); result:=false; exit; end; //回到原来的目录下 ChDir(sCurDir); result:=true; end;
//拷贝函数:CopyDir function CopyDir(sDirName:String; sToDirName:string):Boolean; begin if Length(sDirName)<=0 then exit; //拷贝... Result:=DoCopyDir(sDirName,sToDirName); end; 功能基本实现,还有些小地方有待改进
----------------------------------------------
-