[Rd] file.rename(): Guaranteed to be complete or not at all?

Henrik Bengtsson hb at biostat.ucsf.edu
Wed Mar 2 05:41:53 CET 2011


assume I have an existing file 'pathname' and I want to rename it to
'pathnameN' (which does not exist).  I use:

res <- file.rename(pathname, pathnameN);

Is it guaranteed that:

(1) if res == TRUE, the file now have name 'pathnameN' and there is no
file with name 'pathname'?
(2) if res == FALSE, nothing has changed?

or could it theoretically also be the case that

(3) there are say two identical files named 'pathname' and 'pathnameN',
(4) or worse, that neither exists?

I can see how (3) could happen if the file is renamed by first using
file.copy() and then file.remove() while there is lack of write/delete
permission for the latter.

Currently, my code asserts that (3) and (4) did not happen.  Is that
unnecessary - does file.rename() do that for me (regardless of OS)?



More information about the R-devel mailing list