views:

217

answers:

3

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

+5  A: 

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

Mason Wheeler
+12  A: 

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

RRUZ
I never know ExtractFileName would work on a directory.Perfect.Thanks.
philO
A: 

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

uses SysUtils;

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