Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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!

like image 242
csrvermaak Avatar asked Jul 10 '16 11:07

csrvermaak


People also ask

How do I search for a directory in R?

If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script. To change the current working directory we need to use a function called setwd( ).

How do I search for a file in R?

R‑Studio searches for all files that matches the specified search criteria.. The search results appear in the Find Results panel. Mark/Unmark All . R‑Studio marks/unmarks all files that match the search criteria.

Why are some files not visible on my external hard drive?

One likely cause of files not appearing from an External HDD is that your drivers aren't up to date. This can happen if you plug the drive into a computer that it has never been plugged into before, or if you have run updates on your machine since the last time the drive was plugged in.


2 Answers

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"
like image 100
hrbrmstr Avatar answered Nov 16 '22 01:11

hrbrmstr


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.

like image 42
Ashirwad Avatar answered Nov 15 '22 23:11

Ashirwad