List files in Directory lazarus / 61-Beginners ---------------------------------------------------------------------- jcaser1948 · 2017-11-25 18:52 I try to listen the files in a Directory. I took as basis the Program Example43;of frrepascal .org. Here the listing: PROCEDURE DirList( var Maske:String);   { This procedure  demonstrates the FindFirst function }   Var Info : TSearchRec;     Count : Longint; Begin    LowVideo;   Writeln ('Current Directory is : ',GetCurrentDir);   Count:=0;   If FindFirst ('*',faAnyFile and faDirectory,Info)=0 then     begin     Repeat       Inc(Count);       With Info do         begin         If (Attr and faDirectory) = faDirectory then           Write('Dir : ');         Writeln (Name:40,Size:15);         end;     Until FindNext(info)<>0;     end;   FindClose(Info);   WRITELN( 'EXISTING DATA FILES ON DIRECTORY :' );   Writeln ('Finished search. Found ',Count,' matches'); End;                                                              I get the compilation error "call by var for arg no 3 has to match exactly:Got TRawbyteSearchRec", expected SearchRec If I change  TSearchRec for SearchRec I get the compilation error  Incompatible Types got untyped, expected Int64 Is something wrong with te example Progamm 43?. Do I somethig wrong? ---------------------------------------------------------------------- molly · 2017-11-25 20:48 @jcaser1948: Repeating yourself ? Don't put DOS unit into your uses clause. Use sysutils.TSearchRec instead. Unless your code insist on using DOS unit. In case the latter then adapt your code according to the types used in unit DOS. Start with those used with DOS.findfirst where you can see that it requires to use type SearchRec (not TSearchRec). ---------------------------------------------------------------------- ASerge · 2017-11-25 20:58 and another remarks: Quote from: jcaser1948 on November 25, 2017, 07:52:26 pm..., faAnyFile and faDirectory, ... This is the same as single faDirectory. FindClose should be called in the "if" block. ---------------------------------------------------------------------- molly · 2017-11-25 21:08 As an additional note: do notice that DOS.findfirst() is a procedure, and not a function (therefor it does not return a value, which results in the other error mentioned). Quote from: jcaser1948 on November 25, 2017, 07:52:26 pmI try to listen the files in a Directory. I took as basis the Program Example43;of frrepascal .org. Is something wrong with te example Progamm 43?. I have no idea. Where exactly can i find/download programm43 ? QuoteDo I somethig wrong? Most probably, but there could be a flaw in the example. It seems that you are mixing Sysutils and DOS units in a way that doesn't satisfy the compiler. Alas we are unable to tell because you did not present a compilable example, only a snippet of that code you are using. edit: Ah, you meant example43 from the user manual that is listed by SysUtils.FindFirst ? That one compiles and works out of the box when used without _any_ modifications. That is, when using a common target such as windows/Linux. So either you are targeting another (very specific) platform or you have made modifications to the example. Which one is it ? Quote from: ASerge on November 25, 2017, 09:58:22 pmFindClose should be called in the "if" block. afaik there are two issues with that: a) not for DOS and b) not unless user is using a FPC compiler which honours that behaviour. The documentation is clear: QuoteRemark:      A FindFirst call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory loss. Which means: also call it when when findfirst failed. That is, if documentation is correct. ---------------------------------------------------------------------- ASerge · 2017-11-26 07:21 Quote from: molly on November 25, 2017, 10:08:00 pmRemark:      A FindFirst call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory loss. Which means: also call it when when findfirst failed. That is, if documentation is correct. See 0032012: Findfirst/findclose: if findfirst fails with error on handle, findclose should not be called. Lazarus 1.8RC5 already patched. ---------------------------------------------------------------------- Thaddy · 2017-11-26 08:51 Quote from: ASerge on November 26, 2017, 08:21:30 amQuote from: molly on November 25, 2017, 10:08:00 pmRemark:      A FindFirst call must always be followed by a FindClose call with the same Rslt record. Failure to do so will result in memory loss. Which means: also call it when when findfirst failed. That is, if documentation is correct. See 0032012: Findfirst/findclose: if findfirst fails with error on handle, findclose should not be called. Lazarus 1.8RC5 already patched. That's my report. I don't know if it is back-ported, but it is fixed in FPC trunk. If findfirst fails, you should not call findnext - or findclose. ---------------------------------------------------------------------- tudi_x · 2017-11-26 08:55 @jcaser1948 maybe useful the below example of deleting a folder: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TZipper.DeleteFolder(const AName: string);var  F: TSearchRec; begin  if FindFirst(AName + '\*.*', faAnyFile, F) = 0 then  begin    try    repeat        if (F.Attr and faDirectory <> 0) then        begin         if (F.Name <> '.') and (F.Name <> '..') then          begin          DeleteFolder(AName + '\' + F.Name);          end;        end      else        begin          if (F.Attr and faReadOnly) > 0 then        begin            FileSetAttr(AName + '\' + F.Name, F.Attr - faReadOnly);          end;           DeleteFile(AName + '\' + F.Name);        end;       until FindNext(F) <> 0;    finally      FindClose(F);    end;  end;  RemoveDir(AName);end; ---------------------------------------------------------------------- jcaser1948 · 2017-11-26 16:39 Thanks to everione.I try to update an old Motor calculations program I wrote about 25 years agoNow that I am retired I find the time and the muse to do this .BuT I have plente of problems with reading and write files on the Disk, something was was not so difficult in Turpopascal 3 ---------------------------------------------------------------------- molly · 2017-11-26 17:03 @ASerge @Thaddy: Thanks for the info. I was not sure it was already addressed. I am assuming TS uses latest official release (or older). @jcaser1948 In that case the example as shown in sysutils.findfirst should work for you out of the box. But you use that snippet inside your code and that is what is causing problems for you. I am assuming you can't live without unit DOS but still would like to use SysUtils findfirst/findnext and findclose functions. In that case be explicit when calling those functions and use: SysUtils.FindFirst(), SysUtils.FindNext() and SysUtils.FindClose. That will tell the compiler that you explicitly wish to use these functions from unit SysUtils (and not unit DOS).