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

前端 未结 2 928
有刺的猬
有刺的猬 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:20

    I see it's an old question, but now we can use the fs package that according to the tidyverse blog and the package vignette "provides a cross-platform, uniform interface to file system operations" and "smooth over some of the idiosyncrasies of file handling with base R functions".

    Here's how we can accomplish this task with fs:

    fs::dir_ls(path = c("C:/", "E:/"), type = "directory", glob = "*MyFiles", recurse = TRUE)
    

    There are a couple of added advantages to using this approach:

    1. If we have a general sense of where this directory should be in the folder hierarchy, we can recurse upto a certain level instead of searching the entire tree. We just need to add the argument recurse = #num_levels_to_recurse) to the above code.
    2. Also, if we want to list all directories except MyFiles, we can add the argument invert = TRUE to the above code.

    These two options are not available in list.files() and list.dirs() functions of base R. Check out this document for a thorough comparison between fs functions and base R functions for file system operations.

提交回复
热议问题