54

I am using R function list.files to get a list of files in a folder. I would also like to record when each file was created, modified and accessed

how can i do that? i tried google search, but didnt find any useful command

Below screenshot is from my Windows machine. I get it when i right click a file name and click "properties"

enter image description here

user2543622
  • 5,760
  • 25
  • 91
  • 159

2 Answers2

95

Check out file.info() for ctime and other file properties:

## With a single path
p <- R.home()
file.info(p)$ctime
# [1] "2014-11-20 08:15:53 PST"

## With a vector of multiple paths
paths <- dir(R.home(), full.names=TRUE)
tail(file.info(paths)$ctime)
# [1] "2014-11-20 09:00:45 PST" "2014-11-20 09:00:47 PST"
# [3] "2014-11-20 09:00:47 PST" "2014-11-20 09:00:50 PST"
# [5] "2014-11-20 09:00:33 PST" "2014-11-20 09:00:33 PST"

Notice the definition of ctime isn't necessarily the file's creation time:

What is meant by the three file times depends on the OS and file system. On Windows native file systems ctime is the file creation time (something which is not recorded on most Unix-alike file systems).

wibeasley
  • 5,000
  • 3
  • 34
  • 62
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • This provides me ctime that is the time when the file was created but it don't give me the data about when the original file was created. I can see this by clicking right at the file, choosing "Properties". A window opens and click in the tab "Details". There you can see when the File was Created. But I am interested in the section "Origin": When the content was created? – Corina Roca Oct 04 '21 at 17:59
  • I found actually another answer that fits with my question above: https://stackoverflow.com/questions/67539413/is-there-a-way-to-get-the-content-created-time-of-an-excel-file-in-r – Corina Roca Oct 04 '21 at 18:59
  • I don't think that ctime ist create time. ctime is change time: change of metadata, see [here](https://unix.stackexchange.com/questions/244108/is-ctime-on-linux-always-greater-or-equal-as-mtime). – giordano Jan 21 '23 at 22:39
1

fs::file_info() and fs::dir_info() return a data.frame where the rows are files and columns are file characteristics.

p <- R.home()

tail(fs::file_info(p)$modification_time)
#> [1] "2022-05-21 12:54:00 CDT"
tail(fs::file_info(p)$change_time)
#> [1] "2022-05-21 12:54:00 CDT"
tail(fs::file_info(p)$access_time)
#> [1] "2022-07-03 18:50:48 CDT"

Like base::file.info(), notice the definitions of these characteristics are system-dependent and may not be what you expect for your OS.

Created on 2022-07-03 by the reprex package (v2.0.1)

wibeasley
  • 5,000
  • 3
  • 34
  • 62