我的主要源代码如下: 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),false); end; until FindNextFile(hFindFile,FindFileData)=false; end else begin ChDir(sCurDir); result:=false; exit; end; ChDir(sCurDir); result:=true; end;function CopyDir(sDirName:String;sToDirName:string):Boolean; begin if Length(sDirName)<=0 then exit; //拷贝... Result:=DoCopyDir(sDirName,sToDirName); end; 程序中用 CopyDir进行文件拷贝,如CopyDir('c:\my ducoments\data','d:\data')就将c:\my ducoments\data目录下的文件拷贝到d:\data目录下。
既来之则up之.....
up
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),false);
end;
until FindNextFile(hFindFile,FindFileData)=false;
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
ChDir(sCurDir);
result:=true;
end;function CopyDir(sDirName:String;sToDirName:string):Boolean;
begin
if Length(sDirName)<=0 then
exit;
//拷贝...
Result:=DoCopyDir(sDirName,sToDirName);
end;
程序中用 CopyDir进行文件拷贝,如CopyDir('c:\my ducoments\data','d:\data')就将c:\my ducoments\data目录下的文件拷贝到d:\data目录下。