Andronikos K.
Andronikos K.

Reputation: 115

Accessing file dates in R

I am trying to run a R script which needs to use the file timestamp.

Using windows explorer, I realised that there are 3 ‘types of file dates’ (“Date”, “Date created”, and “Date modified”). See below.

file dates in windows explorer

When using R function file.info() you can read “Date created” and “Date modified”, but not the “Date” column (as apperead on Windows explorer) This date column is the one I need.

photo.directory <- file.info(getwd())
head(photo.directory)
                 size isdir mode               mtime               ctime
IMG_0381.jpg 15904788 FALSE  666 2017-05-02 13:55:14 2017-05-25 15:39:36
IMG_0382.jpg  8895692 FALSE  666 2017-05-02 13:55:17 2017-05-25 15:39:36
IMG_0383.jpg  8731599 FALSE  666 2017-05-02 13:55:21 2017-05-25 15:39:37
IMG_0384.jpg 15189133 FALSE  666 2017-05-02 13:55:25 2017-05-25 15:39:37
IMG_0385.jpg 10545637 FALSE  666 2017-05-02 13:55:29 2017-05-25 15:39:38
IMG_0386.jpg 10565079 FALSE  666 2017-05-02 13:55:31 2017-05-25 15:39:38
                           atime exe
IMG_0381.jpg 2017-05-25 15:39:36  no
IMG_0382.jpg 2017-05-25 15:39:36  no
IMG_0383.jpg 2017-05-25 15:39:37  no
IMG_0384.jpg 2017-05-25 15:39:37  no
IMG_0385.jpg 2017-05-25 15:39:38  no
IMG_0386.jpg 2017-05-25 15:39:38  no

Any ideas how to overcome this in R?

Upvotes: 3

Views: 2163

Answers (1)

Val
Val

Reputation: 7023

The Date column normally refers to the earlier value between "Date created" and "Date modified". However, for files like JPGs, there are special Date fields inside the file's metadata which take precedence.

For R, there's the package exifr, which lets you read an EXIF header to retrieve the dates you want.

Since you're running Windows, you will probably need to install Perl first. You can find more infos on that on the package's github page.

I'm running ubuntu, so it was quite straightforward:

# Function to retrieve data

filedate <- function(f){

  require(exifr)

  if (grepl('.jpg$',f)){

  d <- exifr(f)$DateTimeOriginal

  }else{

  finfo <- file.info(f)

  d <- c(finfo$ctime,finfo$mtime)[which.min(c(finfo$ctime,finfo$mtime))]

  }

  return(d)

}


# list files

fls <- list.files(getwd(),full.names=TRUE)
fls_dts <- sapply(fls,filedate)

Note that I'm just separating for JPG files ... I could add more extensions of filetypes with EXIF to the pattern. Alternatively I could run exifr for all files, and add an if clause to run the file.info if exifr returns NULL (meaning that the file in quiestion didn't have an EXIF format.

HTH

Upvotes: 3

Related Questions