Moving files between folders using AppScript and Google Shared Drives (formerly Team Drives)

51 views
Skip to first unread message

Jason Tucker

unread,
Sep 10, 2019, 5:18:21 PM9/10/19
to Google Apps Script Community

I'm working on a project in Google Sheets using AppScript to move a file from one folder to another in a folder in a Google Shared Drive (formerly Team Drive).  I have a bunch of CSVs I want to move from one folder to another once I'm done processing them and I'm running into issues stating that I cant do this because its in a google shared drive. Is "supportsAllDrives" required?

What I'm waiting to move is move a file into a folder with aaaaa as the folder ID and then later move the file to another folder. My understanding is you dont move folder but rather apply files to a folder. So with the first function adding the file to the aaaaa folder and the second function im adding the file to another folder with the ID of bbbbb and then removing the file from folder aaaaa. Am I doing this right? There seems to be a huge gap in documentation on how do this the right way.



function move_file_to_folder()
{
   
var newDoc = DocumentApp.create('Testing Team Drive MoveTo').getId();
   
var file = DriveApp.getFileById(newDoc);
   
var moveFile = DriveApp.getFolderById('aaaaa').addFile(file);
}


function move_file_to_another_folder()
{
   
var filesIterator = DriveApp.getFilesByName('Testing Team Drive MoveTo');
   
while (filesIterator.hasNext())
   
{
       
var file = filesIterator.next();
   
}
   
var cleanup = DriveApp.getFolderById('bbbbb').addFile(file,{"supportsAllDrives": true});
   
var moveFile = DriveApp.getFolderById('aaaaaa').removeFile(file,{"supportsAllDrives": true});
}

Romain Vialard

unread,
Sep 11, 2019, 4:02:55 AM9/11/19
to Google Apps Script Community
A file in a Shared Drive can only have one parent. Here your file already has a parent ('aaaaa') and you are trying to add another one ('bbbbb').
It won't work. Best would be to remove the old parent and assign the new one in a single call.
eg:
function moveFileBetweenSharedDrives() {
 
var file = Drive.Files.get("1XAphJALlz", {"supportsAllDrives": true});
 
Drive.Files.patch(file, "1XAphJALlz", {
    supportsAllDrives
: true,
    addParents
: "0AHUZ9Z7Uk9PVA",
    removeParents
: "0AJPPDOUk9PVA"
 
});
}


Reply all
Reply to author
Forward
0 new messages