Back


Lennie's Weekly Tip: Extracting Filename and Extension 

22 July 2000 Ref. 29


Sometimes you may want to extract the filename or extension part of a file from a full path string.
You can use the ExtractFilename and ExtractFileExt functions to accomplish it easily.

EXTRACTFILENAME
===============

Declaration
-----------

function ExtractFileName(const FileName: string): string;

Description
-----------
The resulting string is the leftmost characters of FileName, starting with the first character after the colon or backslash that separates the path information from the name and extension. The resulting string is equal to FileName if FileName contains no drive and directory parts.
 

EXTRACTFILEEXT
==============

Declaration
-----------

function ExtractFileExt(const FileName: string): string;

Description
-----------
The resulting string includes the period character that separates the name and extension parts.
The resulting string is empty if the given filename has no extension.
 

Example
=======
Label1.Caption := 'Full Path: ' + Application.ExeName;
Label2.Caption := 'Filename: ' + ExtractFilename(Application.ExeName);
Label3.Caption := 'Extention: ' + ExtractFileExt(Application.ExeName);

In this example the full path to your application's executable is displayed first, then the filename and then the extension part.

NOTE: Remember to add the SysUtils unit to your application's uses-clause.

For more information read Delphi's on-line help.


Back