该问题业已解决,与大家共享! var Form1: TForm1; FromDir,ToDir: pchar; implementation{$R *.dfm} procedure FileCopy(From, Dest: string); var T: TSHFileOpStruct;begin GetMem(FromDir,Length(Fromdir)+2); try GetMem(ToDir,Length(Dest)+2); try FillChar(FromDir^,Length(From)+2,0); FillChar(ToDir^,Length(Dest)+2,0); StrCopy(FromDir,PChar(From)); StrCopy(ToDir,PChar(Dest)); with T do begin Wnd :=0; wFunc :=FO_COPY; pFrom :=FromDir; pTo :=ToDir; fflags:=FOF_FILESONLY; //fFlags :=FOF_RENAMEONCOLLISION ; //FOF_NOCONFIRMATION; //or FOF_RENAMEONCOLLISION or (FOF_FILESONLY); fAnyOperationsAborted:=False; hNameMappings:=nil; lpszProgressTitle:=nil; if SHFileOperation(T)<>0 then raise Exception.Create('拷貝文件失敗!'); end; finally FreeMem(ToDir,Length(Dest)+2); end; finally FreeMem(FromDir,Length(From)+2); end; end; procedure TForm1.Button1Click(Sender: TObject); const SELDIRHELP = 2000; var dir: String; begin dir := 'q:'; if SelectDirectory(dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then fromdir:=pchar(dir+'\'+'*.*'); ToDir:='c:\test\'; FileCopy(pchar(fromdir),pchar(todir)); end;end.
//============================================================================== //拷贝目录(包括子目录一起拷贝)************************************************ //============================================================================== procedure XCopyDir(SourceDir, TargetDir: string); var DirInfo: TSearchRec; DosError: Integer; begin DosError := FindFirst(SourceDir+'\*.*', FaAnyfile, DirInfo); if not DirectoryExists(TargetDir) then ForceDirectories(TargetDir); while DosError=0 do begin if ((DirInfo.Attr and FaDirectory)=faDirectory) and (DirInfo.Name<>'.') and (DirInfo.Name<>'..') then XCopyDir(SourceDir + '\' + DirInfo.Name, TargetDir + '\' + DirInfo.Name); {$IF DEFINED(WIN32) AND DECLARED(UsingVCL)} if ((DirInfo.Attr and FaDirectory)<>FaDirectory) and ((DirInfo.Attr and FaVolumeID)<>FaVolumeID) {$ELSE} if ((DirInfo.Attr and FaDirectory)<>FaDirectory) {$IFEND} then CopyFile(PChar(SourceDir + '\' + DirInfo.Name), PChar(TargetDir + '\' + DirInfo.Name), false); DosError := FindNext(DirInfo); end; SysUtils.FindClose(DirInfo); end;
关于在相同分区下的文件拷贝问题!如需要源代码的可以留下MAIL.
var
Form1: TForm1;
FromDir,ToDir: pchar;
implementation{$R *.dfm}
procedure FileCopy(From, Dest: string);
var T: TSHFileOpStruct;begin
GetMem(FromDir,Length(Fromdir)+2);
try
GetMem(ToDir,Length(Dest)+2);
try
FillChar(FromDir^,Length(From)+2,0);
FillChar(ToDir^,Length(Dest)+2,0); StrCopy(FromDir,PChar(From));
StrCopy(ToDir,PChar(Dest));
with T do
begin
Wnd :=0;
wFunc :=FO_COPY;
pFrom :=FromDir;
pTo :=ToDir;
fflags:=FOF_FILESONLY;
//fFlags :=FOF_RENAMEONCOLLISION ;
//FOF_NOCONFIRMATION; //or FOF_RENAMEONCOLLISION or (FOF_FILESONLY);
fAnyOperationsAborted:=False;
hNameMappings:=nil;
lpszProgressTitle:=nil;
if SHFileOperation(T)<>0 then
raise Exception.Create('拷貝文件失敗!');
end;
finally
FreeMem(ToDir,Length(Dest)+2);
end;
finally
FreeMem(FromDir,Length(From)+2);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
SELDIRHELP = 2000;
var
dir: String;
begin
dir := 'q:';
if SelectDirectory(dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then
fromdir:=pchar(dir+'\'+'*.*');
ToDir:='c:\test\';
FileCopy(pchar(fromdir),pchar(todir));
end;end.
//拷贝目录(包括子目录一起拷贝)************************************************
//==============================================================================
procedure XCopyDir(SourceDir, TargetDir: string);
var DirInfo: TSearchRec;
DosError: Integer;
begin
DosError := FindFirst(SourceDir+'\*.*', FaAnyfile, DirInfo);
if not DirectoryExists(TargetDir) then ForceDirectories(TargetDir);
while DosError=0 do
begin
if ((DirInfo.Attr and FaDirectory)=faDirectory) and (DirInfo.Name<>'.') and (DirInfo.Name<>'..')
then XCopyDir(SourceDir + '\' + DirInfo.Name, TargetDir + '\' + DirInfo.Name);
{$IF DEFINED(WIN32) AND DECLARED(UsingVCL)}
if ((DirInfo.Attr and FaDirectory)<>FaDirectory) and ((DirInfo.Attr and FaVolumeID)<>FaVolumeID)
{$ELSE}
if ((DirInfo.Attr and FaDirectory)<>FaDirectory)
{$IFEND}
then CopyFile(PChar(SourceDir + '\' + DirInfo.Name), PChar(TargetDir + '\' + DirInfo.Name), false);
DosError := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);
end;
揭贴!