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
Mike
Or
procedure TForm1.DeleteFiles(directo ry: 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.DeleteFiles(directo
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
DeleteFile(sr.Name);
until FindNext(sr) <> 0;
end;
FindClose(sr);
end;
fast eps :-(
ASKER
Epsylon,
What do I add the procedure to? Please be as specific as possible, I am just leaning Delphi.
What do I add the procedure to? Please be as specific as possible, I am just leaning Delphi.
ASKER
Epsylon,
What do I add the procedure to? Please be as specific as possible, I am just leaning Delphi.
What do I add the procedure to? Please be as specific as possible, I am just leaning Delphi.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(FileOrF older: 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:\Som eDir\*.*') ;
// Delete all files and folders from 'SomeDir' including 'SubDir'
DeleteFileOrFolder('C:\Som eDir');
end;
end.
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(FileOrF
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
begin
// Delete all files and folders from 'SomeDir' but leave 'SubDir'
DeleteFileOrFolder('C:\Som
// Delete all files and folders from 'SomeDir' including 'SubDir'
DeleteFileOrFolder('C:\Som
end;
end.
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.
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
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
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;
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
DeleteFile(directory + sr.Name);
until FindNext(sr) <> 0;
end;
FindClose(sr);
end;
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;