admin管理员组

文章数量:1313733

Assuming a POSIX path to a specific folder in the macOS filesystem:

/root/Owners/J.F. Kennedy

I would like to obtain J.F. Kennedy.

I used takeFileName function to solve this problem, but I feel there is a better tool for this job.

Is there any function in System.FilePath or System.Directory modules which can accomplish the task at hand ?

Assuming a POSIX path to a specific folder in the macOS filesystem:

/root/Owners/J.F. Kennedy

I would like to obtain J.F. Kennedy.

I used takeFileName function to solve this problem, but I feel there is a better tool for this job.

Is there any function in System.FilePath or System.Directory modules which can accomplish the task at hand ?

Share Improve this question asked Jan 30 at 14:40 F. ZerF. Zer 1,2799 silver badges12 bronze badges 5
  • 3 What's the problem with takeFileName, seems to be exactly the functionality you want. – cafce25 Commented Jan 30 at 14:42
  • It is a subtlety perhaps, looking at the comment in the module: Get the file name. I wouldn’t be using it for its intended purpose. – F. Zer Commented Jan 30 at 15:03
  • 3 Directories are files. Does that ease your mind? – Daniel Wagner Commented Jan 30 at 15:23
  • You're getting the filename of a directory, that's precisely it's intended purpose, you're not going to get something better than a purpose built function for exactly your usecase. – cafce25 Commented Jan 30 at 15:36
  • Thank you, I understand. I guess I am looking for last . splitPath, perhaps. – F. Zer Commented Jan 30 at 16:12
Add a comment  | 

1 Answer 1

Reset to default 2

As per the comments, it's reasonable to consider any path as consisting of a directory part (every component except the last) and a filename part (the last, possibly empty, component) that can be simultaneously retrieved with splitFileName or individually retrieved with takeDirectory or takeFileName, and it doesn't matter if the path refers to a regular file or a directory.

Be warned that if you want to take the last non-empty component of the path, you should consider using:

takeFileName . dropTrailingPathSeparator

instead. This should properly handle all the weird cases of extra path separators:

"/a/b/c" -> "c"
"/a/b/c/" -> "c"
"//a///b//c////" -> "c"

and will only return an empty component if the entire path is empty (or only consists of path separators).

本文标签: haskellHow can I extract the base name of a directory with at least one dot in its nameStack Overflow