DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

DefaultFileExt

Adds a default extension to a file name if it has no extension but leaves the file name unchanged if it has an extension.

function DefaultFileExt(const FileName, Ext: string): string;
begin
  if HasFileExt(FileName) then
    Result := FileName
  else
    Result := ForceFileExt(FileName, Ext);
end;

ForceFileExt

Forces a file name to have a specified extension. If the given extension is the empty string then any existing extension is removed.

function ForceFileExt(const FileName, Ext: string): string;
begin
  Result := RemoveFileExt(FileName);
  if Ext = '' then
    Exit;
  if Ext[1] = '.' then
    Result := Result + Ext
  else
    Result := Result + '.' + Ext;
end;

HasFileExt

Determines if a file name has an extension. A file name ending in '.' is deemed to have an empty extension.

function HasFileExt(const FileName: string): Boolean;
begin
  Result := SysUtils.ExtractFileExt(FileName) <> '';
end;

View the whole database.

Go to the DelphiDabbler website.