Ian Branch 128 Posted October 23, 2022 Hi Team, D11.2 patch 1, 32bit VCL Apps. I have this bit of legacy code.. ... // r := FindFirst(sTempFilesPath + '*.*', FaAnyfile, DirInfo); // 0 if files are found. // while r = 0 do begin if ((DirInfo.Attr and FaDirectory <> FaDirectory) and (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then if DeleteFile(pChar(sTempFilesPath + DirInfo.Name)) = false then Memo1.Lines.Add(' Unable to delete file ' + sTempFilesPath + DirInfo.Name + '!') else Memo1.Lines.Add(' File ' + sTempFilesPath + DirInfo.Name + ' successfully deleted.'); // r := FindNext(DirInfo); // end; // ... ... D11.2 is telling me FaVolumeId is deprecated.. To what? What replaces it? Alternatively, how should the above be now written? Regards & TIA Ian Share this post Link to post
PeterBelow 239 Posted October 23, 2022 I would rewrite this code using the routines from System.IOUtils. Use TDirectory.GetFiles to get a list of files in a directory you can then walk over. TPath.GetTempPath gets you the user's temp folder, TFile.Delete deletes a file. Use a try except block around this statement to catch a failure to delete a file. Share this post Link to post
Ian Branch 128 Posted October 23, 2022 Hi Peter, Tks for the pointer. Haven't used those before. I declared.. var Path: string; Filenames: TArray<string>; i: Integer; Then coded.. // Filenames := TDirectory.GetFiles(sTempFilesPath); // Memo1.Lines.Add('Now deleting ' + Length(Filenames).ToString + ' files found in the Database Temp Files Drive/Directory.'); // for i := 0 to Length(Filenames) - 1 do begin if DeleteFile(PChar(sTempFilesPath + Filenames[i])) = False then Memo1.Lines.Add(' Unable to delete file ' + sTempFilesPath + Filenames[i] + '!') else Memo1.Lines.Add(' File ' + sTempFilesPath + Filenames[i] + ' successfully deleted.'); // next; end; // Memo1.Lines.Add('The Database Temp Files Drive/Directory has been cleared.'); Memo1.Lines.Add(' '); // I used DeleteFile rather than TFile.Delete as the latter did not give me a boolean response for the Memo lines. Regards, Ian Share this post Link to post
Lajos Juhász 295 Posted October 23, 2022 7 hours ago, Ian Branch said: Alternatively, how should the above be now written? From the source: faVolumeID = $00000008 platform deprecated; // not used in Win32 Looks like it was removed from windows. Share this post Link to post
Ian Branch 128 Posted October 23, 2022 (edited) Hi Team, After further study it cleaned own to this.. // // Delete all files in the Server Temp Files Path. // if Trim(sTempFilesPath) <> '' then begin // DBE1.Close; // aFilenames := TDirectory.GetFiles(sTempFilesPath); // Memo1.Lines.Add('Now deleting ' + Length(aFilenames).ToString + ' files found in the Database Temp Files Drive/Directory.'); // for FileName in aFileNames do begin // if DeleteFile(FileName) = False then Memo1.Lines.Add(' Unable to delete file ' + FileName + '!') else Memo1.Lines.Add(' File ' + FileName + ' successfully deleted.'); // end; // Memo1.Lines.Add('The Database Temp Files Drive/Directory has been cleared.'); Memo1.Lines.Add(' '); // end; // Tks to all. Regards, Ian Edited October 23, 2022 by Ian Branch Share this post Link to post
Nigel Thomas 36 Posted October 24, 2022 if Trim(sTempFilesPath) <> '' then Trim *returns* the trimmed string, it doesn't trim it in place. Your code is still working on the non-trimmed sTempFilesPath. Share this post Link to post
Ian Branch 128 Posted October 24, 2022 Hi Nigel, Correct, the test is to ensure that sTempFilesPath actually has something in it. I suppose I could just as easily have used sTempFilesPath.IsEmpty. Ian Share this post Link to post
Fr0sT.Brutal 900 Posted October 25, 2022 11 hours ago, Ian Branch said: I suppose I could just as easily have used sTempFilesPath.IsEmpty. Quote Returns whether this 0-based string is empty (does not contain any characters). IOW, that method won't save you from paths containing only spaces Share this post Link to post