Lazarus

Programming => General => Topic started by: mirce.vladimirov on May 14, 2015, 06:27:33 pm

Title: get last part of path structure
Post by: mirce.vladimirov 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.
Title: Re: get last part of path structure
Post by: taazz on May 14, 2015, 06:35:44 pm
Code: [Select]
Function GetDeepestDir(const aFilename:string):string;
begin
  Result := extractFileName(ExtractFilePath(afilename));
end;
Title: Re: get last part of path structure
Post by: mangakissa 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;
Title: Re: get last part of path structure
Post by: totya 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;
Title: Re: get last part of path structure
Post by: User137 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)
Title: Re: get last part of path structure
Post by: arneolav 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.
Title: Re: get last part of path structure
Post by: JimD 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.  
Title: Re: get last part of path structure
Post by: Zvoni on July 13, 2018, 12:32:54 pm
Errr?

Why not just use RPos with DirectorySeparator?
Then it's just a simple MidStr
Title: Re: get last part of path structure
Post by: wp 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;
Title: Re: get last part of path structure
Post by: rdny on October 02, 2019, 04:18:23 am
may I know how can i use this topic on dropping files? thanks allot :D
TinyPortal © 2005-2018