Moving Files In Google Drive Using Google Script

39,335

Solution 1

If we make a copy of the file and trash the original, it would change the file URL and also the file sharing settings won't be preserved.

In Drive, it is possible to add a file to multiple folders with the .addFolder() method of DriveApp service. You can add the file to the target folder and then remove the file from the immediate parent folder.

function moveFiles(sourceFileId, targetFolderId) {
  var file = DriveApp.getFileById(sourceFileId);
  var folder = DriveApp.getFolderById(targetFolderId);
  file.moveTo(folder);
}

Solution 2

This is my first post! I know this has been answered a few times, but I actually came across this question while working on my project, and while reviewing the Apps Script documentation, I figured out a concise way to do it. A variation of some1's answer.

var file = DriveApp.getFileById(fileid);
DriveApp.getFolderById(folderid).addFile(file);
DriveApp.getRootFolder().removeFile(file);

Hope it helps!

Solution 3

It looks like there is now a moveTo() function with the Drive API (advanced services) that makes it easy to move files:

moveTo(destination)

Moves this item to the provided destination folder.

The current user must be the owner of the file or have at least edit access to the item's current parent folder in order to move the item to the destination folder.

Here is some code I used to move all files from the "screenshot input" folder to the "screenshot processed" folder:

var inputFolder = DriveApp.getFolderById(SCREENSHOT_INPUT_FOLDER_ID);
var processedFolder = DriveApp.getFolderById(SCREENSHOT_PROCESSED_FOLDER_ID);

var files = inputFolder.getFiles();
while (files.hasNext()) {
    var file = files.next();
    file.moveTo(processedFolder);
}

Solution 4

There is no direct method in the File or Folder Classes to move files from one folder in Google Drive to another. As you mentioned you can copy the file to another folder with the method makeCopy() and then delete it with setTrashed(), the code should look like this:

  var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
  var newDoc = DocumentApp.create(requestID + " - " + requestSummary); // Creates the Document in the user's Drive root folder

  // Modify the new document here, example:
  // var body = newDoc.getBody();
  // body.appendParagraph("A paragraph.");
  // newDoc.saveAndClose();

  var driveFile = DriveApp.getFileById(newDoc.getId()); // Gets the drive File

  driveFile.makeCopy(newDoc.getName(), targetFolder); // Create a copy of the newDoc in the shared folder
  driveFile.setTrashed(true);  //  sets the file in the trash of the user's Drive

EDIT:

In a second thought and taking into account Ruben's comments. I agree that it is a better practice to implement Amit's answer.

Solution 5

A bit safer approach compared to the previous ones:

  1. If you remove link to the file first, then you will not be able to addFile.

  2. If file is already located in the target folder, then the approach provided by Amit (https://stackoverflow.com/a/38810986/11912486) only removes file.

So, I suggest to use the following approach:

function move_file(file_id, target_folder_id) {
  var source_file = DriveApp.getFileById(file_id);
  var source_folder = source_file.getParents().next();
  if (source_folder.getId() != target_folder_id) {
    DriveApp.getFolderById(target_folder_id).addFile(source_file);
    source_folder.removeFile(source_file);
  }
}

can be improved by:

  • javascript camel style

  • multiple locations validation

Share:
39,335
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to create documents using information posted through Google forms, then once the document is created I would like to move the document into a shared folder for people to view.

    At the moment I have the script taking all of the information from the Google Forms linked spreadsheet.

    Using that information I'm using the following code to create the document:

      var targetFolder = DriveApp.getFolderById(TARGET_FOLDER_ID);
      var newDoc = DocumentApp.create(requestID + " - " + requestSummary);
    

    This is creating the document successfully in my Google Drive root folder, but I can't seem to move it where I want to move it to.

    I've seen a lot of posts suggesting use stuff like targetFolder.addFile(newDoc) but that doesn't work, similarly I've seen examples like newDoc.addToFolder(targetFolder) but again this isn't working for me.

    It seems that all the online questions people have already asked about this are using the previous API versions that are no longer applicable and these methods do not apply to the new DriveApp functionality.

    What I would like, if possible, is to create the new document as above so that I can edit the contents using the script, then be able to move that file to a shared folder. (From what I understand there is no 'move' function at present, so making a copy and deleting the old one will suffice).

  • ocordova
    ocordova over 7 years
    Thanks @Rubén (: I think both answers are right in the context of the user's needs. @Amit is right about changing id and sharing settings, that is important when you want to move files from one folder to another and they have already been shared to some users.. In this case @S Woodhouse wants to create the file, modify it and then move it, I think he doesn't care about the id and sharing settings of the "temporary file.
  • ocordova
    ocordova over 7 years
    Personally I would use another approach, by using the Advanced Drive Service to create the final document and inserting it in directly in the target folder. with Drive.Files.insert(file) , but that is for the user to decide (:
  • Rubén
    Rubén over 7 years
    We could say that both posts answers the question but I think that adding/removing a file from folders is a better practice to move a file than creating a copy file and deleting the original one. Actually, the second is more related to the main content (data) than the file itself (data and metadata).
  • ocordova
    ocordova over 7 years
    I totally agree on that ;) Also on I'm not sure but maybe creating a copy of the file will impact on the daily quota, that will be another disadvantage of this approach. I edited my answer so it adds value to the OP or future readers (:
  • Senseful
    Senseful over 5 years
    Probably safer to store a reference to the original folder, add the file to the new one, and then remove it from the old one. That way if adding it to the new one failed, it's still in the old folder.
  • Senseful
    Senseful over 5 years
    Also note that if you're moving the files from My Drive in to Team Drive, you don't need to call removeFile. Simply calling addFile causes the file to be removed from My Drive. Probably because the ownership changes.
  • Juuso Nykänen
    Juuso Nykänen over 3 years
    The .addFile method is currently deprecated and does not work anymore. Use File.moveTo() as mentioned in other answers.
  • Juuso Nykänen
    Juuso Nykänen over 3 years
    The .addFolder() method you mention is also deprecated. Sorry for being a pain :D