简体   繁体   中英

List of subfolders names, folder path and date modified field

Need to write a piece of code in R that'll create a list specifying:

  • Names of subfolders with a pre-set depth (eg 2 levels down)
  • Path
  • Date modified

I've tried to use the following generic function but had no luck:

list.files(path, pattern=NULL, all.files=FALSE,
    full.names=FALSE)
dir(path, pattern=NULL, all.files=FALSE,
    full.names=FALSE)

Would very much appreciate your response.

I think what your are missing is the recursive = TRUE parameter in list.files()

One possible solution could be to list all files first and then limit the output to 2 levels accordingly.

files <- list.files(path = "D:/cmder/", recursive = TRUE)

Since R represents paths using "/" a simple example could be to remove everything that has more than 3 slashes if you need a depth of 2.

files[!grepl(".*/.*/.*/.*", files)]

Be careful on windows as you might see a back slash "\" there sometimes, only if your path information comes from something different that R itself, eg a csv import.

My grepl() statement can probably be improved as I'm not an expert there.

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