简体   繁体   中英

Read file info of a zip-file in r

I want to read the time (timestamp) of zip-files in a particular folder. I have tried this:

file.info("C:/Users/foo/")$mt

This seems to read all the time stamps of the other non-zipt files in the folder.

I also used

file.info(list.files("C:/Users/foo ", pattern=".zip"))

This resulted in NA's for all variables: size, isdir, mtime etc. are all < NA>

list.files("C:/Users/foo ", pattern=".zip")) shows the zip-files, as expected.

Q: How to read the creation date and time of zip files (in a folder)?

I'm first disregarding one and maybe two typos: Find.file does not exist, and though possible, I don't often see a space at the end of a path.

Let me show you where your logic is missing something:

list.files("c:/Users/foo", pattern=".zip")
# [1] "file1.zip" "file2.zip"

Okay, so far so good.

file.info(list.files("c:/Users/foo", pattern=".zip"))
#           size isdir mode mtime ctime atime  exe
# file1.zip   NA    NA <NA>  <NA>  <NA>  <NA> <NA>
# file2.zip   NA    NA <NA>  <NA>  <NA>  <NA> <NA>

Okay, that's the problem. Why? Let's break it down by looking at one specific file. The first return from the working list.files is file1, let's try that:

file.info("file1.zip")
#           size isdir mode mtime ctime atime  exe
# file1.zip   NA    NA <NA>  <NA>  <NA>  <NA> <NA>

Still a problem. Let's see where we are ...

getwd()
# [1] "c:/Users/foo/somewhere/else"

file.info has no idea what path= argument you provided to list.files : it assumes you are giving is absolute or relative paths to filenames. And you are: a path relative to the current directory. If you full.names=TRUE to provide the full path to all listed files, you'll see:

list.files("c:/Users/foo", pattern=".zip", full.names=TRUE)
# [1] "c:/Users/foo/file1.zip" "c:/Users/foo/file2.zip"

this is better prepared for other functions. From here, file.info(...) should work fine.

file.info(list.files(path="c:/Users/foo", pattern=".zip", full.names=TRUE))
#                        size isdir mode               mtime               ctime               atime exe
# c:/Users/foo/file1.zip    0 FALSE  666 2018-04-30 07:31:24 2018-04-30 07:31:24 2018-04-30 07:31:24  no
# c:/Users/foo/file2.zip    0 FALSE  666 2018-04-30 07:31:26 2018-04-30 07:31:26 2018-04-30 07:31:26  no

This seems to work:

file.info(list.files("C:/Users/foo ", pattern=".zip", full.names=TRUE)) $mt

adding full.names=TRUE to list.files () seems to be the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM