9

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 directory named "MyFiles", and it could be anywhere on my C:, or on my USB (E:). I'd like to know all the tree locations of the directory.

Thanks for any advice!

csrvermaak
  • 278
  • 1
  • 2
  • 13
  • This sort of question has been asked (and answered) before (e.g. http://stackoverflow.com/questions/4876813/using-r-to-list-all-files-with-a-specified-extension?rq=1) If your question is different to these please consider expanding it. – Phil Jul 10 '16 at 11:32
  • 1
    The poster wants to search the entire tree whereas the link provided only searches the current directory. – G. Grothendieck Jul 10 '16 at 11:41
  • 1
    @phil - thanks for your link - but I'd like to search all possible locations in the tree for a specific file, as g-grothendieck specified. I'll updated the question to make that clearer. – csrvermaak Jul 10 '16 at 11:44
  • Do you want R to figure out what all the drives are for you? If not then just use the `recursive` parameter for `list.files()` with the drive as the path. – hrbrmstr Jul 10 '16 at 12:11
  • thanks @hrbrmstr - I actually do want R to figure out the drives, but I'm converging on a solution using `drives <- paste0(letters,":/")` and `list.dirs(drives)` – csrvermaak Jul 10 '16 at 12:20

2 Answers2

7

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"
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
6

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.

Ashirwad
  • 1,890
  • 1
  • 12
  • 14