Last folder of a directory

Asked

Viewed 1,558 times

9

How do I get the last folder from a directory. Ex:

C:\Program Files\Skype

The last folder would be Skype.

  • James, create an answer please, it worked.

3 answers

9

You can use the function ExtractFileName() to extract the name of a file or folder, the result will be the rightmost characters of the string passed as parameter, starting with the first character after the Colon or backslash.

If the function result above contains a delimiter at the end you can use the function ExcludeTrailingPathDelimiter() to eliminate him.

uses
  SysUtils;

function GetPath(const pPath: string): string;
begin
if ExtractFileExt(pPath) <> '' then
  Result := ExtractFileName(ExtractFileDir(pPath))
else
  Result := ExtractFileName(ExcludeTrailingPathDelimiter(pPath));
end;

To use:

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetPath('C:\Program Files\Skype'));            // Skype
ShowMessage(GetPath('C:\Program Files\Skype\'));           // Skype
ShowMessage(GetPath('C:\Program Files\Skype\Skype.exe'));  // Skype
end;
  • 1

    And if I have the path 'C: Program Files Skype'?

  • @Eprogrammernotfound was the idea even, besides checking if it is a directory or tb file would be cool

  • 1

    @Caputo Today I went to see that my answer failed in this situation. I already got the code. + 1

7


To avoid possible wrong results, I would do as follows

program ConsoleTestApp;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

function ObterNomeUltimaPasta(path: string): string;
var
  pathAjustado: string;
begin
  pathAjustado := path;
  if FileExists(pathAjustado) then
    pathAjustado := ExtractFileDir(pathAjustado);

  Result := ExtractFileName(ExcludeTrailingPathDelimiter(pathAjustado));
end;

begin
  try
    Writeln(ObterNomeUltimaPasta('C:\Program Files\GIMP 2\bin\bz2-1.dll'));
    Writeln(ObterNomeUltimaPasta('C:\Program Files\GIMP 2\bin\'));
    Writeln(ObterNomeUltimaPasta('C:\Program Files\GIMP 2\bin'));
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

The result of the console was

bin
bin
bin

6

You need to break the string "C: Program Files Skype" into " ", like this:

program Project28;

{$APPTYPE CONSOLE}

uses
  Classes,
  SysUtils;

procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings) ;
begin
   ListOfStrings.Clear;
   ListOfStrings.Delimiter     := Delimiter;
   ListOfStrings.DelimitedText := Str;
end;


var
   OutPutList: TStringList;
begin
   OutPutList := TStringList.Create;
   try
     Split('\', 'C:\Program Files\Skype', OutPutList) ;
     Writeln(OutPutList.Text);
     Readln;
   finally
     OutPutList.Free;
   end;
end.

The result will be:

List[0] = 'C:'
List[1] = 'Program Files'
List[2] = 'Skype'

You only have to grab List[2] to get the last folder.

Another alternative is to use the function Extractfilename:

uses
  SysUtils;

var
    Filepath : string ;
begin
  Filepath:='C:\Program Files\Skype';
  ShowMessage(ExtractFileName(Path));

where the result is

Skype
  • 1

    a good way to grab the last folder would be List[List.Count-1]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.