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.