function CopyDir(psSource, psDesti :string; pfCallBack:TBrowseCallBackFunc):boolean; var tStack :TStringList; Status: Integer; SearchRec: TSearchRec; tsCurrent :string; tsDesti,tsTemp,tsTemp2 :string; begin result := false; tStack := TStringList.Create; tStack.Add(psSource); while tStack.Count >0 do begin tsCurrent := tStack[tStack.Count -1]; //出栈 tStack.Delete(tStack.Count -1);
tsTemp := Copy(tsCurrent, length(psSource)+1, 255); //相对路径 tsDesti := psDesti+iif(StrLeft(tsTemp,1)='\',','\')+ tsTemp; if not DirectoryExists(tsDesti) then CreateDir(tsDesti);
tsTemp := tsCurrent + iif(strright(tsCurrent,1)='\',','\')+'*.*'; Status := FindFirst(tsTemp, faAnyFile, SearchRec);
while Status = 0 do begin if (SearchRec.Attr and faDirectory = faDirectory) then //directory begin if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then begin tsTemp := tsCurrent+iif(strright(tsCurrent,1)='\',','\')+ SearchRec.Name; //取得绝对路径 tStack.Add(tsTemp); //进栈 end; end else //file begin tsTemp := tsCurrent+iif(strright(tsCurrent,1)='\',','\')+ SearchRec.Name; tsTemp2 := tsDesti+iif(strright(tsDesti,1)='\',','\')+ SearchRec.Name;
if @pfCallBack <> nil then pfCallBack(tsTemp);
CopyFile(PChar(tsTemp), PChar(tsTemp2), LongBool(1)); end; Status := FindNext(SearchRec); end; end; result := true; end;
function iif(b: boolean; v1, v2: variant): variant; begin if b then Result := v1 else Result := v2; end;
----------------------------------------------
www.acreport.com