如何删除 Google Drive 中的文件? [英] How to delete a File in Google Drive?

查看:177
本文介绍了如何删除 Google Drive 中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写删除文件的 Google Apps 脚本?

How do I write a Google Apps Script that deletes files?

这会找到文件:

var ExistingFiles = DocsList.find(fileName);

但是DocsList.deleteFile不存在删除文件.

有没有办法将这些文件移动到另一个文件夹或垃圾箱?

Is there a way to move those files to another Folder or to Trash?

我会考虑的另一个解决方法是能够覆盖具有相同名称的现有文件.

The other workaround I would consider is to be able to override an existing file with the same name.

目前,当我想创建一个名称已在 MyDrive 中使用的文件时,它会创建另一个同名的文件.我想保留 1 个文件(新的保留,旧的丢失).

Currently when I want to create a file with a name already used in MyDrive then it creates a second file with the same name. I would like to keep 1 file (the new one is kept and the old one is lost).

推荐答案

有 3 种服务可用于删除文件.

There are 3 services available to delete a file.

  • DriveApp - Apps 脚本内置
  • 高级驱动器服务 - 内置于 Apps 脚本,但必须启用.比 DriveApp 拥有更多功能
  • Google Drive API - 不是内置于 Apps 脚本,但可以使用 Drive REST API 和 UrlFetchApp.fetch(url,options)
  • 从 Apps 脚本中使用
  • DriveApp - Built-in to Apps Script
  • Advanced Drive Service - Built-in to Apps Script but must be enabled. Has more capability than DriveApp
  • Google Drive API - Not built-in to Apps Script, but can be used from Apps Script using the Drive REST API together with UrlFetchApp.fetch(url,options)

DocsList 服务现已弃用.

高级驱动器服务可用于删除文件而不将其发送到回收站.认真考虑无法找回已删除文件的风险.Advanced Drive Service 有一个 remove 方法,可以删除文件而不将其发送到垃圾文件夹.高级服务具有许多与 API 相同的功能,无需发出 HTTPS GET 或 POST 请求,也不需要 OAuth 库.

The Advanced Drive Service can be used to delete a file without sending it to the trash. Seriously consider the risk of not being able to retrieve the deleted file. The Advanced Drive Service has a remove method which removes a file without sending it to the trash folder. Advanced services have many of the same capabilities as the API's, without needing to make an HTTPS GET or POST request, and not needing an OAuth library.

function delteFile(myFileName) {
  var allFiles, idToDLET, myFolder, rtrnFromDLET, thisFile;

  myFolder = DriveApp.getFolderById('Put_The_Folder_ID_Here');

  allFiles = myFolder.getFilesByName(myFileName);

  while (allFiles.hasNext()) {//If there is another element in the iterator
    thisFile = allFiles.next();
    idToDLET = thisFile.getId();
    //Logger.log('idToDLET: ' + idToDLET);

    rtrnFromDLET = Drive.Files.remove(idToDLET);
  };
};

这结合了 DriveApp 服务和 Drive API 来删除文件而不将其发送到回收站.Drive API 方法 .remove(id) 需要文件 ID.如果文件ID不存在,但文件名有,则可以先按名称查找文件,然后获取文件ID.

This combines the DriveApp service and the Drive API to delete the file without sending it to the trash. The Drive API method .remove(id) needs the file ID. If the file ID is not available, but the file name is, then the file can first be looked up by name, and then get the file ID.

要使用 DriveAPI,您需要通过资源高级 Google 服务菜单添加它.将 Drive API 设置为开启.并且确保在您的 Google Cloud Platform 中启用 Drive API.如果它没有在BOTH 处打开,它将不可用.

In order to use DriveAPI, you need to add it through the Resources, Advanced Google Services menu. Set the Drive API to ON. AND make sure that the Drive API is turned on in your Google Cloud Platform. If it's not turned on in BOTH places, it won't be available.

这篇关于如何删除 Google Drive 中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆