?
Solved

Deleting all files of a certain file type - URGENT

Posted on 2007-07-25
9
Medium Priority
?
154 Views
Last Modified: 2010-04-05
How can I delete all .tmp files in my application directory please?

Thanks

- ST3VO
0
Comment
Question by:ST3VO
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:dinilud
ID: 19564246
function DeleteFiles(RootDir;FileType:String):Boolean;
var sr: TSearchRec;ParentDir:String;
begin
  if RootDir[length(RootDir)]<>'\' then RootDir:=RootDir+'\';
  ChDir(RootDir);
  if FindFirst(FileType, faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faReadOnly)=faReadOnly then
         SetFileAttributes(pchar(RootDir+sr.Name),sr.Attr and $FE);
      Result:= DeleteFile(RootDir+sr.Name);
    until (not Result) or (FindNext(sr) <> 0);
    FindClose(sr);
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
   DeleteFiles(ExtractFileDir(Application.ExeName),'*.tmp');
end;
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19564262
procedure DeleteTMPFiles;
var OwnDir: string;
    SearchRec: TSearchRec;
begin
 // first get our applicationdirectory
 OwnDir := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName));

 // search for all *.tmp files here and delete them
 if FindFirst(OwnDir + '*.tmp', faAnyFile, SearchRec) = 0 then
 repeat
  if not DeleteFile(OwnDir + SearchRec.Name) then
   ShowMessage('can''t delete ' + OwnDir + SearchRec.Name);
 until FindNext(SearchRec) <> 0;

 // close the handle
 FindClose(SearchRec);
end;
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19564267
hmm, should have checked if there was no new post before posting my solution.

Be aware though, dinilud's solution changes the current directory!
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.

 
LVL 10

Accepted Solution

by:
dinilud earned 2000 total points
ID: 19564278
orry some error

function DeleteFiles(RootDir,FileType:String):Boolean;
var sr: TSearchRec;ParentDir:String;
begin
  if RootDir[length(RootDir)]<>'\' then RootDir:=RootDir+'\';
  ChDir(RootDir);
  if FindFirst(FileType, faAnyFile, sr) = 0 then
  begin
    repeat
      if (sr.Attr and faReadOnly)=faReadOnly then
         SetFileAttributes(pchar(RootDir+sr.Name),sr.Attr and $FE);
      Result:= DeleteFile(RootDir+sr.Name);
    until (not Result) or (FindNext(sr) <> 0);
    FindClose(sr);
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
   DeleteFiles(ExtractFileDir(Application.ExeName),'*.tmp');
end;
0
 

Author Comment

by:ST3VO
ID: 19564282
Perfect!!!

Thanks!!!!

-ST3VO
0
 

Author Comment

by:ST3VO
ID: 19564285
Yes...I spotted the mistype and changed it to the comma... :) No a problem!!!

0
 
LVL 10

Expert Comment

by:dinilud
ID: 19564291
Actually there is no changing of directory due to this line
   ChDir(ExtractFileDir(Application.ExeName))
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19564296
be ware of the changed current dir, might give surprises.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 19564299
I don't see that line in your code
0

Featured Post

Independent Software Vendors: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

840 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