简体   繁体   中英

How to create csv file of multiple generations of directories ie layers of subdirectories

I have a large directory with many layers/generations of directories within, lets say 5 layers. I am trying to count the occurrence/presence of the final generation of subdirectories (not the files within the final subdirectory).

eg parent directory (gen1) and all subdirectories within gen1 (gen2) and all subdirectories within gen2 (gen3) and all subdirectories within gen3 (gen4) and all subdirectories within gen4 (gen5)

To do this I would like to create a.csv with the names of all 5 generations of directories, so each row in the csv will show a final subdirectory name (gen5) and all the above parent directories (gen4, 3, 2, 1), separated by columns. I can then count directory occurrences at different levels by filtering by directory name.

There is a different amount of subdirectories at each level. eg gen2 has 4 folders -> Folder1 = 8 subdirectories, Folder2 = 12 subdirectories, Folder3 = 6 subdirectories, Folder4 15 subdirectories and so on...

I'm using R on windows and have tried applying the list.dirs() and the list.files() but can't get past manually listing each subdirectory individually, and can't figure out how to exclude the files - this makes my R crash as the files are too large. Surely I can spit out a csv file showing multiple generations of just directories by name? Struggling to find a code that excludes the files and only spits out the directories.

here's what I'm hoping the output will look like:

示例输出表

The following function goes down the directory tree recursively and adds 1 each time it finds a leaf. Optionally, it will save the leaves names in a file.

count_dirs <- function(path = ".", savefile = FALSE, filename) {
  f <- function(path) {
    if(dir.exists(paths = path)) {
      dir_vec <- list.dirs(path = path, full.names = TRUE, recursive = FALSE)
      if(length(dir_vec) == 1L) {
        e$n <- e$n + 1L
        if(savefile) {
          df1 <- data.frame(directory = dir_vec)
          write.table(
            df1, e$outfile, 
            quote = FALSE, 
            row.names = FALSE, col.names = FALSE,
            append = TRUE
          )
        }
      } else {
        for(d in dir_vec)  Recall(path = d)
      }
    }
  }
  e <- new.env()
  e$n <- 0L
  if(savefile) {
    e$outfile <- filename
    df1 <- data.frame(directory = character(0))
    write.table(df1, e$outfile, quote = FALSE, row.names = FALSE)
  }
  path <- normalizePath(path = path)
  if(.Platform$OS.type == "windows") {
    path <- chartr("\\", "/", path)
  }
  f(path = path)
  e$n
}

# sample usage    
tmpfile <- tempfile(fileext = ".dat")
count_dirs(savefile = TRUE, filename = tmpfile)
# [1] 79

# tidy up
#unlink(tmpfile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM