[英]How to copy a folder from Google Drive to create a new folder in Google drive using JS
[英]How to copy a google drive document from one folder to another using JavaScript API
我的 Google 驱动器中有两个文件夹Folder1
和Folder2
。
我在 Folder1 中创建了一个谷歌驱动器文档doc1
。 稍后我需要将此文档复制到Folder2。 我怎样才能做到这一点。
我尝试了下面的链接,但它在同一个文件夹中创建了副本。
https://developers.google.com/drive/v2/reference/files/copy
/**
* Copy an existing file.
*
* @param {String} originFileId ID of the origin file to copy.
* @param {String} copyTitle Title of the copy.
*/
function copyFile(originFileId, copyTitle) {
var body = {'title': copyTitle};
var request = gapi.client.drive.files.copy({
'fileId': originFileId,
'resource': body
});
request.execute(function(resp) {
console.log('Copy ID: ' + resp.id);
});
}
正如我在此博客上所读到的,没有直接的方法可以将文件从 Google Drive 中的一个文件夹移动到另一个文件夹。 您的选择是将文件复制到另一个文件夹,将其名称设置为原始文件,然后使用 File 的setTrashed(true) 方法删除原始文件。
请注意,如果脚本在其他用户下运行时文件已由其他用户上传,则此方法将失败。
这是一个示例片段:
function copyFile(originFileId, destinationFileId) {
var files = originFileId.getFiles();
while (files.hasNext()) {
var file = files.next();
file.makeCopy(target).setName(file.getName());
file.setTrashed(true);
}
希望这可以帮助! :)
2020 年的 Hackaround。转到 Google Colab 并使用! cp path1 path2
! cp path1 path2
您甚至可以对目录使用 -r 选项。 漂亮整齐! 尽管我应该警告您,如果您的文件夹包含大量文件或包含大文件,则此方法会很慢。
编辑:Colab 中的错字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.