0

hey i need to compare files by their names,If they have the same name than we copied the value of the item F2 the new file on the old one and we delete the new file.

  1. The files are on Different folder

  2. I need to compare files by their names

if they have the same name than : we coped the value of F2 new file ----->to the old one and delete new file

think you for your help

  • Here's a similar post [Delete a File in Google Drive](http://stackoverflow.com/questions/14241237/google-apps-script-how-to-delete-a-file-in-google-drive) – Alan Wells Aug 06 '14 at 17:29

2 Answers2

0

You can use:

getFilesByName(name) Google Documentatin

Then:

setTrashed(trashed)

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

Sandy posted the correct method (getFilesByName()) for the DriveApp.folder class.

A code sample would look something like this:

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);
  }
}
Cyrus Loree
  • 837
  • 6
  • 7