Recent

Author Topic: get last part of path structure  (Read 4458 times)

mirce.vladimirov

  • Full Member
  • ***
  • Posts: 219
get last part of path structure
« on: May 14, 2015, 06:27:33 pm »
suppose the path of my executable is equal to "c:\myapp\thefirst", how can i get the "thefirst" string alone ? I already used   ExtractFileName, ExtractFilePath , ExtractFileExt , ExtractFileDir , ExtractFileDrive, getcurrentdir  but those extract either the complete path or only the executable's filename.
I only need the deepest directory part of the path.

taazz

  • Hero Member
  • *****
  • Posts: 5363
Re: get last part of path structure
« Reply #1 on: May 14, 2015, 06:35:44 pm »
Code: [Select]
Function GetDeepestDir(const aFilename:string):string;
begin
  Result := extractFileName(ExtractFilePath(afilename));
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

mangakissa

  • Hero Member
  • *****
  • Posts: 879
Re: get last part of path structure
« Reply #2 on: May 14, 2015, 06:41:46 pm »
very raw code:
Code: [Select]
function GetDeepestDir(const aDir : string) : string;
var Directory : string;
      Index     : integer;
begin
  Directory := ExtractFileDir(aDir);
  for index := length(Directory) downto 1 do
  begin
     if Directory[index] = '\' then
       break;
  end;
  if index > 1 then
    result := copy(Directory,index,(length(Directory) - index))
  else
    result := '';
end;
This is nice and this is dirty:
Code: [Select]
var  slDir       : TStringlist;
begin
  slDir := TStringlist.create;
  try
    Directory := ExtractFileDir(aDir);
    sldir.Delimiter := '\';
    slDir.strctdelimited := true;
    slDir.delimitedtext := ExtractFileDir(aDir);;
    if sldir.count > 0 then
      result := slDir(sldir.count - 1)
    else
      result := '';
  finally
     slDir.free;
  end;
end;
« Last Edit: May 14, 2015, 06:43:25 pm by mangakissa »
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

totya

  • Sr. Member
  • ****
  • Posts: 453
Re: get last part of path structure
« Reply #3 on: January 03, 2016, 11:01:38 am »
Code: [Select]
Function GetDeepestDir(const aFilename:string):string;
begin
  Result := extractFileName(ExtractFilePath(afilename));
end;

Hi!

This doesn't work. Corrected code:

Code: [Select]
Function GetDeepestDir(const aFilename:string):string;
begin
  Result := extractFileName(ExtractFileDir(afilename));
end;

User137

  • Hero Member
  • *****
  • Posts: 1776
    • Nxpascal home
Re: get last part of path structure
« Reply #4 on: January 03, 2016, 12:15:09 pm »
That seems elegant enough for solution. Indeed the ExtractFilePath() returns trailing \ .

So from "C:\TestPath\Project\file.txt" the ExtractFileDir() returns ""C:\TestPath\Project", which can be considered a filename without extension. ExtractFileName() from that can then return just "Project". You can parse the whole folder down with that for parent folders aswell. But if you get "C:" for filename you need to deal with it, unless you're sure that you don't go too deep. (*NIX systems obviously differ for root)

arneolav

  • Full Member
  • ***
  • Posts: 112
    • ElTranslador
Re: get last part of path structure
« Reply #5 on: February 02, 2016, 11:31:13 pm »
suppose the path of my executable is equal to "c:\myapp\thefirst", how can i get the "thefirst" string alone ?
 
Code: Pascal  [Select]
  1.  function GetDeepestDir(const aDir : string) : string;
  2.   var   Index     : integer;
  3.   begin
  4.     for index := length(aDir) downto 1 do
  5.     begin
  6.        if aDir[index] = PathDelim then
  7.        begin
  8.          if index > 1
  9.            then Result := copy(aDir,index+1,(length(aDir) - index))             // index+1 -> avoid  Dir Delimiter '\' or '/'
  10.            else Result := '';
  11.          Break;
  12.        end;
  13.     end;
  14.   end;

Avoid use value of "index" after loop, may not be correct.
« Last Edit: February 05, 2016, 04:10:20 pm by arneolav »
Win XP, Win7, Win 10 win64 , Lazarus 1.8.2
Delphi/DevExpress

jasc2v8

  • New member
  • *
  • Posts: 40
Re: get last part of path structure
« Reply #6 on: July 12, 2018, 08:34:41 pm »
I know it's been a while for this reply, but here is another option.

This works on Windows and Ubuntu with the default {$mode objfpc}{$H+}

Code: Pascal  [Select]
  1. function ExtractFilePathLast(aPath: string): string;
  2. { Returns the last dir in the path or empty string if directory separator not found }
  3. begin
  4.   Result:=Copy(aPath,aPath.LastIndexOf(DirectorySeparator)+2);
  5. end;
  6.  
« Last Edit: July 13, 2018, 03:15:47 am by jasc2v8 »

Zvoni

  • Jr. Member
  • **
  • Posts: 87
Re: get last part of path structure
« Reply #7 on: July 13, 2018, 12:32:54 pm »
Errr?

Why not just use RPos with DirectorySeparator?
Then it's just a simple MidStr
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie

wp

  • Hero Member
  • *****
  • Posts: 5350
Re: get last part of path structure
« Reply #8 on: July 13, 2018, 01:39:10 pm »
Another short one, in modern-style Pascal:

Code: Pascal  [Select]
  1. function GetDeepestDirOfPath(AFileName: String): String;
  2. var
  3.   sa: TStringArray;
  4. begin
  5.   sa := AFileName.Split(PathDelim);
  6.   Result := sa[High(sa)-1];  // -1 because the last item is the file name itself
  7. end;
  8.  
  9. // or for directory (without file):
  10.  
  11. function GetDeepestDir(ADir: String): String;
  12. var
  13.   sa: TStringArray;
  14. begin
  15.   sa := AFileName.Split(PathDelim);
  16.   Result := sa[High(sa)];
  17. end;
  18.  

And if there is a chance that the input parameter can contain a relative path then ExpandFileName() should be included. Here for the first case:
Code: Pascal  [Select]
  1. function GetDeepestDirOfPath(AFileName: String): String;
  2. var
  3.   sa: TStringArray;
  4.   fn: String;
  5. begin
  6.   fn := ExpandFileName(AFileName);
  7.   sa := fn.Split(PathDelim);
  8.   Result := sa[High(sa)-1];
  9. end;
« Last Edit: July 13, 2018, 01:47:32 pm by wp »
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10