Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path to the parent folder of a certain directory?

Tags:

delphi

When I would have this directory path:

C:\Program Files (x86)\Embarcadero\

The function I'm looking for should return:

C:\Program Files (x86)\

I've tried this code but it works only for files, not for directories:

function GetParentDirectory(const Path: string): string;
begin
  Result := ExpandFileName(Path);
end;

Does anyone know what should I use to return path to the parent folder of a certain directory ?

like image 298
jwz104 Avatar asked Mar 25 '14 16:03

jwz104


1 Answers

You can use a couple of ways:

From a folder name:

ExtractFilePath(ExcludeTrailingPathDelimiter('C:\Parent\Child\'));

From a file name:

ExtractFilePath(ExcludeTrailingPathDelimiter(ExtractFilePath('C:\Parent\Child\app.exe')));
like image 69
Ken White Avatar answered Oct 06 '22 11:10

Ken White