文件夹的复制或剪切(100分)

  • 主题发起人 主题发起人 jb040303
  • 开始时间 开始时间
J

jb040303

Unregistered / Unconfirmed
GUEST, unregistred user!
我要实现文件夹(包括子文件夹)从c:/A复制或剪切到c:/B,请问如何实现啊!帮帮忙啊!
 
复制目录dir1到目录dir2
//要加上ShellAPI单元
procedure DirectoryCopy( dir1:string; dir2:string);
var
a: _WIN32_FIND_DATA;
b: longint;
temp: string;
go: TSHFileOPSTRUCT;
begin
temp:= dir1; //文件目录
if (not DirectoryExists(dir2)) then CreateDir(dir2);
b:= findFirstFile(PChar(temp+'/*.*'),a);
if(Trim(a.cFileName)<>'.')and(Trim(a.cFileName)<>'..')and(Trim(a.cFileName)<>'') then
begin
go.Wnd:=0;
go.wFunc:= FO_COPY ;
go.fFlags:= FOF_SILENT ;
go.pFrom:= PChar(temp+'/'+a.cFileName);
go.pTo:= PChar(dir2+'/'+a.cFileName);
SHFileOperation(go);
end;
while(FindNextFile(b,a)=true) do
begin
if(Trim(a.cFileName)<>'.')and(Trim(a.cFileName)<>'..')and(Trim(a.cFileName)<>'')then
begin
go.Wnd:= 0;
go.wFunc:= FO_COPY ;
go.fFlags:= FOF_SILENT ;
go.pFrom:= PChar(temp+'/'+a.cFileName);
go.pTo:= PChar(dir2+'/'+a.cFileName);
SHFileOperation(go);
end;
if((a.dwFileAttributes=49)or(a.dwFileAttributes=48)or(a.dwFileAttributes=16))
and(Trim(a.cFileName)<>'.')and(Trim(a.cFileName)<>'..') then
begin
DirectoryCopy(Trim(temp)+'/'+a.cFileName ,dir2+'/'+a.cFileName);
end;
end;
end;
 
function TForm1.CopyDir(const AFrom, ATo: String): Boolean;
var
F: TSHFileOpStruct;
begin
Result := False;
if ((AFrom = '') or (ATo = '')) then Exit;
with F do begin
Wnd:=0;
wFunc := FO_Copy;
pFrom := PChar(AFrom);
pTo := PChar(ATo);
fFlags := FOF_NOCONFIRMATION;
hNameMappings := nil;
lpszProgressTitle := PChar('正在复制文件夹"'+ AFrom + '"到"' + ATo + '"');
fAnyOperationsAborted := False;
end;
Result := (SHFileOperation(F) = 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
CopyDir('c:/a','c:/b');
end;
 
我是想把A文件夹下的所有文件拷贝到B文件夹下,并不是连同A文件夹也一起复制
 
不会啊,我上面的程序不会将A文件夹也复制啊
 
uses ShellAPI;

procedure CopyFile(Handle:THandle;Source, Dest : string);
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
fillChar(OpStruc, Sizeof(OpStruc), 0);
FillChar(frombuf, Sizeof(frombuf), 0);
FillChar(tobuf, Sizeof(tobuf), 0);
StrPCopy(frombuf, Source);
StrPCopy(tobuf, Dest);
With OpStruc DO Begin
Wnd:= Handle;
wFunc:= FO_COPY; //or FO_MOVE or FO_DELETE
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
ShFileOperation( OpStruc );
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
CopyFile(self.Handle, 'C:/A/*.*','C:/B/');
end;
 
按我的经验,直接用WindowsAPI最简单,为什么要这么复杂
function WinCopyFile(pForm, pTo: string; //复制整个目录,可用通配符(不知为什么空目录不能复制)(注意:CopyFile不能覆盖只读文件)
pFlags: FILEOP_FLAGS=FOF_NOCONFIRMMKDIR OR FOF_NOCONFIRMATION OR FOF_SILENT): boolean;
function DeleteToRecycleBin(WindowHandle: THandle; Filename: string): Boolean; //删除文件到回收站
function ExDeleteFile(const FileName: string): Boolean; //只读等文件属性的文件也可以删除
function IsFileInUse(fName : string ) : boolean; //检测文件是否正在被使用

function WinCopyFile(pForm, pTo: string; pFlags: FILEOP_FLAGS): boolean;
var
r: integer;
fData: TShFileOpStruct;
begin
fData.pFrom:=PChar(pForm+#0#0); //重要注释:不加#0#0会偶而出错
fData.pTo:=PChar(pTo+#0#0);
fData.wFunc:=FO_COPY;
fData.Wnd:=Application.Handle;
fData.lpszProgressTitle:=nil; //'正在复制'
fData.fFlags:=pFlags;
r:=SHFileOperation(fData);
result:=(r=0);
end;

function DeleteToRecycleBin(WindowHandle: THandle; Filename: string): Boolean;
var
FromName: PChar;
SH: TSHFILEOPSTRUCT;
begin
FromName := PChar(Filename);
FillChar(SH, SizeOf(SH),0);
with SH do begin
Wnd := WindowHandle;
wFunc := FO_DELETE;
pFrom := FromName;
StrCat(pFrom, #0);
fFlags:= FOF_SILENT or FOF_ALLOWUNDO;
end;
Result := SHFileOperation(SH) = 0;
end;

function ExDeleteFile(const FileName: string): Boolean; //只读等文件属性的文件也可以删除
begin
Result:=false;
if not FileExists(FileName) then exit;
FileSetAttr(FileName, (not SysUtils.faReadOnly) and (not SysUtils.faHidden) and (not SysUtils.faSysFile));
Result:=DeleteFile(FileName);
end;

function IsFileInUse(fName : string ) : boolean;
var
HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then exit;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
 
谢谢zuodan大侠,我还想在复制文件上加上文件扩展名和文件创建日期的限制,请问如何实现啊
 
后退
顶部