Home > OS >  Error of duplication in root drive with google script while copying a file from one folder to anothe
Error of duplication in root drive with google script while copying a file from one folder to anothe

Time:08-02

So, I'm trying to copy a file which is updated automatically in my computer in a folder. Thing is that every time the script runs, it copies the file in the selected folder but also creates a duplication in the root folder despite root isn't referred in the whole script

function moveFiles() {

  var olderfiles = DriveApp.getFolderById("idfromdestinyfolder").getFiles(); 
  while (olderfiles.hasNext()) {

    var olderfile = olderfiles.next();
    var pull = DriveApp.getFolderById("idfromdestinyfolder");
    pull.removeFile(olderfile)
  }

  var files = DriveApp.getFolderById("idfromstartingfolder").getFiles();
  while (files.hasNext()) {

    var file = files.next();
    var destination = DriveApp.getFolderById("idfromdestinyfolder");
    file.makeCopy("nameofthefile",destination);
  }
}

The first part olderfiles checks if a file exist in the destiny, and if exists, it removes it. The second part copies the file from the starting folder to destination.

Thing is, every time it runs, it creates a copy in root which should not happen. Is the first time I'm using google script

CodePudding user response:

From the question

... it copies the file in the selected folder but also creates a duplication in the root folder despite root isn't referred in the whole script

The problem might be caused by the use of Class Folder.removeFile(child). Pleaser bear in mind that it's a deprecated method, the docs instructs to use Folder.moveTo(destination) instead.

If you are looking to send the file to the trash, then use Class File.setTrashed(true)

Related

CodePudding user response:

Try this makeCopy()

function makeCopy() {
  var folderid = "folderid";
  var fileid = "fileid";
  var folder = Drive.Files.get(folderid, { "supportsAllDrives": true });
  var newFile = { "fileId": fileid, "parents": [folder] };
  var args = { "resource": { "parents": [folder], "title": "new Title" }, "supportsAllDrives": true };
  Drive.Files.copy(newFile, fileid, args);
}
  • Related