Is there any shorter / more handy / more flexible way to get directory tree as a nested Rules/Associations
list?
I tried to incorporate FileNames
+ GatherBy
and GroupBy
, but nothing was as short and handy as old fashioned:
ClearAll @ filesInDir;
filesInDir[dirname_, deep_] := If[
DirectoryQ[dirname],
FileBaseName[ dirname] -> (
If[ deep == 1,
FileNameTake[#, -1],
filesInDir[#, deep - 1]
] & /@ FileNames["*", dirname]),
FileNameTake[dirname, -1]
];
filesInDir[$UserBaseDirectory, 2]
"Mathematica" -> {"ApplicationData" -> {"CCompilerDriver", "CloudObject", "CUDALink", "Parallel"}, "Applications" -> {...}, "Autoload" -> {...}, "FrontEnd" -> {"init.m"}, "Kernel" -> {"init.m"}, "Licensing" -> {}, "Paclets" -> {"Configuration", "Repository", "Temporary"}, "SystemFiles" -> {"CharacterEncodings", "FrontEnd", "Kernel", "SpellingDictionaries"}}
And now, from this nested rules I want to go back to the output of:
FileNameSplit /@ FileNames["*", $UserBaseDirectory, 2]
p.s.
I've chosen nested lists/associations to reduce ByteCount
and make querying more user friendly. But if you can convince me I should keep a full paths list it would be a valid answer too.
Comments
Post a Comment