正巧我刚做了个自动备份的程序要用到这个函数
function tform1.DoCopyDir(sDirName:String;sToDirName:String):Boolean;
var
hFindFile:Cardinal;
t,tfile :String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
//记录当前目录
sCurDir:=GetCurrentDir;
ChDir(sDirName);
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName)
else
begin
DelTree(sToDirName);
//rmdir(sToDirName);
end;
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile<>INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
{else
begin
DelTree(sToDirName);
rmdir(sToDirName);
end;}
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;