philO
philO

Reputation: 141

Delphi 7 get folder name from path

I have a path say C:\Program Files\Borland what would bet the easiest way to parse that string and just return Borland? thanks

Upvotes: 14

Views: 16528

Answers (4)

Abdullah
Abdullah

Reputation: 21

This will work on all folders whether it has a trailing back slash or not:

ExtractFileName(ExcludeTrailingBackslash(Path))

Upvotes: 2

lkessler
lkessler

Reputation: 20132

To directly parse that string and just return "Borland", you can do this:

uses SysUtils;

Delete(Path, 1, LastDelimiter('\', Path));

Upvotes: 1

RRUZ
RRUZ

Reputation: 136391

try using the ExtractFileName function, this function only works (for your example) if your path not finalize with an backslash, so you can use the ExcludeTrailingPathDelimiter function to remove the final backslash.

see this sample

program ProjectExtractPathDemo;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
    Path : string ;
begin
  Path:='C:\Program Files\Borland';
  Writeln(ExtractFileName(Path));//return Borland

  Path:='C:\Program Files\Borland\';
  Writeln(ExtractFileName(Path));//return ''

  Path:='C:\Program Files\Borland\';
  Writeln(ExtractFileName(ExcludeTrailingPathDelimiter(Path)));//return Borland


  Readln;
end.

check this link for more info

Path Manipulation Routines

Upvotes: 31

Mason Wheeler
Mason Wheeler

Reputation: 84540

You can get whatever comes after the last backslash with ExtractFileName, which is found in the SysUtils unit.

Upvotes: 8

Related Questions