I have a list of long directory names. For each directory name, I need a label just long enough to distinguish itself from the rest. Each label should be a FileNameJoin
of one or more components of the corresponding directory name.
You can assume that all the directories names have a common beginning and are unique. They tend to differ by the last one or two components.
Example list: {"common/a/b/c", "common/b/c", "common/x/y/z"}
Result: {"a/b/c", "common/b/c", "z"}
or equivalent.
Failing to find any system function that does this directly, I'm thinking along the lines of a recursive GroupBy
, e.g.
f[dirs_List] := If[Length[dirs] == 1,
FileNameTake @@ dirs,
Normal @ GroupBy[dirs, FileNameTake -> FileNameDrop, f]]
f[{"common/a/b/c", "common/b/c", "common/x/y/z"}]
(*Out: {"c" -> {"b" -> {"a" -> "common", "common" -> ""}}, "z" -> "y"} *)
I'm stuck at how to collect from such a structure. Also I'd like to hear your ideas before going further :)
Hash
on the directory name. But this label is part of the user interface so I want it to be meaningful. $\endgroup$dirs = {"\\\\server\\share\\path\\last", "\\\\server\\share\\path\\last1", "\\\\server\\share\\path\\notlast\\last"}; NestWhile[Rest /@ # &, FileNameSplit /@ dirs, Length@Union@#[[All, 1]] == 1 &]
$\endgroup$