Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Too many programs create files but never cleanup after them self why? Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Files Operation
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Bjarne Winkler

Yes that is a good question. In this short article I will try to motivate 
programmers to cleanup after their programs. 


In this short article I will try to motivate programmers to cleanup after their 

A program has several of files to keep track off in this busy world.  A program has 
supports files like the Ini file type.  The rule here is to remove old stuff from 
the file it is no longer is using.  You have a new release and you change the topic 
from one type to another.  Please remove the old one you know where and what - the 
user does not. 

A program can create files as an output or function of the program.  In general the 
rule is that the program that creates the program gives it to someone else (another 
program).  In a good world the "other" program now owns the files and should be the 
one that removes the files when no longer needed or outdated. 

A program can create log files.  This is to me always a real good idea to create 
log files.  The program should be able to run in three different modes: Full debug 
mode, log error mode, and absolute no logging at all.  One smart way of doing this 
is to create a folder structure lets say under the Exe location or user defined 
under setup.  Under the Log folder or whatever you call it create daily folders 
with the folder name of YYYYMMDD this way your program can easily delete older 
folders by simply reading the folder name.  You can select to keep all log files, 
delete all log files that is older than 30 - 60 - 90 days, or you can say I only 
want the last 7 folders.  The last option is great for programs that may only be 
used on weekly bases. 

If you are in full debug mode you can even let you program email you the log files, 
so you can monitor the progress of the program.  You can take this to a profiling 
level where you log every function and then you can see that your clients are 
really using and what is not that heavily used.  Very good for upgrades 

A trick regarding log files is to create them as ASCII comma delimited files (you 
can use the Commatext property in the TStringList).  With a CSV file you can use 
most database manager to massage the data in the file.  If you are not in the 
consulting business the CSV file can help you with your client.  If a client want a 
special report you can guide them to Excel and the book "Excel for dummies" and you 
clients can create reports till the paper runs out of the printer. 

Again please have a function that will cleanup old files.  Here is another 

The DeleteAllFilesOlderThan function takes either a path like "C:\MyProgram\" or a 
full filename like "C:\MyProgram\Tmp\*.Txt".  If the Date is "Now" then all the 
files in the path or with the filename will be deleted. 
2   {====================================================================}
4   function DeleteAllFilesOlderThan(const FileName: string; Date: TDateTime): Boolean;
5   {====================================================================}
6   var
7     SearchRec: TSearchRec;
8     sFile, sPath: string;
10  begin
11    Result := True;
12    sFile := ExpandFileName(FileName);
13    sPath := ExtractFilePath(sFile);
14    if FindFirst(sFile, faAnyFile, SearchRec) = 0 then
15    begin
16      if (SearchRec.Name <> '') and (SearchRec.Name <> '.') and (SearchRec.Name <> 
17  '..')
18        then
19      begin
20        if FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date then
21        begin
22          if not SysUtils.DeleteFile(sPath + SearchRec.Name) then
23          begin
24            Result := False;
25          end;
26        end;
27      end;
28      while FindNext(SearchRec) = 0 do
29      begin
30        if (SearchRec.Name <> '') and (SearchRec.Name <> '.') and (SearchRec.Name <>
31          '..') then
32        begin
33          if FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date then
34          begin
35            if not SysUtils.DeleteFile(sPath + SearchRec.Name) then
36            begin
37              Result := False;
38            end;
39          end;
40        end;
41      end;
42    end;
43    SysUtils.FindClose(SearchRec);
44  end;

I use this function as a base function for other functions like: 
46  {====================================================================}
48  function DeleteAllFilesOlderThan30Days(const FileName: string): Boolean;
49  {====================================================================}
50  begin
51    Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -1));
52  end;
54  {====================================================================}
56  function DeleteAllFilesOlderThan60Days(const FileName: string): Boolean;
57  {====================================================================}
58  begin
59    Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -2));
60  end;
62  {====================================================================}
64  function DeleteAllFilesOlderThan90Days(const FileName: string): Boolean;
65  {====================================================================}
66  begin
67    Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -3));
68  end;

The Delphi IncMonth works also with negative numbers so if "Now" is May 13 and you 
are using -2 you will be looking at March 13. 

So now your program should know.  Cleanup all the old files and files that the program no longer is using. 

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC