Unix-like touch to update modification timestamp of file?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unix-like touch to update modification timestamp of file?

Henrik Bengtsson
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unix-like touch to update modification timestamp of file?

Earl F. Glynn
"Henrik Bengtsson" <[hidden email]> wrote in message
news:<[hidden email]>...


> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unix-like touch to update modification timestamp of file?

Gabor Grothendieck
If you only need Windows then this will do it even without RTools:

shell("copy /b /v myfile +,,>nul")



On Wed, Feb 27, 2008 at 11:12 AM, Earl F. Glynn
<[hidden email]> wrote:

> "Henrik Bengtsson" <[hidden email]> wrote in message
> news:<[hidden email]>...
>
>
> > 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
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unix-like touch to update modification timestamp of file?

Henrik Bengtsson
On Wed, Feb 27, 2008 at 8:24 AM, Gabor Grothendieck
<[hidden email]> 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?

Thanks to both you.

Henrik

>
>
>
>
>
>  On Wed, Feb 27, 2008 at 11:12 AM, Earl F. Glynn
>  <[hidden email]> wrote:
>  > "Henrik Bengtsson" <[hidden email]> wrote in message
>  > news:<[hidden email]>...
>  >
>  >
>  > > 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
>  >
>  >
>  > ______________________________________________
>  > [hidden email] mailing list
>  > https://stat.ethz.ch/mailman/listinfo/r-devel
>  >
>
>  ______________________________________________
>  [hidden email] mailing list
>  https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unix-like touch to update modification timestamp of file?

Gabor Grothendieck
On Wed, Feb 27, 2008 at 1:27 PM, Henrik Bengtsson <[hidden email]> wrote:
> On Wed, Feb 27, 2008 at 8:24 AM, Gabor Grothendieck
> <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Unix-like touch to update modification timestamp of file?

Nicholas Lewin-Koh
In reply to this post by Henrik Bengtsson
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" <[hidden email]>
Subject: Re: [Rd] Unix-like touch to update modification timestamp of
        file?
To: "Henrik Bengtsson" <[hidden email]>
Cc: [hidden email]
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Feb 27, 2008 at 1:27 PM, Henrik Bengtsson <[hidden email]>
wrote:
> On Wed, Feb 27, 2008 at 8:24 AM, Gabor Grothendieck
> <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel