从 FTP 服务器检索文件的修改日期

Retrieve date modified of a file from an FTP Server

基于这个问题 (),很清楚如何获取修改日期值。但是,即使在 FTP 站点上可见,也不会返回完整日期。

这显示了如何获取位于 ftp://ftp.FreeBSD.org/pub/FreeBSD/

的文件的修改日期值
library(curl)
library(stringr)

con <- curl("ftp://ftp.FreeBSD.org/pub/FreeBSD/")
dat <- readLines(con)
close(con)

no_dirs <- grep("^d", dat, value=TRUE, invert=TRUE)
date_and_name <- sub("^[[:alnum:][:punct:][:blank:]]{43}", "", no_dirs)
dates <- sub('\s[[:alpha:][:punct:][:alpha:]]+$', '', date_and_name)
dates
## [1]  "May 07  2015" "Apr 22 15:15" "Apr 22 10:00"

一些日期采用 month/day/year 格式,其他日期采用 month/day/hour/minute 格式。

查看 FTP 网站,所有日期均采用 month/day/year hour/minutes/seconds 格式。

我认为它与 Unix 格式标准有关(在 FTP details command doesn't seem to return the year the file was modified, is there a way around this? 中有解释)。如果能得到完整的日期就好了。

如果您使用 download.file,您将获得目录的 html 表示形式,您可以使用 xml2 包对其进行解析。

read_ftp <- function(url)
{
  tmp <- tempfile()
  download.file(url, tmp, quiet = TRUE)
  html <- xml2::read_html(readChar(tmp, 1e6))
  file.remove(tmp)
  lines <- strsplit(xml2::xml_text(html), "[\n\r]+")[[1]]
  lines <- grep("(\d{2}/){2}\d{4}", lines, value = TRUE) 
  result <- read.table(text = lines, stringsAsFactors = FALSE)
  setNames(result, c("Date", "Time", "Size", "File"))    
}

这让您可以这样做:

read_ftp("ftp://ftp.FreeBSD.org/pub/FreeBSD/")
#>         Date    Time      Size        File
#> 1 05/07/2015 12:00AM     4,259  README.TXT
#> 2 04/22/2020 08:00PM        35   TIMESTAMP
#> 3 04/22/2020 08:00PM Directory development
#> 4 04/22/2020 10:00AM     2,325   dir.sizes
#> 5 11/12/2017 12:00AM Directory         doc
#> 6 11/12/2017 12:00AM Directory       ports
#> 7 04/22/2020 08:00PM Directory    releases
#> 8 11/09/2018 12:00AM Directory   snapshots

reprex package (v0.3.0)

于 2020-04-22 创建