Solved

deleting files

Posted on 2001-06-26
10
141 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delphi custom sort exception 6 111
add combobox item based on numbers 9 100
find a node in VST 2 47
Working with hours 3 31
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now