Topic: An academic question on file deletions.

I have managed to write some code that is successful in deleting backup files, but I had hoped to delete files to a pattern. In this I have failed. I had thought of using the TRIM function, but of course that won't help with the actual names that are on the disk. Perhaps the only answer is to make my own equivalent of the Delphi class TSearchRec which of course is not available in Pascal script.


The files in the backup folder are named as so:


WOS_backup '+ FormatDateTime('dd-mm-yyyy hh-nn-ss',now)+'.db'

The backups occur on opening the program, every 3.5 hours and on close.  My thought was on opening the program a backup is made and then all the files with yesterdays date are deleted. In other words I need the file name to ignore the time content and delete the files with the day before's date.


The code below does not delete any files in the directory.

function DeleteDB(f: string): boolean;
var
    fileDelete : TStringList;
    fileName : string;
    cnt, indx: integer;
    Date : TDateTime;
begin

    result := True;
    fileDelete := TStringList.Create;
    fileDelete.Text := GetFilesList(f, '*.db',False);

    cnt :=  fileDelete.Count -1;
    
    date := Now - 1;
    fileName := DateToStr(date);
    fileName := 'WOS_backup '+ fileName + '*.db';
    
   // Timer.Enabled := False;

    for   indx := 0 to cnt do
      if fileDelete[indx] = filename then  // If I delete this line then all files in the directory get deleted.
        begin
       // DeleteFile(fileDelete[indx]);
        if not DeleteFile(fileDelete[indx]) then
        begin
           messageBox('Unable to delete file '+fileDelete[indx], 'Delete File', 0);
           result := False;
        end; 
     end;


   // Timer.Enabled := True;

   fileDelete.Free;

As noted above, if I remove the code segment if fileDelete[indx] = filename, then it does delete files.  So from a purely academic viewpoint does anyone have any thoughts on a way to ignore the time stamp in the file name?

Thank you in advance for any thoughts.

On a clear disk you can seek forever

2 (edited by derek 2020-06-12 20:31:14)

Re: An academic question on file deletions.

Hi Colin,
Not sure if I've understood the question correctly, but couldn't you, instead of storing all your backups in a single backup folder, create a sub-folder within the backup folder and name each sub-folder with the date.  Then you can simply use the 'createdir' and 'removedir' commands (along with any other stuff you want to do) to create and remove the appropriate sub-folders when your program starts and closes and circumvent the issue of the timestamps.
Please see the attached as a basic approach (obviously you'll want to change the directory paths).
Derek.

Post's attachments

Attachment icon createandremovedir.zip 374.15 kb, 263 downloads since 2020-06-12 

Re: An academic question on file deletions.

Good thinking, I'll give it a whirl! Thanks

On a clear disk you can seek forever

4 (edited by CDB 2020-06-13 13:33:21)

Re: An academic question on file deletions.

Sadly back to the drawing board!   I recoded everything to create and delete folders and then after it was working perfectly, I realised it wasn't practical for weekends or bank holidays, as obviously there would be more than 1 days break.


I think I'll have to go to counting files in the folder and deleting when files in the folder are greater than a number to be decided.


It will give me something to ponder as I wander off to bed.


The mere thought of bed has solved the problem - run a loop in the delete file function incrementing a days prior counter.

So first iteration is Day -1, second is Day-2, third is Day-3, etc.

On a clear disk you can seek forever

Re: An academic question on file deletions.

An update.


1. I have solved the date checking by having a backup table with the last date accessed. Read this on opening and then the last directory created can be found no matter how many days have elapsed since running the program.


2. RemoveDir cannot delete a directory when the folder contains files.

On a clear disk you can seek forever

Re: An academic question on file deletions.

OK, for anyone who wants to be able to delete a folder and files contained within, here is the code I used to get it working. The difficulty was because my folders are 4 folders deep and RemoveDir will not delete folders that are not empty.


Parameter 'F' contains the file path when the function is called.

function DeleteDB(f: string): boolean;
var
 fileDelete :TStringList;  //will contain the list of files in the folder
 filename : string;          // This will be assigned each file name found in the stringlist. 
 indx, cnt: integer;
begin

    result := True;
    fileDelete := TStringList.Create;
    fileDelete.Text := GetFilesList(f, '*.db',True);  //finds all the *.db files in the filepath
    indx :=  fileDelete.Count -1;   // number of files in the folder

   for cnt := 0 to indx  do
   //with filedelete do    -- 'with' will only work if folder is root. As we are 4 folders deep each file in filelist has to become individulised so we have to use a FOR loop

    begin
        filename := filedelete[cnt]; // take each file in the list and assign it to the string one file at a time
        DeleteFile(filename);  //delete the file
    end;

//Once the folder is empty  the directory/folder can now be deleted

    if DirectoryExists(f) then
    begin  
       result := RemoveDir(f);
         //showmessage('Z after delete ' + boolToStr(z));
    end
    else
    begin  //error message we should never get here 
        messageBox('Unable to delete file '+ f, 'Delete File', 0);
        result := False;
    end;

   fileDelete.Free;  // release the stringlist from memory

end;

The calling code

 dtToday := FormatDateTime('dd-mm-yyyy',now);
   dtYesterday := SQLExecute('SELECT dateBackup FROM backups WHERE id = 1'); // note the last date of a backup could be stored in a text file instead of a table

y := ExtractFilePath(application.ExeName);  // y is declared as a string. This gets the folder path of wherever your executable is living

 y := y + 'backup\backup ' + dtYesterday; // filepath to executable + folder 'Backup' + subfolder name = backup + date

if y < b then    // b contains 'today's' date so if the backup date in the database is less than today's date call the delete function
     DeleteDB(y);

Hope that is of some help to somebody

On a clear disk you can seek forever