Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing a directory tree as a recursive list

I am stuck with a certain task. What I want is a function that, given a directory path, would return a recursive-list as an output.

The output should be of the form myList$dir$subdir$subdir$fullFilePath

So basically I want to represent a directory tree as a certain list. I obtain all the files, get all the subdirectories for each of the file, but I am stuck as to how to throw it all into a list with multiple levels.

like image 445
Karolis Koncevičius Avatar asked Jan 07 '13 00:01

Karolis Koncevičius


1 Answers

Here is a solution using recursion:

tree.list <- function(file.or.dir) {
    isdir <- file.info(file.or.dir)$isdir
    if (!isdir) {
        out <- file.or.dir
    } else {
        files <- list.files(file.or.dir, full.names   = TRUE,
                                         include.dirs = TRUE)
        out <- lapply(files, tree.list)
        names(out) <- basename(files)
    }
    out
}

I have tested it here on a small directory

test.dir <- tree.list("./test")
test.dir
# $a
# $a$`1.txt`
# [1] "./test/a/1.txt"
# 
# $a$aa
# $a$aa$`2.txt`
# [1] "./test/a/aa/2.txt"
# 
# $b
# $b$`3.txt`
# [1] "./test/b/3.txt"

If this is too slow for your needs, I would consider reading all the files into a single call to list.files with recursive = TRUE then do a bit of parsing.

like image 94
flodel Avatar answered Sep 18 '22 12:09

flodel