[comp.lang.pascal] FindFile & FindNext problems with TP 5.x

reino@cs.eur.nl (Reino de Boer) (11/14/89)

bundalo@iitmax.IIT.EDU (Predrag S. Bundalo) writes:

>	I'm writing a program that needs to make two calls to
>	FINDFILE.  The first time, I need to get the names of
>	all of the subdirectories in the current directory and
>	store that somewhere (stack).  The next time, I need
>	to find all of the files (in the current directory)
>	that match a given pattern (paramstr(1)).

program filefind;
uses  crt, dos;
type  link = ^cell;
      cell = record rec  : searchrec;
        			next : link
      		 end;
var   dirs : link;
      arg  : string;
procedure push( var srec : searchrec );
  var p : link;
  begin new( p );
    with p^ do begin
      rec := srec;
      next := dirs
    end;
    dirs := p
  end;
procedure pop( var srec : searchrec );
  var p : link;
  begin p := dirs;
    dirs := dirs^.next;
    srec := p^.rec;
    dispose( p )
  end;
procedure getdirs;
  var srec : searchrec;
  begin findfirst( '*.*', anyfile, srec );
    while( doserror = 0 ) do begin
      if( ( srec.attr and directory ) = directory ) then
        if( srec.name[1] <> '.' ) then
          push( srec );
      findnext( srec )
    end;
  end;
procedure getfiles( pattern : string );
  var srec : searchrec;
  begin findfirst( pattern, anyfile, srec );
    while( doserror = 0 ) do begin
      if( ( srec.attr and directory ) <> directory ) then
        writeln( srec.name );
      findnext( srec )
    end
  end;
procedure showdirs;
  var srec : searchrec;
  begin while( dirs <> nil ) do begin pop( srec ); writeln( srec.name )
    	end
  end;
procedure pause;
  begin write( 'press a key...' );
    while( readkey = #0 ) do; writeln
  end;
begin
  if( paramcount < 1 ) then begin
    writeln( 'usage: filefind <filename>' );
    halt( 1 )
  end;
  arg := paramstr( 1 );
  dirs := nil;
  getdirs;
  getfiles( arg ); pause;
  showdirs; pause
end.

Hope this helps -- reino

-- 
Reino R. A. de Boer
Erasmus University Rotterdam ( Informatica )
e-mail: reino@cs.eur.nl

john@wsl.UUCP (John Allen on wsl) (11/16/89)

In article <1989Nov14.073404.10271@cs.eur.nl>, reino@cs.eur.nl (Reino de Boer) writes:
> bundalo@iitmax.IIT.EDU (Predrag S. Bundalo) writes:
> 
> >	I'm writing a program that needs to make two calls to
> >	FINDFILE.  The first time, I need to get the names of
> >	all of the subdirectories in the current directory and
> >	store that somewhere (stack).  The next time, I need
> >	to find all of the files (in the current directory)
> >	that match a given pattern (paramstr(1)).
> 

I presume that you want to scan an entire directory tree, searching for
files that match a particular file spec. If this assumption is correct
then you taking a slightly wrong approach.


If this is what you want, then I will send you complete source for a 
program which does this. The code is in 'C', but it is fundamentally the 
same as in PASCAL. (I use TurboC & TurboPascal all the time).