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

{$APPTYPE CONSOLE}

uses
  SysUtils;

function IsValidFileName(fnm : string) : boolean;

function IsValidShortFileName(fnm : string) : boolean;

const
  Invalid = '<>:"/\|?*';

begin
  IsValidShortFileName := LastDelimiter(Invalid, fnm) = 0;
end;

begin
  IsValidFileName := DirectoryExists(ExtractFilePath(fnm))
                     and IsValidShortFileName(ExtractFileName(fnm));
end;

begin
  writeln(IsValidFileName('C:\Code\foo.bar'));
  writeln(IsValidFileName('C:\foo.bar'));
  writeln(IsValidFileName('C:\Code2\foo.bar'));
  writeln(IsValidFileName('D:\foo.bar'));
  writeln(IsValidFileName('C:\foo:bar'));
  writeln(IsValidFileName('C:\foo<>bar'));
end.
