将Google云端硬盘上的重复文件与GAS进行比较

问题描述 投票:0回答:1

我希望下面的GAS删除给定文件夹中具有相同名称的文件。另一个循环中的循环应将所有文件与所有文件进行比较,但不是。

由于该文件夹中有5个文件,该脚本应进行5x5比较,但仅进行5x5比较。我的错在哪里?

代码:

function setTrashedDuplicatedFiles(){
  var folder = DriveApp.getFolderById("1FfbNvPQpg6Ilx-5VLuSIePvpl-FxewFl");
  var files = folder.getFiles();
  var files_ = folder.getFiles();

  while (files.hasNext())
  { 
    var file = files.next()
    while (files_.hasNext())
    {
      var file_ = files_.next()
      Logger.log("file: " + file + " = " + "file_: " + file_)
      Logger.log("file.getId(): " + file.getId() + " = " + "file_.getId(): " + file_.getId())
      if((file === file_) && (file.getId() != file_.getId()))
      {
        file.setTrashed(true)
      }
    }
  }
}

当前输出:

file: File1.pdf = file_: File1.pdf
file.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA = file_.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA
file: File1.pdf = file_: File2.pdf
file.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA = file_.getId(): 1LY68d1aP9DLe5LqkzZyCJVdQIAm9XzzY
file: File1.pdf = file_: File2.pdf
file.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA = file_.getId(): 19tKdBNt5bzxAOfcO1n14a_DbjWLb-LN-
file: File1.pdf = file_: File1.pdf
file.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA = file_.getId(): 1Quwyi6WIP0dt3qgV4S5XJof1wMu9mRP1
file: File1.pdf = file_: File1.pdf
file.getId(): 1yyFlRAGISjKstVxPRu8Eqi4iCIYJGDZA = file_.getId(): 1fjMu1NEJu9ZI3aFhYpFyLkw_o79KiTQa
javascript google-apps-script google-drive-api
1个回答
0
投票
  • 您要在文件名重复时将文件移到垃圾箱。
© www.soinside.com 2019 - 2024. All rights reserved.