Using R - How do I search for a file/folder on all drives (hard drives as well as USB drives)

前端 未结 2 927
有刺的猬
有刺的猬 2021-01-12 03:48

Please help - using R, how would I search for a specific file/folder on all drives (hard drives as well as attached USB drives)?

For example, I\'m looking for a dir

2条回答
  •  有刺的猬
    2021-01-12 04:10

    Messed up a bit in the comment as I misread the thread (you need dirs). You can still do this with list.files() tho. I mocked up a directory structure looking for directories named "data" but also included a file named "data":

    (pre <- list.files("/var/tmp/a", "data", recursive=TRUE, full.names=TRUE, include.dirs=TRUE))
    
    ## [1] "/var/tmp/a/data"   "/var/tmp/a/l/data" "/var/tmp/a/q/data"
    

    (/var/tmp/a/l/data is actually just a file)

    But, you only need/want directories, so if you have a fairly modern R install and the purrr package installed then you can do:

    purrr::keep(pre, dir.exists)
    
    ## [1] "/var/tmp/a/data"   "/var/tmp/a/q/data"
    

提交回复
热议问题