ExtractFile...
There are many file naming functions in Delphi that extract file name from path,
directory from path and so on. These functions are:
ExtractFileName
function ExtractFileName(const FileName: string): string;
Returns file name with it's extension from full-qualified file path,
Example:
var
FileName: string;
begin
FileName:= 'c:\windows\notepad.exe';
FileName:= ExtractFileName(FileName);
This function will return:
notepad.exe
ExtractFileExt
function ExtractFileExt(const FileName: string): string;
Returns file extension with period (.) separator.
Example:
ShowMessage(ExtractFileExt('c:\autoexec.bat'));
The result will be:
.bat
ExtractFilePath
function ExtractFilePath(const FileName: string): string;
Returns file directory with back slash separator.
Example:
ShowMessage(ExtractFilePath('c:\windows\win.ini'));
The result will be:
c:\windows\
ExtractFileDir
function ExtractFileDir(const FileName: string): string;
Returns file directory without back slash, but if qualified file path exists in
root directory such as 'c:\autoexec.bat'; slash will be returned with directory name.
Example 1:
FileName:= 'c:\windows\system\MyDll.dll';
Label1.Caption:= ExtractFileDir(FileName);
The result will be:
c:\windows\system
Example 2:
FileName:= 'c:\config.sys';
Label1.Caption:= ExtractFileDir(FileName);
The result will be:
c:\
ChangeFileExt
function ChangeFileExt(const FileName, Extension: string): string;
Changes file extension or erase extension if used with nil Extension.
Example 1:
Label1.Caption:= ChangeFileExt(Edit1.Text, '.bak');
Result:
if Exit.Text = 'c:\config.sys' the result will be
c:\config.bak
Example 2: (Erasing extension)
ShowMessage(ChangeFileExt('c:\config.sys', ''));
Result:
c:\config
How can I get file name without extension ?
Simply use this trick:
FileName:= 'c:\windows\win.ini';
FileName:= ExtractFileName(ChangeFileExt(FileName, ''));
Result:
win