我需要按名称比较文件并删除新文件

时间:2014-08-05 16:43:05

标签: google-apps-script

嘿我需要按名字比较文件,如果它们的名称与我们复制项目F2的值相同,那么旧文件就会删除新文件。

  1. 文件位于不同文件夹

  2. 我需要按名称比较文件

  3. 如果它们的名称相同:我们将F2新文件----->的值更改为旧文件并删除新文件

    认为你的帮助

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

Sandy为DriveApp.folder类发布了正确的方法(getFilesByName())。

代码示例如下所示:

function fileChecker(){
  try{
    var folder1id = '';
    var folder2id = '';

    var folder1 = DriveApp.getFolderById(folder1id);
    var folder2 = DriveApp.getFolderById(folder2id);

    //get the files in folder 1
    var files = folder1.getFiles();

    // loop through the files in folder 1
    while(files.hasNext()){

      // get the individual file in folder 1 to process
      var file = files.next(); 

      // check for same name in folder 2
      var files2 = folder2.getFilesByName(file.getName());

      // the code below assumes there is only the potential for a single file
      // in folder 2 with the same name as folder 1
      // otherwise additional processing would need to be handled
      if(files2.hasNext()){ 
        var file2 = files2.next();
        file.setTrashed(true);
        // add the file to folder 1
        folder1.addFile(file2);
        // remove the file from folder 2
        folder2.removeFile(file2);
      }
    }
  }catch(err){
    Logger.log(err.lineNumber + ' - ' + err);
  }
}