Link to home
Start Free TrialLog in
Avatar of msmouse
msmouse

asked on

deleting files

Does anyone know how I can brouse to a folder then delete all files (but not the Directory) with a certain date stamp?  Would it be easiest to use API or go with DirectoryListBox and FileListBox.  Any suggestions?

Mike
Avatar of Epsylon
Epsylon

procedure DeleteFiles(directory: PChar);
var sr: TSearchRec;
    sl: TStringList;
begin
  if FindFirst(directory + '*.*', faAnyFile, sr) = 0 then
  begin
    DeleteFile(sr.Name);
    while FindNext(sr) = 0 do
      DeleteFile(sr.Name);
  end;
  FindClose(sr);
end;
Or

procedure TForm1.DeleteFiles(directory: String);
var sr: TSearchRec;
begin
  if FindFirst(directory + '*.*', faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') then
        // only if newer than 1-1-2001
        if FileDateToDateTime(sr.Time) > EncodeDate(2001, 1, 1) then
          DeleteFile(sr.Name);
    until FindNext(sr) <> 0;
  end;
  FindClose(sr);
end;
Avatar of kretzschmar
fast eps :-(
Avatar of msmouse

ASKER

Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
Avatar of msmouse

ASKER

Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
ASKER CERTIFIED SOLUTION
Avatar of Epsylon
Epsylon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe this is a better and more flexible solution, because it will also delete subdirs:


unit Unit1;

interface

uses
  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}

function DeleteFileOrFolder(FileOrFolder: String): Boolean;
var
 OS: TSHFileOpStruct;
begin
 if Length(FileOrFolder)>0 then
 begin
   FillChar(OS, sizeof(OS),0);
   OS.pFrom := PChar(FileOrFolder + #0);
   OS.wFunc := FO_DELETE;
   OS.fFlags := FOF_NOCONFIRMATION or FOF_SILENT;
   Result := SHFileOperation(OS)=0;
 end
 else
   Result := false;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  // Delete all files and folders from 'SomeDir' but leave 'SubDir'
  DeleteFileOrFolder('C:\SomeDir\*.*');

  // Delete all files and folders from 'SomeDir' including 'SubDir'
  DeleteFileOrFolder('C:\SomeDir');
end;

end.
Avatar of msmouse

ASKER

Epsylon,

Any idea why the first 2 examples don't delete the files?  When I step thur the program it selects the files but will not delete them?  The last example works fine.
Oops, sorry. Use this function instead:


procedure DeleteFiles(directory: String);
var sr: TSearchRec;
begin
  sl := TStringList.Create;
  if FindFirst(directory + '*.*', faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Name <> '.') and (sr.Name <> '..') then
        // only if newer than 1-1-2001
        if FileDateToDateTime(sr.Time) > EncodeDate(2001, 1, 1) then
          DeleteFile(directory + sr.Name);
    until FindNext(sr) <> 0;
  end;
  FindClose(sr);
end;
procedure DeleteFiles(directory: String);
var sr: TSearchRec;
begin
 if FindFirst(directory + '*.*', faAnyFile, sr) = 0 then
 begin
   repeat
     if (sr.Name <> '.') and (sr.Name <> '..') then
       // only if newer than 1-1-2001
       if FileDateToDateTime(sr.Time) > EncodeDate(2001, 1, 1) then
         DeleteFile(directory + sr.Name);
   until FindNext(sr) <> 0;
 end;
 FindClose(sr);
end;