Back
Lennie's Weekly Tip: Extracting Only the File-Path

03 August 2000 Ref. 31


Sometimes you may only want to know what's the path of a specified file (excluding the filename). You can use the ExtractFilePath function to accomplish this easily.

Declaration
------------
function ExtractFilePath(const FileName: string): string;

Description
-----------
The resulting string is the rightmost characters of FileName, up to and including the colon or backslash that separates the path information from the name and extension. The resulting string is empty if FileName contains no drive and directory parts.

Example
-------

if (OpenDialog.Execute) then
  begin
     Label1.Caption := OpenDialog.Filename;
     Label2.Caption := ExtractFilePath(OpenDialog.Filename);
  end;

In this example the full path to a file (including the filename) is displayed first and then only the path (without the filename).

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


Back