Recent

Author Topic: How to fix this simple delete function that worked fine in Delphi 5  (Read 680 times)

QuinnMartin

  • New Member
  • *
  • Posts: 27
I have this simple all-around delete function that works great in Delphi 5 but won't work in Lazarus.

Code: Pascal  [Select][+][-]
  1. procedure DeleteFiles(spath:string; sfile:string);
  2. var
  3.   MySearch: TSearchRec;
  4. begin
  5.   FindFirst(spath+sfile, faAnyFile+faReadOnly, MySearch);
  6.   DeleteFile(spath+MySearch.Name);
  7.   while FindNext(MySearch)=0 do
  8.     begin
  9.       DeleteFile(spath+MySearch.Name);
  10.     end;
  11.   FindClose(MySearch);
  12.   Application.ProcessMessages;
  13. end;  


This doesn't compile in Lazarus, because DeleteFile expects pchar instead of a string, and FindClose expects a longword instead of TSearchRec.

What would be a proper way to rewrite this?  Ideally I'd like it to still work if ported to Linux.

Just to be clear, the function above isn't necessarily for deleting a single file but can accept multiple files if you pass it a wildcard for the filename.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: How to fix this simple delete function that worked fine in Delphi 5
« Reply #1 on: August 17, 2022, 06:45:10 pm »
I have no problem compiling your source code using my Lazarus 2.2.0 64-bit GTK2 on Ubuntu Mate.

Not sure but maybe you need to use
{$mode objfpc}{$H+}

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: How to fix this simple delete function that worked fine in Delphi 5
« Reply #2 on: August 17, 2022, 06:47:15 pm »
This doesn't compile in Lazarus, because DeleteFile expects pchar instead of a string, and FindClose expects a longword instead of TSearchRec.
Remove Windows from uses or move it before SysUtils.
Code: Pascal  [Select][+][-]
  1. procedure DeleteFiles(const APath, AMask: string);
  2. var
  3.   Path: string;
  4.   R: TSearchRec;
  5. begin
  6.   Path := IncludeTrailingPathDelimiter(APath);
  7.   if FindFirst(Path + AMask, faAnyFile, R) = 0 then
  8.   try
  9.     repeat
  10.       DeleteFile(Path + R.Name);
  11.     until FindNext(R) <> 0;
  12.   finally
  13.     FindClose(R);
  14.   end;
  15. end;

QuinnMartin

  • New Member
  • *
  • Posts: 27
Re: How to fix this simple delete function that worked fine in Delphi 5
« Reply #3 on: August 17, 2022, 07:53:19 pm »
I moved Windows before SysUtils and it seems to work now.  Thank you.  I will also try that code you suggested, ASerge.

Does the last unit in the uses clause take priority?  I still haven't completed compiling and I'm no longer getting errors, but I hovered over DeleteFile() and it says that it's referencing sysutils\filutil.h, which comes after Windows now.  Googling around I found someone who said if there are name collisions, the last unit in the Uses list wins, so that sounds right.

I also looked at the header and I think the Lazarus built-in code importer shuffled the uses ordering around, which caused this problem, so maybe I need to watch for that.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to fix this simple delete function that worked fine in Delphi 5
« Reply #4 on: August 18, 2022, 09:09:25 am »
Does the last unit in the uses clause take priority?  I still haven't completed compiling and I'm no longer getting errors, but I hovered over DeleteFile() and it says that it's referencing sysutils\filutil.h, which comes after Windows now.  Googling around I found someone who said if there are name collisions, the last unit in the Uses list wins, so that sounds right.

Yes, identifiers are searched right-to-left in the order of the units in the uses-clauses (and in the implementation-section the uses-clause of that takes precedence to that of the interface-section).

 

TinyPortal © 2005-2018