[Rd] file.copy() of non-existing file creates empty file

From: Henrik Bengtsson <hb_at_stat.berkeley.edu>
Date: Sun 23 Jul 2006 - 00:44:49 GMT


Copying a non-existing file using file.copy() creates empty file. Example:

> file.exists("non-existing-file")

[1] FALSE
> file.copy("non-existing-file", "new-file")
> file.exits("new-file")

[1] TRUE
> file.info("new-file")$size

[1] 0

The reason for this is that file.copy() calls file.create() without checking if the 'from' file exists;

> 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, basename(from))     else if (nf > nt)

        stop("more 'from' files than 'to' files")     if (nt > nf)

        from <- rep(from, length.out = nt)     if (!overwrite)

        okay <- !file.exists(to)
    else okay <- rep.int(TRUE, length(to))     if (any(from[okay] %in% to[okay]))

        stop("file can not be copied both 'from' and 'to'")     if (any(okay)) {

        okay[okay] <- file.create(to[okay])
        if (any(okay))
            okay[okay] <- file.append(to[okay], from[okay])
    }
    okay
}
<environment: namespace:base>

Adding one line:

    okay <- okay & file.exists(from);

before the last if-statement will do.

/Henrik



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun Jul 23 10:46:45 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 23 Jul 2006 - 16:27:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.