using R to copy files
Asked Answered
F

3

37

As part of a larger task performed in R run under windows, I would like to copy selected files between directories. Is it possible to give within R a command like cp patha/filea*.csv pathb (notice the wildcard, for extra spice)?

Facilitation answered 5/3, 2010 at 4:10 Comment(0)
A
56

I don't think there is a direct way (shy of shelling-out), but something like the following usually works for me.

flist <- list.files("patha", "^filea.+[.]csv$", full.names = TRUE)
file.copy(flist, "pathb")

Notes:

  • I purposely decomposed in two steps, they can be combined.
  • See the regular expression: R uses true regex, and also separates the file pattern from the path, in two separate arguments.
  • note the ^ and $ (beg/end of string) in the regex -- this is a common gotcha, as these are implicit to wildcard-type patterns, but required with regexes (lest some file names which match the wildcard pattern but also start and/or end with additional text be selected as well).
  • In the Windows world, people will typically add the ignore.case = TRUE argument to list.files, in order to emulate the fact that directory searches are case insensitive with this OS.
  • R's glob2rx() function provides a convenient way to convert wildcard patterns to regular expressions. For example fpattern = glob2rx('filea*.csv') returns a different but equivalent regex.
Axletree answered 5/3, 2010 at 4:46 Comment(5)
I think the pattern should be "^filea.+[.]csv".Bari
@Marek: right you are! Also, in particular in the Windows world, peopole will typically want to add the argument ignore.case = TRUE. I edited accordingly, thanks.Axletree
you can use ?glob2rx to convert from wildcards to regexes.Mohammadmohammed
@Edurardo Leoni: yes, you can. The nice, if only occasionally unsettling, thing about R is that you keep discovering ways of doing things. It's the first I heard about glob2rx in R; I typically write my regexes longhand (which btw for the globbing patterns isn't that hard), but yeah, glob2rx() works. I'll add this to the notes in the answer.Axletree
If you are using this with intention to copy and replace files, then please do not forget to add overwrite = TRUE inside file.copy()! Lost an hour debugging this.Taxiway
D
12

You can

  • use system() to fire off a command as if it was on shell, incl globbing
  • use list.files() aka dir() to do the globbing / reg.exp matching yourself and the copy the files individually
  • use file.copy on individual files as shown in mjv's answer
Denaturalize answered 5/3, 2010 at 4:33 Comment(2)
never heard of dir. It's exactly identical to list.files... strange to have bothOleary
'dir' is older, but less intutive.Burnette
B
0

For fellow script kiddies:

mainDir<-getwd() #so it creates the file wherever your working directory is
  subDir<-"name_of_my_new_sub-directory"
  dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
  setwd(file.path(mainDir, subDir))
Burnette answered 22/1 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.