拷贝目录树!!! {拷贝目录!注意:sDirName, sToDirName 一定要用绝对路径 } function DoCopyDir(sDirName:String; sToDirName:String; overwrites:Boolean):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, overwrites) else DoCopyDir(sDirName+tfile,sToDirName+tfile,overwrites); end else begin t:=sToDirName+'\'+tFile; CopyFile(PChar(tfile),PChar(t),not overwrites); end; until FindNextFile(hFindFile,FindFileData)=false; windows.FindClose(hFindFile); end else begin ChDir(sCurDir); result:=false; exit; end; //回到原来的目录下 ChDir(sCurDir); result:=true; end;function CopyDir(sDirName:String; sToDirName:string; overwrites:Boolean):Boolean; begin if Length(sDirName)<=0 then exit; Result:=DoCopyDir(sDirName,sToDirName,overwrites); end;
var str,str1:string; OpStruc: TSHFileOpStruct; FromBuf, ToBuf: Array [0..128] of Char; direc:string; begin try direc:='c:\temp'; if not DirectoryExists(direc) then ForceDirectories(direc); str:='a.db'; deletefile(direc+'\'+str); if not fileExists(direc+'\'+str) then begin FillChar( FromBuf, Sizeof(FromBuf), 0 ); FillChar( ToBuf, Sizeof(ToBuf), 0 ); StrPCopy( FromBuf, Pchar(frm_directory+'\'+frm_filename) ); StrPCopy( ToBuf, Pchar(direc) ); // 设置OpStruc with OpStruc do begin Wnd:= Handle; wFunc:= FO_COPY; pFrom:= @FromBuf; pTo:=@ToBuf; fFlags:=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION; fAnyOperationsAborted:= False; hNameMappings:= nil; lpszProgressTitle:= nil; end; if ShFileOperation( OpStruc ) = 0 then //复制文件成功 begin str1:=extractfilename(frm_directory+'\'+frm_filename); ChDir(direc); // str1:=copy(str1,1,8)+'.db'; if renamefile(str1,str) then //备份文件该名成功 ; end; end; except exit; end;
path2:=edit2.text; //備份路徑
try
copyfile(Pchar(v_path1),Pchar(path2),False);
except
showmessage('error');
end;
{拷贝目录!注意:sDirName, sToDirName 一定要用绝对路径 }
function DoCopyDir(sDirName:String;
sToDirName:String; overwrites:Boolean):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, overwrites)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile,overwrites);
end
else
begin
t:=sToDirName+'\'+tFile;
CopyFile(PChar(tfile),PChar(t),not overwrites);
end;
until FindNextFile(hFindFile,FindFileData)=false;
windows.FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//回到原来的目录下
ChDir(sCurDir);
result:=true;
end;function CopyDir(sDirName:String;
sToDirName:string; overwrites:Boolean):Boolean;
begin
if Length(sDirName)<=0 then exit; Result:=DoCopyDir(sDirName,sToDirName,overwrites);
end;
str,str1:string;
OpStruc: TSHFileOpStruct;
FromBuf, ToBuf: Array [0..128] of Char;
direc:string;
begin
try
direc:='c:\temp';
if not DirectoryExists(direc)
then ForceDirectories(direc);
str:='a.db';
deletefile(direc+'\'+str);
if not fileExists(direc+'\'+str) then
begin
FillChar( FromBuf, Sizeof(FromBuf), 0 );
FillChar( ToBuf, Sizeof(ToBuf), 0 ); StrPCopy( FromBuf, Pchar(frm_directory+'\'+frm_filename) );
StrPCopy( ToBuf, Pchar(direc) );
// 设置OpStruc
with OpStruc do
begin
Wnd:= Handle;
wFunc:= FO_COPY;
pFrom:= @FromBuf;
pTo:=@ToBuf;
fFlags:=FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= nil;
lpszProgressTitle:= nil;
end;
if ShFileOperation( OpStruc ) = 0 then //复制文件成功
begin
str1:=extractfilename(frm_directory+'\'+frm_filename);
ChDir(direc);
// str1:=copy(str1,1,8)+'.db';
if renamefile(str1,str) then //备份文件该名成功
; end;
end;
except
exit;
end;
不过它不能备份当前更新数据