如何删除指定目录中的所有文件?(30分)

  • 主题发起人 主题发起人 sunylat
  • 开始时间 开始时间
S

sunylat

Unregistered / Unconfirmed
GUEST, unregistred user!
你好:
请问如何删除指定目录中的所有文件,不用删除目录?
 
procedure TForm1.Button1Click(Sender: TObject);
var SearchRec: TSearchRec;
begin
if FindFirst('c/aa/*.*', faAnyFile, SearchRec)=0 then
begin
deletefile(SearchRec.name);
while findnext(SearchRec)=0 do deletefile(SearchRec.name);
findclose(SearchRec);
end;
end;
 
为什么找不到文件用FindFirst('c/aa/*.*', faAnyFile, SearchRec)=0
 
//---------------删除目录树------------------------------------------------
FUNCTION FkDeleteTree(vFolder:STRING):Boolean;
VAR
srList: TSearchRec;
DirList: TStringList;
oFound,oTemp: Boolean;
i: integer;
BEGIN
oTemp:=True;
// 建立一个文件夹列表
DirList := TStringList.Create;
vFolder:=TRIM(vFolder);
IF vFolder[length(vFolder)]<>'/' THEN vFolder:=vFolder + '/';
// 生成文件夹列表
oFound:=FindFirst(vFolder+'*.*',(faDirectory+faHidden+faSysFile), srList) = 0;
WHILE oFound DO
BEGIN
IF (DirectoryExists(vFolder+srList.Name) and (srList.Name<>'.') AND (srList.Name<>'..')) THEN
DirList.Add(vFolder+srList.Name);
oFound :=(FindNext(srList)=0);
END;
FindClose(srList);
//查找当前目录的文件删除
oFound:=FindFirst(vFolder+'*.*',(faAnyFile+faHidden+faSysFile+faReadOnly), srList) = 0;
WHILE oFound DO
BEGIN
FileSetAttr(vFolder+srList.Name,0);
oTemp:=DeleteFile(vFolder+srList.Name) and oTemp;
oFound:=(FindNext(srList)=0);
END;
FindClose(srList);
//查找列表的子目录
FOR i := 0 TO DirList.Count-1 DO FkDeleteTree(DirList);
FileSetAttr(vFolder,0);
oTemp:=RemoveDir(vFolder) AND oTemp;
DirList.Free;
Result:=oTemp;
END;
//--------------------------------------------------------------
//--------------删除指定目录中的所有文件--------------------
FUNCTION FkDeleteAllFile(vFolder:STRING):Boolean;
VAR
srList: TSearchRec;
oFound,oTemp: Boolean;
i: integer;
BEGIN
oTemp:=True;
// 建立一个文件夹列表
DirList := TStringList.Create;
vFolder:=TRIM(vFolder);
IF vFolder[length(vFolder)]<>'/' THEN vFolder:=vFolder + '/';
//查找当前目录的文件删除
oFound:=FindFirst(vFolder+'*.*',(faAnyFile+faHidden+faSysFile+faReadOnly), srList) = 0;
WHILE oFound DO
BEGIN
FileSetAttr(vFolder+srList.Name,0);
oTemp:=DeleteFile(vFolder+srList.Name) and oTemp;
oFound:=(FindNext(srList)=0);
END;
FindClose(srList);
//查找列表的子目录
Result:=oTemp;
END;

 
笔误,少了个冒号。

procedure TForm1.ToolButton2Click(Sender: TObject);
var
SearchRec: TSearchRec;
dir: string;
begin
dir := 'c:/aa/';
if FindFirst(dir+'*.*', faAnyFile, SearchRec)=0 then
begin
deletefile(dir+SearchRec.name);
while findnext(SearchRec)=0 do deletefile(dir+SearchRec.name);
findclose(SearchRec);
end;
end;
 
uses Windows, Classes, ShellAPI;

const
FOF_DEFAULT_IDEAL = FOF_MULTIDESTFILES + FOF_RENAMEONCOLLISION + FOF_NOCONFIRMATION + FOF_ALLOWUNDO +
FOF_FILESONLY + FOF_NOCONFIRMMKDIR + FOF_NOERRORUI + FOF_SIMPLEPROGRESS;
FOF_DEFAULT_DELTREE = FOF_NOCONFIRMATION + FOF_ALLOWUNDO + FOF_NOERRORUI;
FOF_DEFAULT_COPY = FOF_NOCONFIRMATION + FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_NOERRORUI + FOF_MULTIDESTFILES;
FOF_DEFAULT_DELFILES = FOF_DEFAULT_DELTREE;

function ShellDeleteFiles( hWnd : THandle ; const DirName : string; Flags : FILEOP_FLAGS; WinTitle : PChar ) : integer;
{---------------------------------------------------------------------------------------------}
{Apaga arquivos/Diretorios atraves do shell do windows}
//Notas: Ver comentario sobre o uso de duplo #0 nos parametros de Origem e destino
var
FileOpShell : TSHFileOpStruct;
Oper : array[0..1024] of char;
begin
if WinTitle <> nil then begin
Flags:=Flags + FOF_SIMPLEPROGRESS;
end;
with FileOpShell do begin
wFunc:=FO_DELETE;
pFrom:=Oper;
pTo:=Oper; //pra garantir a rapadura!
fFlags:=Flags;
lpszProgressTitle:=WinTitle;
Wnd:=hWnd;
hNameMappings:=nil;
fAnyOperationsAborted:=False;
end;
StrPCopy( Oper, DirName );
StrCat(Oper, PChar( ExtractFileName( FindFirstChildFile( DirName )) ) );
Result:=0;
try
while Oper <> EmptyStr do begin
Result:=ShFileOperation( FileOpShell );
if FileOpShell.fAnyOperationsAborted then begin
Result:=ERROR_REQUEST_ABORTED;
break;
end else begin
if Result <> 0 then begin
Break;
end;
end;
StrPCopy(Oper, FindFirstChildFile( DirName ) );
end;
except
Result:=ERROR_EXCEPTION_IN_SERVICE;
end;
end;
 
如果确定没有子目录
用WinExec('cmd /k del d:/aa/*.* /q',SW_HIDE);
很快的不信你试试看
 
//==============================================================================
//删除目录(包括子目录一起删除)************************************************
//==============================================================================
procedure EraseDir(DirName:string);
var DirInfo: TSearchRec;
DosError: Integer;
begin
DosError := FindFirst(DirName+'/*.*', FaAnyfile, DirInfo);
while DosError=0 do
begin
if ((DirInfo.Attr and FaDirectory)=faDirectory) and (DirInfo.Name<>'.') and (DirInfo.Name<>'..')
then EraseDir(DirName + '/' + 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 DeleteFile(pChar(DirName + '/' + DirInfo.Name));
DosError := FindNext(DirInfo);
end;
SysUtils.FindClose(DirInfo);
RemoveDirectory(PChar(DirName));
end;
 
不用写这么多吧?!给人家一点思路就可以了。。
楼顶的贴来得简单
 
后退
顶部