0

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • There is no method `removeFile` in [class Folder](https://developers.google.com/apps-script/reference/drive/folder) – Cooper Aug 01 '22 at 15:44
  • 1
    @Cooper It's in the list of deprecated methods. So it exists, but they shouldn't use it in new code. – Barmar Aug 01 '22 at 16:28

2 Answers2

0

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Deprecated methods should still work. What destination should they move to if they want to remove the file? Is there a standard Trash folder for this? – Barmar Aug 01 '22 at 16:33
  • @Barmar Use Class File.setTrashed(true) (answer edited) – Rubén Aug 01 '22 at 16:39
  • It worked, changed the pull directly by setTrashed and didn't generate a copy. var file2 = olderfile.next(); file2.setTrashed(true) – Matias Tiberio Aug 01 '22 at 19:04
0

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);
}
Cooper
  • 59,616
  • 6
  • 23
  • 54