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.
4 function DeleteAllFilesOlderThan(const FileName: string; Date: TDateTime): Boolean;
7 SearchRec: TSearchRec;
8 sFile, sPath: string;
11 Result := True;
12 sFile := ExpandFileName(FileName);
13 sPath := ExtractFilePath(sFile);
14 if FindFirst(sFile, faAnyFile, SearchRec) = 0 then
16 if (SearchRec.Name <> '') and (SearchRec.Name <> '.') and (SearchRec.Name <>
20 if FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date then
22 if not SysUtils.DeleteFile(sPath + SearchRec.Name) then
24 Result := False;
28 while FindNext(SearchRec) = 0 do
30 if (SearchRec.Name <> '') and (SearchRec.Name <> '.') and (SearchRec.Name <>
31 '..') then
33 if FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date then
35 if not SysUtils.DeleteFile(sPath + SearchRec.Name) then
37 Result := False;
I use this function as a base function for other functions like:
48 function DeleteAllFilesOlderThan30Days(const FileName: string): Boolean;
51 Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -1));
56 function DeleteAllFilesOlderThan60Days(const FileName: string): Boolean;
59 Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -2));
64 function DeleteAllFilesOlderThan90Days(const FileName: string): Boolean;
67 Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -3));
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.