Skip to content

Bug in file.copy: overwrite=FALSE ignored (PR#3529)

5 messages · Raubertas, Richard, Uwe Ligges, Duncan Murdoch

#
I am using R 1.7.1 on Windows XP Pro.

The 'overwrite=FALSE' argument appears to be ignored in
file.copy():
[1] TRUE TRUE
[1] TRUE
and sure enough, file1.txt has been overwritten.

Rich Raubertas
Biometrics Research, RY33-300
Merck & Co.

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, ...{{dropped}}
#
richard_raubertas@merck.com wrote:
Fix:

     if (!overwrite) {
         if (nt > nf)
             from <- rep(from, length = nt)
         exists <- file.exists(from)
         from <- from[exists]
         to <- to[exists]
     }
     file.create(to)
     file.append(to, from)


should be something like


     if (!overwrite) {
         if (nt > nf)
             from <- rep(from, length = nt)
         exists <- file.exists(to)
         if(sum(exists))
             warning("File(s) \"", paste(to[exists], collapse = ", "), 
"\" already exist(s)")
         from <- from[!exists]
         to <- to[!exists]
     }
     if (length(to)) {
         file.create(to)
         file.append(to, from)
     }


Uwe Ligges
#
On Mon, 21 Jul 2003 19:27:34 +0200 (MET DST),
richard_raubertas@merck.com wrote :
There was a bug in the file.copy code; I'll submit a patch.

Duncan Murdoch
#
On Mon, 21 Jul 2003 20:02:39 +0200, Uwe Ligges
<ligges@statistik.uni-dortmund.de> wrote :
I don't think that gets the return value right.  Presumably if you're
copying 5 files to 5 files, and two of the destination files already
exist, the return should consist of 3 TRUE values and 2 FALSE values.
Here's my rewrite:

file.copy <- function(from, to, overwrite=FALSE)
{
    if (!(nf <- length(from))) stop("no files to copy from")
    if (!(nt <- length(to)))   stop("no files to copy to")
    if (nt == 1 && file.exists(to) && file.info(to)$isdir)
        to <- file.path(to, from)
    else if (nf > nt)  stop("more `from' files than `to' files")
    if(nt > nf) from <- rep(from, length = nt)
    if (!overwrite) okay <- !file.exists(to)
    else okay <- rep(TRUE, length(to))
     
    file.create(to[okay])
    okay[okay] <- file.append(to[okay], from[okay])
    okay
}

Duncan Murdoch
#
On Mon, 21 Jul 2003 14:57:36 -0400, Duncan Murdoch <dmurdoch@pair.com>
wrote :
This isn't quite right either; it doesn't handle the case of no
eligible files or the case of copying a file onto itself properly.
The first is easy to fix (copying Uwe's test);  I think I'll just make
the second illegal.

Duncan Murdoch