FPC: recursive "FileFind" 
Author Message
 FPC: recursive "FileFind"

In Linux(dist; RedHat 6.2):

This is how I'm used to do recursive "FileFind":
---------------------------------
procedure FindDirs(ADir: string);
var
  sr: TSearchRec;
  e: LongInt;
begin
  ADir := AddSlash(ADir);
  e := FindFirst(ADir + '*', faDirectory, sr);
  while e = 0 do
  begin
    if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
sr.Name)) and (sr.Name[1] <> '.')  then
      FindDirs(ADir + sr.Name);
    e := FindNext(sr);
  end;
  FindClose(sr);
end;
---------------------------------
When I do my procedure call "FindDirs(ADir + sr.Name);" the "while"
loop breaks/exits.
Isn't a new instance of the procedure started and a new local variable
created?

Now I feeling stupid doing monsters like this:
---------------------------------
procedure FindDirs(ADir: string);

  procedure DoRest(_ADir: string);
  var
    sr: TSearchRec;
    e: LongInt;
    Strs: TStrings;
    i: integer;
  begin
    _ADir := AddSlash(_ADir);
    if SetCurrentDir(_ADir) then
    begin
      Strs := TStringList.Create;
      try
        e := FindFirst('*',faDirectory, sr);
        while e = 0 do
        begin
          if ((sr.Attr and faDirectory) <> 0) and (not IsLink(_ADir +
sr.Name)) and (sr.Name[1] <> '.') then
            Strs.Add(_ADir + sr.Name);
          e := FindNext(sr);
        end;
        FindClose(sr);
        if Strs.Count > 0 then
          with Strs do
            for i:=Count-1 downto 0 do
              FindDirs(Strings[i]);
      finally
        Strs.Free;
      end;
    end;
  end;

var
  sr: TSearchRec;
  e: LongInt;
  Strs: TStrings;
  i: integer;
begin
  ADir := AddSlash(ADir);
  if SetCurrentDir(ADir) then
  begin
    Strs := TStringList.Create;
    try
      e := FindFirst('*', faDirectory, sr);
      while e = 0 do
      begin
        if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
sr.Name)) and (sr.Name[1] <> '.')  then
          Strs.Add(ADir + sr.Name);
        e := FindNext(sr);
      end;
      FindClose(sr);
    finally
      Strs.Free;
    end;
  end;
end;

-----------------------------------------------------------
:::::: remove x:es from email address
-----------------------------------------------------------
:::::: Sonket Dev: http://www.*-*-*.com/ ~u32102551/
:::::: Yo!Utilities II: http://www.*-*-*.com/ ~u32103496/
-----------------------------------------------------------



Thu, 20 Mar 2003 03:00:00 GMT  
 FPC: recursive "FileFind"

Quote:

>In Linux(dist; RedHat 6.2):
>end;
>---------------------------------
>When I do my procedure call "FindDirs(ADir + sr.Name);" the "while"
>loop breaks/exits.
>Isn't a new instance of the procedure started and a new local variable
>created?

Note that there are two findfirst, one BP compatible (in unit DOS where it
should), and one Delphi compatible(in sysutils, where it should :-).

For the first success is tested with (doserror=0), the second
(returnvalue_of_findfirst=0)



Fri, 21 Mar 2003 14:38:21 GMT  
 FPC: recursive "FileFind"
Something to think about -- In C, the distinction between functions and
procedures is somewhat blurred, as everything returns a value.  In Pascal,
a procedure does NOT return a value (except, perhaps, through a VAR
parameter); a function does.  Thus embedding a recursive procedure call
within a WHILE loop may not do what you expect.

Bob Schor

Quote:

> In Linux(dist; RedHat 6.2):

> This is how I'm used to do recursive "FileFind":
> ---------------------------------
> procedure FindDirs(ADir: string);
> var
>   sr: TSearchRec;
>   e: LongInt;
> begin
>   ADir := AddSlash(ADir);
>   e := FindFirst(ADir + '*', faDirectory, sr);
>   while e = 0 do
>   begin
>     if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
> sr.Name)) and (sr.Name[1] <> '.')  then
>       FindDirs(ADir + sr.Name);
>     e := FindNext(sr);
>   end;
>   FindClose(sr);
> end;
> ---------------------------------
> When I do my procedure call "FindDirs(ADir + sr.Name);" the "while"
> loop breaks/exits.
> Isn't a new instance of the procedure started and a new local variable
> created?

> Now I feeling stupid doing monsters like this:
> ---------------------------------
> procedure FindDirs(ADir: string);

>   procedure DoRest(_ADir: string);
>   var
>     sr: TSearchRec;
>     e: LongInt;
>     Strs: TStrings;
>     i: integer;
>   begin
>     _ADir := AddSlash(_ADir);
>     if SetCurrentDir(_ADir) then
>     begin
>       Strs := TStringList.Create;
>       try
>         e := FindFirst('*',faDirectory, sr);
>         while e = 0 do
>         begin
>           if ((sr.Attr and faDirectory) <> 0) and (not IsLink(_ADir +
> sr.Name)) and (sr.Name[1] <> '.') then
>             Strs.Add(_ADir + sr.Name);
>           e := FindNext(sr);
>         end;
>         FindClose(sr);
>         if Strs.Count > 0 then
>           with Strs do
>             for i:=Count-1 downto 0 do
>               FindDirs(Strings[i]);
>       finally
>         Strs.Free;
>       end;
>     end;
>   end;

> var
>   sr: TSearchRec;
>   e: LongInt;
>   Strs: TStrings;
>   i: integer;
> begin
>   ADir := AddSlash(ADir);
>   if SetCurrentDir(ADir) then
>   begin
>     Strs := TStringList.Create;
>     try
>       e := FindFirst('*', faDirectory, sr);
>       while e = 0 do
>       begin
>         if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
> sr.Name)) and (sr.Name[1] <> '.')  then
>           Strs.Add(ADir + sr.Name);
>         e := FindNext(sr);
>       end;
>       FindClose(sr);
>     finally
>       Strs.Free;
>     end;
>   end;
> end;

> -----------------------------------------------------------
> :::::: remove x:es from email address
> -----------------------------------------------------------
> :::::: Sonket Dev: http://w1.321.telia.com/~u32102551/
> :::::: Yo!Utilities II: http://w1.321.telia.com/~u32103496/
> -----------------------------------------------------------



Fri, 21 Mar 2003 03:00:00 GMT  
 FPC: recursive "FileFind"


Quote:
>>When I do my procedure call "FindDirs(ADir + sr.Name);" the "while"
>>loop breaks/exits.
>>Isn't a new instance of the procedure started and a new local variable
>>created?

>Note that there are two findfirst, one BP compatible (in unit DOS where it
>should), and one Delphi compatible(in sysutils, where it should :-).

ahhhhhhh... GREATNESS! pjhuuuuuuu,
I've 3 days new to FPC, so I'll try a lot of stuff to get familair
with it, eg, place SysUtils before Dos in Uses and vice verce and also
fool around with SysUtils.FindFirst()
and see what the compiler says...

Thanks a million!!!

-----------------------------------------------------------
:::::: remove x:es from email address
-----------------------------------------------------------
:::::: Sonket Dev: http://w1.321.telia.com/~u32102551/
:::::: Yo!Utilities II: http://w1.321.telia.com/~u32103496/
-----------------------------------------------------------



Sat, 22 Mar 2003 08:02:48 GMT  
 FPC: recursive "FileFind"

Quote:

>Something to think about -- In C, the distinction between functions and
>procedures is somewhat blurred, as everything returns a value.  In Pascal,
>a procedure does NOT return a value (except, perhaps, through a VAR
>parameter); a function does.  Thus embedding a recursive procedure call
>within a WHILE loop may not do what you expect.

No problems(with me and Pascal).
a enum callback in C and so, can confuse when see a thing like a
procedure doing something recursive without return anything.

e.g. in my case here for find files, I can extend the procedure with
eg:

procedure FindDirs(ADir: string; Strs: TStrings);
var
  sr: TSearchRec;
  e: LongInt;
begin
  ADir := AddSlash(ADir);
  e := FindFirst(ADir + '*', faDirectory, sr);
  while e = 0 do
  begin
    if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
sr.Name)) and (sr.Name[1] <> '.')  then
    begin
      Strs.Add(ADir + sr.Name);
      FindDirs(ADir + sr.Name);
    end;
    e := FindNext(sr);
  end;
  FindClose(sr);
end;

or I can make a function to collect what I want, eg.

function FindDirs(ADir: string): TStrings;
var
  sr: TSearchRec;
  e: LongInt;
begin
  ADir := AddSlash(ADir);
  Result := TStringList.Create;
  try
    e := FindFirst(ADir + '*', faDirectory, sr);
    while e = 0 do
    begin
      if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
       sr.Name)) and (sr.Name[1] <> '.')  then
      begin
        Result.Add(ADir + sr.Name);
        FindDirs(ADir + sr.Name);
      end;
      e := FindNext(sr);
    end;
  finally
    FindClose(sr);
    Result.Free;
  end;
end;

But maybe this also looks confusing, no "cdecl;", a C-user must
womit;-)

The 3:th option is to create an event and signal it in the procedure
and catch it.
the 4:th can be the same as 3:th but with the option e.g:

function FindDirs(ADir: string): BOOL;
var
  sr: TSearchRec;
  e: LongInt;
begin
  ADir := AddSlash(ADir);
  Result := false;
  e := FindFirst(ADir + '*', faDirectory, sr);
  while e = 0 do
  begin
    if ((sr.Attr and faDirectory) <> 0) and (not IsLink(ADir +
     sr.Name)) and (sr.Name[1] <> '.')  then
    begin
      result := true;
      if MyEvent <> nil then
        MyEvent(ADir + sr.Name);
      FindDirs(ADir + sr.Name);
    end;
    e := FindNext(sr);
  end;
  FindClose(sr);
end;

and in code:
if not FindDirs('/') then
  {cout}writeln('Didn''t found a signle dir...');

I just love Pascal;-)
anything that can be done with pointers(and how) in C/C++ can be done
in Pascal to.

-----------------------------------------------------------
:::::: remove x:es from email address
-----------------------------------------------------------
:::::: Sonket Dev: http://w1.321.telia.com/~u32102551/
:::::: Yo!Utilities II: http://w1.321.telia.com/~u32103496/
-----------------------------------------------------------



Sat, 22 Mar 2003 08:15:11 GMT  
 FPC: recursive "FileFind"

Quote:

> >Note that there are two findfirst, one BP compatible (in unit DOS where it
> >should), and one Delphi compatible(in sysutils, where it should :-).

> ahhhhhhh... GREATNESS! pjhuuuuuuu,
> I've 3 days new to FPC, so I'll try a lot of stuff to get familair
> with it, eg, place SysUtils before Dos in Uses and vice verce and also
> fool around with SysUtils.FindFirst()
> and see what the compiler says...

Dos is originally a Turbo Pascal unit, sysutils comes from Delphi.
Although some functionality only exists in one of them, most of it is
duplicated and you shouldn't use both, since it will mainly confuse
you.

If you really need a procedure from one of them, you can always
copy/paste it from the RTL source in your own program.

Jonas



Sat, 22 Mar 2003 03:00:00 GMT  
 FPC: recursive "FileFind"
On Tue, 03 Oct 2000 12:39:39 +0200, Jonas Maebe

Quote:

>> ahhhhhhh... GREATNESS! pjhuuuuuuu,

>Dos is originally a Turbo Pascal unit, sysutils comes from Delphi.
>Although some functionality only exists in one of them, most of it is
>duplicated and you shouldn't use both, since it will mainly confuse
>you.

>If you really need a procedure from one of them, you can always
>copy/paste it from the RTL source in your own program.

Yes, I must do that(first download the sources, I only has the binary
stuff for the moment), becouse I tried the SearchRec from the Dos
unit, it works(I didn't had it included before), then I begin back
again with remove the Dos(not used in my tests) and fool around with
the TSearchRec, I can't get it to work so I must download the sources
now to see whats done there(I've a lot of Delphi "tools units" I've
made, and all file-stuff based on find a file for some reason uses the
TSearchRec.

But first, I must yarn a bit and then wipe the dustroads down to town
and buy some food(this is very very interesting...)

-----------------------------------------------------------
:::::: remove x:es from email address
-----------------------------------------------------------
:::::: Sonket Dev: http://w1.321.telia.com/~u32102551/
:::::: Yo!Utilities II: http://w1.321.telia.com/~u32103496/
-----------------------------------------------------------



Sat, 22 Mar 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. FPC: recursive "FileFind", corrected...

2. The keys "-"/"+"/"*"

3. Prob: FPC Installation on "DOS 7"

4. ""256 Color Mouse""

5. What are "Denormals" and "Nans"

6. Utility to convert "asm" to "inline( )"

7. "delete from" followed by "insert into" = AV

8. "Replace Connection", "Unable to connect to: PARADOX".

9. HELP!!: "Multiple net files","Lock file has grown too large"

10. A problem in creating equal sized "packets" from a set of "packages".

11. How to make a "xxx.EXE" from the "xxx.PAS"???

12. "blob" and "DBT" file errors

 

 
Powered by phpBB® Forum Software