Solved

deleting files

Posted on 2001-06-26
10
144 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:msmouse
  • 6
  • 3
10 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226962
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;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6226988
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;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6226999
fast eps :-(
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 1

Author Comment

by:msmouse
ID: 6227009
Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
0
 
LVL 1

Author Comment

by:msmouse
ID: 6227021
Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 6227053
> I am just leaning Delphi

Well, then be carefull with this function  :o)


Here's what the unit could look like:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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(sr.Name);
    until FindNext(sr) <> 0;
  end;
  FindClose(sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DeleteFiles('c:\somedir\');
end;

end.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6227068
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.
0
 
LVL 1

Author Comment

by:msmouse
ID: 6227221
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.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6227342
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;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6227346
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;
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question