file.info在r中返回NA值

nwlqm0z1  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(70)

我试图从我的目录中获取文件的大小和其他详细信息,但它返回的是NA值的一些文件,但它返回的是其他文件的详细信息。下面是我使用的代码。将有任何文件的管理员设置来获取这些详细信息??

library(tidyr)
library(dplyr)
wd <- "F:\\working\\others"
setwd(wd)
#get file list - your list of files would be different
fileList <- list.files()[1:240]
class(fileList)
#result
cbind(
  file.info(fileList)[,c("size"), drop=FALSE],
  x = as.character(file.mtime(fileList))) %>% 
  separate(x,
           into = c("DateModified","TimeModified"),
           sep=" ") %>% 
  add_rownames %>% 
  select(DateModified,
         TimeModified,
         Size=size,
         FileName=rowname)

字符串

acruukt9

acruukt91#

试试这个:技巧是完整的。names = TRUE

ldir <-  normalizePath("<type here the path of directory>")
finf <- file.info(dir(path = ldir, full.names = TRUE), extra_cols = FALSE)
View(finf)

字符串

uubf1zoe

uubf1zoe2#

值得注意的一点是,至少在Windows上,如果路径是一个文件夹,R会给予NA。如果有人有这个问题,他们可以使用fs::is_dir()来识别这些文件夹,并实现不同的方法(可能是递归计算最新日期)。
比如说,

folder_last_modified <- function(path, depth = "max") {
  # if (depth == "max") { # then, `recursive = TRUE`
    files <- list.files(path, full.names = TRUE, recursive = TRUE)
  # }
  max(fs::file_info(files)$modification_time)
}

last_modified <- function(path) {
  if (fs::is_dir(path)) {
    folder_last_modified(path)
  } else {
    fs::file_info(path)$modification_time
  }
}

lapply(character_vector_of_paths, last_modified)

字符串

相关问题