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 <> 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 = 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;
===============================================
第二种方法
uses ShellApi;
///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!
//例如如果要复制E:\Temp整个目录到E:\那么代码为: copydirectory('e:\temp','e:\');
///如果要复制E:\Temp到E:\Test目录下面,那么代码为:CopyDirecotry('E:\Temp','E:\TEST');
function CopyDirectory(const Source, Dest: string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_COPY;
pFrom := PChar(source+#0);
pTo := PChar(Dest+#0);
fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR ;
end;
Result := (SHFileOperation(fo) = 0);
end;