Virtual Pascal

Classic Object Pascal for free

Does anyone have something that just goes from the current directory down and lists all files. I'm sure I could write it myself, but that means diving into the manuals, and right now I'm a tiny bit lazy...

Views: 71

Reply to This

Replies to This Discussion

A basic skeleton nicked from one of my FPC programs looks like this (uses sysutils,classes). VP has sysutils, so I guess that will ok.

The principles for doing old-skool over unit dos are roughly the same afaik, but I haven't used that unit in over ten years.

procedure findfile(startpath:string);
var Rec : TSearchRec;
    Path,s: string;
    dirs:Tstringlist;
    i: Integer;
begin
 Path := IncludeTrailingPathDelimiter(startpath) ;  // d6+ but only appends a \ if none is there.

 dirs:=Tstringlist.Create;
 if FindFirst (path+'*', faAnyFile, Rec) = 0 then
 try
   repeat
     if (rec.Attr and faDirectory)>0 then
      begin
       if (rec.name<>'.') and (rec.name<>'..')   then
        dirs.add(path+rec.Name) 
      end
     else
       begin
         writeln(path+rec.name);
       end;
    until FindNext(Rec) <> 0;
 finally
 FindClose(Rec) ; // not required for TP 8.3 dos, but required for dos with LFN and all other OSes.
 end;

  // I prefer to first read all files, close handle, and only then recurse. Saves on handles and is more predicatble.
 for i:=0 to dirs.Count-1 do
     findfile(dirs[i]);
 dirs.Free;
end;

RSS

© 2017   Created by Allan Mertner.   Powered by

Report an Issue  |  Terms of Service