This solution should work, assuming your directory structure is always artist/album/songs . If some directories are deeper (or less deep), you will not get what you want.
First I get a list of directories (i.e. a list of executors):
artists <- list.dirs(path=m,recursive=FALSE,full.names=FALSE)
Then I create a nested list:
lapply(artists,function(dir) { albums <- list.dirs(path=paste0(m,"/",dir),recursive=FALSE,full.names=FALSE) album.list <- lapply(albums,function(dir2) { list.files(path=paste0(m,"/",dir,"/",dir2)) }) names(album.list) <- albums album.list })
And finally, I call the top level of the list:
names(music.list) <- artists
The album level works identically to the artist level: I get the directories (corresponding to the albums), then I list the files inside (corresponding to the songs) and, finally, I call the list items album names.
EDIT: As the docendo distant points out, the above solution is not general. The following recursive solution should do this work in a more elegant way:
rfl <- function(path) { folders <- list.dirs(path,recursive=FALSE,full.names=FALSE) if (length(folders)==0) list.files(path) else { sublist <- lapply(paste0(path,"/",folders),rfl) setNames(sublist,folders) } } rfl(m)
This is still not completely general: as long as the folder contains subfolders, the algorithm descends into these folders without saving files that may also exist at the same depth in the list.