[R] Possible to access a USB volume by name in windows

Joshua Wiley jwiley.psych at gmail.com
Wed Sep 7 05:41:58 CEST 2011


Hi,

This comes with absolutely no guarantees (and a good recommendation to
be cautious), but you could try it:

myset <- function(name = "", path = "") {
  res <- vector("character", length(LETTERS))
  for(i in LETTERS) {
    res[i] <- shell(shQuote(paste("VOL ", i, ":", sep = '')), intern =
TRUE, ignore.stderr = TRUE)[1L]
  }
  tmp <- gsub("[[:space:]]", "", grep(name, res, ignore.case = TRUE,
value = TRUE))
  if (!nzchar(tmp)) stop("No volume with ", name, "label name could be found")
  vol <- strsplit(tmp, "drive|is")[[1L]][2L]
  fullpath <- paste(vol, ":/", path, sep = '')
  cat("Setting WD to ", fullpath, fill = TRUE)
  setwd(fullpath)
}

#### Example usage ####
## set WD to root of volume with label "FLASH_NAME"
myset("FLASH_NAME")
## set WD so some subdirectory
myset("FLASH_NAME", "path/to/something")

At least on my system, this takes awhile to run.  It iterates through
all the volumes [A-Z], and there will probably be quite a few warnings
unless all volumes are mounted, but the warnings (at least about not
finding drives/bad exit status) should be ignorable.

Cheers,

Josh

On Tue, Sep 6, 2011 at 2:56 PM, Gene Leynes <gleynes+r at gmail.com> wrote:
> On the Mac it's pretty easy to get to a USB drive by name.  For example the
> following command works if you have a USB drive named "MYUSB"
> setwd('/Volumes/MYUSB')
>
> Is there a way to do the same thing in Windows (without knowing the drive
> letter)?
>
>
> Thanks!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list