Henrik Bengtsson
2008-Feb-27 05:54 UTC
[Rd] Unix-like touch to update modification timestamp of file?
Hi, is it possible to update the modification time stamp of a file using R (on file systems supporting it)? It is sufficient to update the modification time to the current time. The best I can do for now is: touchFile <- function(pathname, ...) { if (!file.exists(pathname)) stop("No such file: ", pathname); info <- file.info(pathname); if (info$isdir) stop("Cannot change the timestamp of a directory: ", pathname); oldTimestamp <- info$mtime; con <- NULL; on.exit({ if (!is.null(con)) close(con); }); # Zero-sized files have to be treated specially if (info$size == 0) { con <- file(pathname, open="w"); } else { con <- file(pathname, open="r+b"); seek(con=con, where=0, origin="start", rw="read"); bfr <- readBin(con=con, what="raw", n=1); seek(con=con, where=0, origin="start", rw="write"); writeBin(con=con, bfr); } invisible(oldTimestamp); } # touchFile() # 1. Create a file pathname <- tempfile() cat(file=pathname, "Hello world!") md5a <- digest::digest(pathname, file=TRUE); # 2. Current time stamp print(file.info(pathname)$mtime) ## [1] "2008-02-26 21:41:23 Pacific Standard Time" # 3. Update time stamp Sys.sleep(1.2); touchFile(pathname) print(file.info(pathname)$mtime) ## [1] "2008-02-26 21:41:24 Pacific Standard Time" # 4. Verify that the contents did not change md5b <- digest::digest(pathname, file=TRUE); stopifnot(identical(md5a, md5b)) /Henrik
Earl F. Glynn
2008-Feb-27 16:12 UTC
[Rd] Unix-like touch to update modification timestamp of file?
"Henrik Bengtsson" <hb at stat.berkeley.edu> wrote in message news:<59d7961d0802262154r2e049998yf5ce8ea08ab5d249 at mail.gmail.com>...> is it possible to update the modification time stamp of a file using R > (on file systems supporting it)?For a Windows PC, if you have RTools in your path (from http://www.murdoch-sutherland.com/Rtools/installer.html), then you should be able to use the touch that's in the RTools\bin directory: system("touch sample.dat") efg Earl F. Glynn Bioinformatics Stowers Institute for Medical Research
Nicholas Lewin-Koh
2008-Feb-29 00:36 UTC
[Rd] Unix-like touch to update modification timestamp of file?
Wow, this has to win the prize for some of the most obscure documentation ever. Kudos on your M$-archaeology. Nicholas Message: 9 Date: Wed, 27 Feb 2008 15:23:51 -0500 From: "Gabor Grothendieck" <ggrothendieck at gmail.com> Subject: Re: [Rd] Unix-like touch to update modification timestamp of file? To: "Henrik Bengtsson" <hb at stat.berkeley.edu> Cc: r-devel at stat.math.ethz.ch Message-ID: <971536df0802271223s13462925w2b3fd5d7f90805b at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Wed, Feb 27, 2008 at 1:27 PM, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:> On Wed, Feb 27, 2008 at 8:24 AM, Gabor Grothendieck > <ggrothendieck at gmail.com> wrote: > > If you only need Windows then this will do it even without RTools: > > > > shell("copy /b /v myfile +,,>nul") > > Interesting. Show I figured out that '+' is for "append", but how to > interpret the two commas? >Commas generally have various undocumented effects in Windows batch and sometimes Microsoft mentions one: http://support.microsoft.com/kb/69581