Clearing out a directory

How can I delete all the files in a directory, leaving the directory there?
gwillsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gwillsonAuthor Commented:
Perfect!  Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.