2017-09-23 51 views
1

例如,我在一個文件夾中有三個文件。 我想以這種方式提取它們。在Mac上的文件夾中提取文件的創建日期和大小?

file_name create_time size 
A   2017-09-11 3MB 
B   2017-09-12 2MB 
C   2017-09-13 1MB 
+0

見'?file.info'。我不知道Mac是否記錄文件創建時間。 –

+0

檢查https://stackoverflow.com/questions/34123076/osx-how-to-get-the-creation-modification-time-of-a-file-from-the-command-lin –

回答

1
library(Rcpp) 

# This likely only works on macOS 

# Define a C function to get the creation time 
cppFunction(
    includes = c("#include <sys/stat.h>"), 
    " 
long birth_time_raw(std::string x) { 
    struct stat ftime; 
    stat(x.c_str(), &ftime); 
    return(ftime.st_birthtimespec.tv_sec); 
} 
", 
) 

# Wrap it in a helper that does some sanity checks 
birth_time <- function(x) { 
    x <- path.expand(x) 
    if (!file.exists(x)) return(NULL) 
    as.POSIXct(birth_time_raw(x), origin="1970-01-01 00:00:00") 
} 

do.call(
    rbind, 
    lapply(
    dir("ƒ", full.names = TRUE), 
    function(.x) { 
     data.frame(
     file_name = basename(.x), 
     create_time = as.Date(birth_time(.x)), 
     size = sprintf("%3.1fMB", file.size(path.expand(.x))/1024/1024), 
     stringsAsFactors=FALSE 
    ) 
    } 
) 
) -> files_df 
## files_df 
## file_name create_time size 
## 1   A 2017-09-20 4.4MB 
## 2   B 2017-09-20 4.2MB 
## 3   C 2017-09-21 0.0MB 
+0

你確定'ctime '是創造時間而不是'最後狀態改變',就像幫助頁面所說的那樣? –

+0

我會dbl檢查,你很可能是對的。直到那時,勒梅才取下答案。 #ty – hrbrmstr

相關問題