Back
Lennie's Weekly Tip: Expanding a Filename in Delphi

10 August 2000 Ref. 33


Let's say you only have the name of a file and that you want to expand it, by adding a full qualified path (drive + directory/subdirectories), then you can use the ExpandFilename function.

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

function ExpandFileName(const FileName: string): string;

Description
-----------
The ExpandFileName function converts the relative file name into a fully qualified path name by merging in the current drive and directory. A fully qualified path name includes the drive letter and any directory and subdirectories in addition to the file name and extension.

ExpandFileName does not verify that the resulting fully qualified path name refers to an existing file, or even that the resulting path exists.

Example
-------

If (FileExists('Test.txt')) then
  Label1.Caption := ExpandFileName('Test.txt')
else
  Messagedlg('ERROR: Please make sure that the file exists...', mterror, [mbok], 0);

In this example the FileExists function (see Ref.26 "Does that Directory or File Exist?") is used to make sure that the file ("Test.txt") exists and will then expand the filename and display it into a label. If the file doesn't exists, an error message will be displayed.

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


Back