rcurl :: geturl如何仅列出文件夹中的文件URL

发布于 2025-02-01 16:19:50 字数 971 浏览 2 评论 0原文

我想在远程SFTP服务器中列出文件,因此我这样做了:

    url <- "sftp://remoteserver.com/dir/"
    credentials <- "myusrname/mypwd"

    file_list <- tryCatch({
    
        RCurl::getURL(
          url,
          userpwd = credentials,
          ftp.use.epsv = FALSE,
          dirlistonly = TRUE,
          forbid.reuse = TRUE,
          .encoding = "UTF-8"
        )
    
      }, error = function(e) {
        as.character()
      })

但是,在file_list中,除了该文件夹中的文件的URL外,我也有一些我不''的额外条目。需要:

# at the beginning of the vector
[1] "sftp://remoteserver.com/dir/."
[2] "sftp://remoteserver.com/dir/.."

# at the end of the vector
[67] "sftp://remoteserver.com/dir/"

有没有办法避免这些条目?使用以下代码删除它们是安全的吗?

file_list <- file_list[c(-1, -2)]
file_list <- file_list[-length(file_list)]

I want to list the files in a remote SFTP server, so I did this:

    url <- "sftp://remoteserver.com/dir/"
    credentials <- "myusrname/mypwd"

    file_list <- tryCatch({
    
        RCurl::getURL(
          url,
          userpwd = credentials,
          ftp.use.epsv = FALSE,
          dirlistonly = TRUE,
          forbid.reuse = TRUE,
          .encoding = "UTF-8"
        )
    
      }, error = function(e) {
        as.character()
      })

However, in file_list, except for the URLs of the files in that folder, there are also some extra entries that I don't need:

# at the beginning of the vector
[1] "sftp://remoteserver.com/dir/."
[2] "sftp://remoteserver.com/dir/.."

# at the end of the vector
[67] "sftp://remoteserver.com/dir/"

Is there a way to avoid these entries? Is it safe to use the following code to just delete them?

file_list <- file_list[c(-1, -2)]
file_list <- file_list[-length(file_list)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2025-02-08 16:19:50

我认为这不是最好的方法,以防它并不总是按顺序排列。如果您想要一个.logs文件的所有内容,那么我会做这样的事情:

library(dplyr)
library(stringr)

file_list <- c(
  "sftp://remoteserver.com/dir/.",
  "sftp://remoteserver.com/dir/.",
  "sftp://remoteserver.com/dir/names.logs",
  "sftp://remoteserver.com/dir/"
)

as_tibble(file_list) %>% # because it's just easier for me to think of things as dataframes 
  filter(str_detect(value, "logs$")) %>% 
  pull()


[1] "sftp://remoteserver.com/dir/names.logs"

I don't think that's the best method in case it's not always in that order. If you want everything that is a .logs file, then I'd do something like this:

library(dplyr)
library(stringr)

file_list <- c(
  "sftp://remoteserver.com/dir/.",
  "sftp://remoteserver.com/dir/.",
  "sftp://remoteserver.com/dir/names.logs",
  "sftp://remoteserver.com/dir/"
)

as_tibble(file_list) %>% # because it's just easier for me to think of things as dataframes 
  filter(str_detect(value, "logs
quot;)) %>% 
  pull()


[1] "sftp://remoteserver.com/dir/names.logs"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文