Solved

deleting files

Posted on 2001-06-26
10
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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