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

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
0
msmouse
Asked:
msmouse
  • 6
  • 3
1 Solution
 
EpsylonCommented:
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
 
EpsylonCommented:
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
 
kretzschmarCommented:
fast eps :-(
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
msmouseAuthor Commented:
Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
0
 
msmouseAuthor Commented:
Epsylon,

What do I add the procedure to?  Please be as specific as possible, I am just leaning Delphi.
0
 
EpsylonCommented:
> 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
 
EpsylonCommented:
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
 
msmouseAuthor Commented:
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
 
EpsylonCommented:
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
 
EpsylonCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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