方法1 unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,shellapi;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); var sh:TShFileOpStruct; dir:string; begin sh.Wnd:=handle; dir:='c:\test\';{要删的目录} sh.wFunc:=FO_DELETE; sh.pFrom:=PChar(dir+'*.*'+chr(0)); sh.pTo:=nil; sh.fFlags:=FOF_ALLOWUNDO; sh.hNameMappings:=nil; sh.lpszProgressTitle:='删除'; if (ShFileOperation(sh))<>0 then begin ShowMessage('删除错误'); end; end; end.方法2 procedure delallfilesinpath(path:string); //删除目录下*.* var sr:tsearchrec; begin if findfirst(path+'\*.*',faanyfile,sr)=0 then begin deletefile(path+'\'+sr.name); end ; while findnext(sr)=0 do begin deletefile(path+'\'+sr.name); end;end; 第一种方法是调用Windows的api. 可以做到和资源管理器中的表现一样。 第二种用的是递归删除文件的方法。
把第一种方法的 sh.fFlags:=FOF_ALLOWUNDO; 这一行,改成 sh.fFlags:=FOF_NOCONFIRMATION 其中 aFlags的可以取的其他值有: aFlags - a combination among the following FOF_ALLOWUNDO - Preserve undo information FOF_FILESONLY - Do not apply to directories FOF_MULTIDESTFILES - aTo specifies a file for every file in the aFrom list FOF_NOCONFIRMATION - User is not asked to press the yes button FOF_NOERRORUI - Do not display error messages FOF_NORECURSION - Do not parse sub-folders FOF_RENAMEONCOLLISION - Create a new file name if one with same exists already FOF_SILENT - Do not display a progress dialog
说明白点
是不是用delphi函数实现删除目录下的所有文件?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,shellapi;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
sh:TShFileOpStruct;
dir:string;
begin
sh.Wnd:=handle;
dir:='c:\test\';{要删的目录}
sh.wFunc:=FO_DELETE;
sh.pFrom:=PChar(dir+'*.*'+chr(0));
sh.pTo:=nil;
sh.fFlags:=FOF_ALLOWUNDO;
sh.hNameMappings:=nil;
sh.lpszProgressTitle:='删除';
if (ShFileOperation(sh))<>0 then
begin
ShowMessage('删除错误');
end;
end;
end.方法2
procedure delallfilesinpath(path:string); //删除目录下*.*
var
sr:tsearchrec;
begin
if findfirst(path+'\*.*',faanyfile,sr)=0 then
begin
deletefile(path+'\'+sr.name);
end ;
while findnext(sr)=0 do
begin
deletefile(path+'\'+sr.name);
end;end;
第一种方法是调用Windows的api. 可以做到和资源管理器中的表现一样。
第二种用的是递归删除文件的方法。
sh.fFlags:=FOF_ALLOWUNDO;
这一行,改成
sh.fFlags:=FOF_NOCONFIRMATION
其中 aFlags的可以取的其他值有:
aFlags - a combination among the following
FOF_ALLOWUNDO - Preserve undo information
FOF_FILESONLY - Do not apply to directories
FOF_MULTIDESTFILES - aTo specifies a file for every file
in the aFrom list
FOF_NOCONFIRMATION - User is not asked to press the yes
button
FOF_NOERRORUI - Do not display error messages
FOF_NORECURSION - Do not parse sub-folders
FOF_RENAMEONCOLLISION - Create a new file name if one
with same exists already
FOF_SILENT - Do not display a progress dialog