从R下载Google Drive目录中的所有文件和子目录

krcsximq  于 2023-10-13  发布在  Go
关注(0)|答案(1)|浏览(104)

some prior related questions123),但没有什么是我想要的,我不能让代码示例运行Jenny Bryan posted in 2018
我有一个与我共享的文件夹,里面有一些大文件。文件是嵌套的。所以我想递归到子目录中,并从每个子目录中获取所有文件。在我的例子中,只有两层,但如果有一种方法可以适用于任意数量的层,那就太好了。
最明显的命令是简单地告诉它下载文件夹,希望它能弄清楚子结构:

#load the libraries
library(tidyverse)
library(googledrive)

#folder link to id
#hidden for privacy reasons
jp_folder = "https://drive.google.com/drive/folders/XXXXX" 
folder_id = drive_get(as_id(jp_folder))

#download in entirety
drive_download(folder_id)

不幸的是,这不起作用,因为它显然不能处理文件夹:

> drive_download(folder_id)
Error: Not a recognized Google MIME type:
  * application/vnd.google-apps.folder

下面是我通过进入每个子目录来避免这个问题的尝试:

#load the libraries
library(tidyverse)
library(googledrive)

#folder link to id
#hidden for privacy reasons
jp_folder = "https://drive.google.com/drive/folders/XXXXX" 

#get the id data frame
folder_id = drive_get(as_id(jp_folder))

#find files in folder
files = drive_ls(folder_id)

#loop dirs and download files inside them
for (i in seq_along(files$name)) {
  i_dir = drive_ls(files$id[i])
  
  #download files
  walk(i_dir$id, ~ drive_download(as_id(.x)))
}

files对象看起来很好(用填充符替换字符串):

# A tibble: 6 x 3
  name   id                                drive_resource   
* <chr>  <chr>                             <list>           
1 A      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA <named list [32]>
2 B      BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB <named list [32]>
3 C      CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC <named list [31]>
4 D      DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD <named list [31]>
5 E      EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE <named list [31]>
6 F      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF <named list [31]>

然而,当一个人试图获取subdir的内容时,它会抛出这个错误:

> i_dir = drive_ls(files$id[i])
Error: 'path' does not identify at least one Drive file.

怎么了?

velaa5lx

velaa5lx1#

其实很简单:drive_ls()需要一个 Dataframe 输入,其中包含1行,而不是字符向量。错误消息具有误导性(如果它只是告诉用户给它给予一个 Dataframe 就好了)。如果将代码更改为该代码,并添加所需的循环,则可以自动下载子目录的内容。如果有子-子-目录,它将失败。需要在包中编写并实现适当的递归函数。
这段代码对我很有效:

#load the libraries
library(stringr)
library(googledrive)
  
#folder link to id
jp_folder = "https://drive.google.com/drive/folders/XXXXX"
folder_id = drive_get(as_id(jp_folder))

#find files in folder
files = drive_ls(folder_id)

#loop dirs and download files inside them
for (i in seq_along(files$name)) {
  #list files
  i_dir = drive_ls(files[i, ])
  
  #mkdir
  dir.create(files$name[i])
  
  #download files
  for (file_i in seq_along(i_dir$name)) {
    #fails if already exists
    try({
      drive_download(
        as_id(i_dir$id[file_i]),
        path = str_c(files$name[i], "/", i_dir$name[file_i])
      )
    })
  }
}

此版本跳过已下载的文件。

相关问题