Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

list.files() on a specific sub-directory in a folder tree

I have a huge folder structure like :

main
  |____ A1
  |     |__proj_doc
  | 
  |____ B2
  |     |__proj_doc
  |
  |____ C3
        |__proj doc
  

and would like to list all the txt files from all proj_doc and proj doc folders.

mylist <-list.files(path = "/main", recursive = TRUE,
                            pattern = "\\.txt$", 
                            full.names = TRUE)

will list all the files but from the root main folder, including all the sub folders, which cause a lot of unnecessary data. How could I correct the path to just look into correct sub-directories ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Here’s one way:

dirs = list.dirs('main')
proj_doc_dirs = grep('/proj./proj[ _]doc', dirs, value = TRUE)
txt_files = dir(proj_doc_dirs, pattern = '\\.txt$', full.names = TRUE)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading