From dmurdoch at pair.com  Mon Jul 21 15:57:36 2003
From: dmurdoch at pair.com (Duncan Murdoch)
Date: Mon Jul 21 20:53:40 2003
Subject: [Rd] Bug in file.copy: overwrite=FALSE ignored (PR#3529)
In-Reply-To: <3F1C2ABF.5050402@statistik.uni-dortmund.de>
References: <200307211727.h6LHRYJH017824@pubhealth.ku.dk>
	<3F1C2ABF.5050402@statistik.uni-dortmund.de>
Message-ID: <sfdohvkqc92tmr3gorticeq4pdoiq214at@4ax.com>

On Mon, 21 Jul 2003 20:02:39 +0200, Uwe Ligges
<ligges@statistik.uni-dortmund.de> wrote :

>     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)
>     }

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


[Rd] Bug in file.copy: overwrite=FALSE ignored (PR#3529)
Duncan Murdoch