• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Clearing out a directory

How can I delete all the files in a directory, leaving the directory there?
0
gwillson
Asked:
gwillson
  • 2
1 Solution
 
RadlerCommented:
A sample:

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;


T++, Radler.
0
 
gwillsonAuthor Commented:
Radler,
This doesn't work because the "FindFirstChildFile" is not defined.  Is that something you wrote?
0
 
rarigoCommented:
HI gwillson,

Function DeleteFiles(FilePath,FileMask: String): Boolean;
var
  DeleteFilesSearchRec: TSearchRec;
begin
  Result := False;
  FindFirst(FilePath+'\'+FileMask, faAnyFile, DeleteFilesSearchRec);
  If Not (DeleteFilesSearchRec.Name = '') Then
  Begin
    Result := True;
    DeleteFile(
      {$IFDEF WIN32}PChar({$ENDIF}
      FilePath+'\'+DeleteFilesSearchRec.Name
      {$IFDEF WIN32}){$ENDIF}
      );
    While True Do
    Begin
      If FindNext(DeleteFilesSearchRec)<0 Then
      Begin
        Break;
      End
      Else
      Begin
        DeleteFile(
          {$IFDEF WIN32}PChar({$ENDIF}
          FilePath+'\'+DeleteFilesSearchRec.Name
          {$IFDEF WIN32}){$ENDIF}
          );
      End;
    End;
  End;
End;


This function does what your looking for.


Tchau,
Reginaldo

0
 
gwillsonAuthor Commented:
Perfect!  Thanks for the help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now