Please clean up your space !

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 programs. A program has several of files to keep track

of 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 information.



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 solution.



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.



{===================================================================}

Function DeleteAllFilesOlderThan(const FileName: string;

  Date: TDateTime): Boolean;

{===================================================================}

var

  SearchRec: TSearchRec;

  sFile, sPath: String;

begin

  Result := True;


  sFile := ExpandFileName(FileName);

  sPath := ExtractFilePath(sFile);

  If FindFirst(sFile, faAnyFile, SearchRec) = 0 Then

  Begin

    If (SearchRec.Name <> '') And (SearchRec.Name <> '.') And

      (SearchRec.Name <> '..') Then

    Begin

      If FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date Then

      Begin

        If Not SysUtils.DeleteFile(sPath + SearchRec.Name) Then

        Begin

          Result := False;

        End;

      End;

    End;



    While FindNext(SearchRec) = 0 Do

    Begin

      If (SearchRec.Name <> '') And (SearchRec.Name <> '.') And

       (SearchRec.Name <> '..') Then

      Begin

        If FileDateToDateTime(FileAge(sPath + SearchRec.Name)) < Date Then

        Begin

          If Not SysUtils.DeleteFile(sPath + SearchRec.Name) Then

          Begin

            Result := False;

          End;

        End;

      End;

    End;

  End;

  

  SysUtils.FindClose(SearchRec);

end;



I use this function as a base function for other functions like:



{==================================================================}

Function DeleteAllFilesOlderThan30Days(const FileName: string): Boolean;

{==================================================================}

Begin

  Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -1));

End;

 

{===================================================================}

Function DeleteAllFilesOlderThan60Days(const FileName: string): Boolean;

{===================================================================}

Begin

  Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -2));

End;



{===================================================================}

Function DeleteAllFilesOlderThan90Days(const FileName: string): Boolean;

{===================================================================}

Begin

  Result := DeleteAllFilesOlderThan(FileName, IncMonth(Now, -3));

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.

 

Share this article!

Follow us!

Find more helpful articles: