(* Collection of code snippets by Arne Vajhøj *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
program dirlist;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  ptronefile = ^onefile;
  onefile = record
              next : ptronefile;
              prev : ptronefile;
              name : string;
            end;

procedure addfile(name : string; var first,last : ptronefile);

var
  tmp : ptronefile;

begin
  if first = nil then begin
    new(first);
    tmp := nil;
    last := first;
  end else begin
    new(last^.next);
    tmp := last;
    last := last^.next;
  end;
  last^.next := nil;
  last^.prev := tmp;
  last^.name := name;
end;

procedure printfiles(first : ptronefile);

var
  curr : ptronefile;

begin
   curr := first;
   while curr <> nil do begin
      writeln(curr^.name);
      curr := curr^.next;
   end;
end;

procedure dirlistrec(dirspec : string; var first,last : ptronefile);

var
  sr : TSearchRec;

begin
  if FindFirst(dirspec + '*.*', faAnyFile, sr) = 0 then begin
    if (sr.Attr and faDirectory) <> 0 then begin
      if (sr.Name <> '.') and (sr.Name <> '..') then begin
        dirlistrec(dirspec + sr.Name + '\', first, last);
      end;
    end else begin
      addfile(dirspec + sr.Name, first, last);
    end;
    while FindNext(sr) = 0 do begin
      if (sr.Attr and faDirectory) <> 0 then begin
        if (sr.Name <> '.') and (sr.Name <> '..') then begin
          dirlistrec(dirspec + sr.Name + '\', first, last);
        end;
      end else begin
        addfile(dirspec + sr.Name, first, last);
      end;
    end;
    FindClose(sr);
  end;
end;

var
  first,last : ptronefile;

begin
  first := nil;
  last := nil;
  dirlistrec('C:\zzz\', first, last);
  printfiles(first);
end.
