首页  编辑  

复制、删除文件和目录

Tags: /超级猛料/Stream.File.流、文件和目录/目录/   Date Created:

删除目录:

function DelDirectory(const Source:string): boolean;

var

 fo: TSHFILEOPSTRUCT;

begin

 FillChar(fo, SizeOf(fo), 0);

 with fo do

 begin

   Wnd := 0;

   wFunc := FO_DELETE;

   pFrom := PChar(source+#0);

   pTo := #0#0;

   fFlags := FOF_NOCONFIRMATION+FOF_SILENT;

 end;

 Result := (SHFileOperation(fo) = 0);

end;

复制目录:

///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!

//例如如果要复制E:\Temp整个目录到E:\那么代码为: copydirectory('e:\temp','e:\');

///如果要复制E:\Temp到E:\Test目录下面,那么代码为:CopyDirecotry('E:\Temp','E:\TEST');

function CopyDirectory(const Source, Dest: string): boolean;

var

 fo: TSHFILEOPSTRUCT;

begin

 FillChar(fo, SizeOf(fo), 0);

 with fo do

 begin

   Wnd := 0;

   wFunc := FO_COPY;

   pFrom := PChar(source+#0);

   pTo := PChar(Dest+#0);

   fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR    ;

 end;

 Result := (SHFileOperation(fo) = 0);

end;

重新命名:

用MoveFile()或者下面的函数也可以。

RenameFile('c:\a','c:\b')好想也可以?Win2K。

//RenDirectory('d:\wt2','d:\bcde');

function RenDirectory(const OldName,NewName:string): boolean;

var

 fo: TSHFILEOPSTRUCT;

begin

 FillChar(fo, SizeOf(fo), 0);

 with fo do

 begin

   Wnd := 0;

   wFunc := FO_RENAME;

   pFrom := PChar(OldName+#0);

   pTo := pchar(NewName+#0);

   fFlags := FOF_NOCONFIRMATION+FOF_SILENT;

 end;

 Result := (SHFileOperation(fo) = 0);

end;

//Copy 多个文件的处理:

function CopyFiles(const Source,Dest: string): boolean;

var

 fo: TSHFILEOPSTRUCT;

begin

 FillChar(fo, SizeOf(fo), 0);

 with fo do

 begin

   Wnd := 0;

   wFunc := FO_COPY;

   pFrom := @source[1];

   pTo :=pchar(dest);

   fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR    ;

 end;

 Result := (SHFileOperation(fo) = 0);

end;

procedure TForm1.Button1Click(Sender: TObject);

var

 str:string;

 i:integer;

begin

 if opendialog1.Execute then

 begin

   for i:=0 to OpenDialog1.Files.Count-1 do

    str:=str+OpenDialog1.Files.strings[i]+#0;

   showmessage(str);

   str:=str+#0;

   CopyFiles(str,'d:\temp');

 end;

end;

function ClearDirectory(const DirName: string; const IncludeSub, ToRecyle: Boolean): Boolean; stdcall;

{

 清除目录

}

var

 fo: TSHFILEOPSTRUCT;

begin

 FillChar(fo, SizeOf(fo), 0);

 with fo do

 begin

   Wnd := GetActiveWindow;

   wFunc := FO_DELETE;

   pFrom := PChar(DirName + '\*.*' + #0);

   pTo := #0#0;

   fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI

             or (Ord(not IncludeSub) * FOF_FILESONLY)

             or (ORd(ToRecyle) or FOF_ALLOWUNDO);

 end;

 Result := (SHFileOperation(fo) = 0);

end;