Recent

Author Topic: Listfiles does not compile  (Read 4072 times)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Listfiles does not compile
« on: November 16, 2017, 01:25:48 pm »
 
Hi I am new to Lazarus
I try to compile  the following procedure, to list the files of a directory (Linux)

PROCEDURE ListFiles ( var Maske:AnsiString);
 Var Info : SearchRec;
    Count : Longint;

Begin
  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 ('Finished search. Found ',Count,' matches');

End;       
       I get the error Incompatible Types: Got "untyped" but expected "Int64"
If I compile with TSearchRec; instead of SearchRec; does not work either Error Call by var for  arg no. 3 has to match exactly :Got TRawbyteSearchRec, expected SearchRec;
I am very puzzled, since I took the template for this procedure from a Lazarus programm

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Listfiles does not compile
« Reply #1 on: November 16, 2017, 01:51:10 pm »
Remove unit dos, and change info:searchrec to info:tsearchrec;

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Listfiles does not compile
« Reply #2 on: November 17, 2017, 11:47:02 pm »
The logic  could be better too..

Count := 0;
LastResult := FindFirst(.....);
While lastResult = 0 do
 begin
  Inc( Count);
    // Process the current find or current find.
  LastResult := FindNext(.....);
End;
FindClose(....);

Etc.



 
 
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Listfiles does not compile
« Reply #3 on: November 18, 2017, 09:35:52 am »
The logic  could be better too..
Better?
In my opinion, what was earlier is better (no need additional lastResult). And it's even better to add a try-finally:
Code: Pascal  [Select][+][-]
  1. Count := 0;
  2. if FindFirst(...) = 0 then
  3. try
  4.   repeat
  5.     Inc(Count);
  6.    // Process the current find.
  7.   until FindNext(...) <> 0;
  8. finally
  9.   FindClose(...);
  10. end;



jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Listfiles does not compile
« Reply #4 on: November 19, 2017, 02:13:40 am »
Really ?

 What if I want a quick reference to the last error returned, which is what the return is.?
 
 Using a results variable works for Error logging and Loop control, if FINDFirst fails, you don't
bother to enter the loop and you have a IO status. If FindNext fails, you still have a loop control
of the same variable, still reporting the same data.

  I suppose one could use the GetlastError but I am sure that only works with windows...

  Just my way of coding, its worked well for me  ;)

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Listfiles does not compile
« Reply #5 on: November 19, 2017, 11:07:19 am »
  I suppose one could use the GetlastError but I am sure that only works with windows...
GetLastOsError is cross-platform..... It is in sysutils.
Specialize a type, not a var.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Listfiles does not compile
« Reply #6 on: November 21, 2017, 11:56:56 pm »
Well that's good to know, I try very hard to not use API calls...Otherwise I need to put in
conditional statements for other targets.
The only true wisdom is knowing you know nothing

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Listfiles does not compile
« Reply #7 on: November 25, 2017, 08:04:24 pm »
 ;D Thanks to everione everyone,  It seems to work

 

TinyPortal © 2005-2018