1

I am using read.csv to import the data from my csv file. I would also like to load the Created or Modified Date of the file to a new column at the end of my newly imported table. How do I read the Created or Modified date of a file into a variable? Thanks

Phil
  • 7,287
  • 3
  • 36
  • 66
JRDew
  • 129
  • 1
  • 1
  • 13
  • Please provide a small [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example. – user2100721 Jul 09 '16 at 14:51
  • 1
    Sounds like you want `file.info()`. – Kevin Ushey Jul 10 '16 at 04:26
  • Kevin - You are correct file.info() is what I need. I want to use ctime but am having an issue with syntax. I can get file.mtime() to work correctly but when I try to get ctime file.info(??ctime??) I am not sure of the syntax. – JRDew Jul 11 '16 at 12:16
  • As far as an example, I have a file MyFile="C:\myfile.csv" Then I read it into R -- CurrentFile = read.csv(MyFile,header = F,strip.white=TRUE) and now I want the create date of the file I just read in. – JRDew Jul 11 '16 at 12:21
  • Okay, I have a solution or at least a work around. I read in all the file info values into FileInfo. I then extract the 'ctime' from FileInfo. FileInfo = file.info("C:\myfile.csv) CreateTime = format(FileInfo['ctime'],"%m/%d/%Y") I don't know if it is right, but it works. Thanks! – JRDew Jul 11 '16 at 14:20

1 Answers1

1

This function may work well:

read.tables <- function(file.names, ...) {require(plyr)
ldply(file.names, function(fn) data.frame(read.table(fn, ...), Filename=fn, date.extraction=date(file.info(fn)$mtime)))}

I simply modified the function given in this topic: how to read table multiple files into a single table in r

colidyre
  • 4,170
  • 12
  • 37
  • 53
VincentP
  • 89
  • 10