AvoidNameCollision

衝突したファイル名を自動で置き換える関数。

function AvoidNameCollision(const FileName: string): string;
var
  I: Integer;
  Path, Name, Ext: string;
begin
  if not FileExists(FileName) then
  begin
    Result := FileName;
    Exit;
  end;

  I := 1;
  Path := ExtractFilePath(FileName);
  Name := ChangeFileExt(ExtractFileName(FileName), '');
  Ext := ExtractFileExt(FileName);
  while FileExists(Format('%s%s[%d]%s', [Path, Name, I, Ext])) do
    Inc(I);
  Result := Format('%s%s[%d]%s', [Path, Name, I, Ext]);
end;

Return index page