(* 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 findab;

uses
  sysutils;

procedure process(a, b : string);

begin
  writeln('>>>> ' + a + ' and ' + b);
end;

procedure recurse(dir : string);

var
  ctx : TSearchRec;
  status : integer;
  ext, name : string;
  a, b : string;

begin
  writeln('Processing directory: ' + dir);
  status := FindFirst(dir + '\*.*', faAnyFile, ctx);
  while status = 0 do begin
    ext := ExtractFileExt(ctx.Name);
    name := Copy(ctx.Name, 1, Length(ctx.Name) - Length(ext));
    if (ext = '.jpg') or (ext = '.JPG') or (ext = '.jpeg') or (ext = '.JPEG') then begin
       if name.EndsWith('a') then begin
          a := dir + '\' + ctx.Name;
       end else if name.EndsWith('b') then begin
          b := dir + '\' + ctx.Name;
          process(a, b);
       end else begin
         writeln('Ignoring non-a/b file: ' + ctx.Name);
       end;
    end else if (ctx.Attr and faDirectory) <> 0 then begin
      if ctx.Name[1] <> '.' then begin
         recurse(dir + '\' + ctx.Name);
      end else begin
        writeln('Ignoring pseudo directory: ' + ctx.Name);
      end;
    end else begin
      writeln('Ignoring irrelevant file extension: ' + ctx.Name);
    end;
    status := FindNext(ctx);
  end;
end;

begin
  recurse('\Work\z');
  readln;
end.

