Robert Johnson
Robert Johnson

Reputation: 21

Move Folder Using App Script

So i'm brand new to JS and trying to build a program for internal operation at the company I work for. Below is the first bit of my code. I have a created a google form, and set a trigger so that when the submit button is clicked the below code will start. The trigger works and the first two functions work, but I cannot seem to get the Karrie folder to move from the root to the sub folder id (I do have the correct ID but omitted it from this post). Maybe I'm doing this all backwards so any help would be appreciated.

// Create Karrie Folder In Root
function start() {

var sourceFolder = "Property Template";
var targetFolder = "Karrie"; 
var source = DriveApp.getFoldersByName(sourceFolder);
var target = DriveApp.createFolder(targetFolder);
if (source.hasNext()) {
  copyFolder(source.next(), target);}}

// Adds Property data to Karrie Folder
function copyFolder(source, target) {
var folders = source.getFolders();
var files   = source.getFiles();
while(files.hasNext()) {
  var file = files.next();
  file.makeCopy(file.getName(), target);}

// Adds Photo Folders to Karrie Folder
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
  copyFolder(subFolder, targetFolder);}} 


// Moves Karrie folder to propertie folder
function MoveFiles(){
var files = DriveApp.getFolderByName("Karrie");
   var destination = DriveApp.getFolderById("ID NA");
   destination.addFolder(files);}

Debugger

Upvotes: 2

Views: 2764

Answers (2)

Tanaike
Tanaike

Reputation: 201683

I understood that MoveFiles() in your script doesn't work. If my understanding is correct, how about this modification?

Modification points :

  • In order to move a folder, it required to move the parent of the folder.
  • But when addFolder() is used to the folder, the folder has 2 parents.
    • So original parent has to be removed.
  • There is no getFolderByName(). Please use getFoldersByName().

Modified script :

function MoveFiles(){
  var folder = DriveApp.getFoldersByName("Karrie").next();
  var destination = DriveApp.getFolderById("ID NA");
  destination.addFolder(folder);
  folder.getParents().next().removeFolder(folder);
}

Note :

  • In this script, The folder and parent of "Karrie" supposes only one in the Google Drive, respectively.

References :

If I misunderstand your question, please tell me. I would like to modify it.

Edit 1 :

When you run this function, does the ID of Logger.log(r) show the folderId of "Karrie"?

function sample() {
  var r = DriveApp.getFoldersByName("Karrie").next().getId();
  Logger.log(r)
}

Edit 2 :

Please confirm and consider the following points.

  1. Confirm the folder name and the existence of the folder again.
  2. Run sample() and confirm folder ID of "Karrie".
  3. Confirm whether there are no functions with the same name.
  4. Can I ask you the folder ID of "Karrie"? If you cannot retrieve the folder ID using DriveApp.getFoldersByName("Karrie").next().getId(), I propose to use directly the folder ID of "Karrie".

Updated on March 30, 2022:

In the current stage, the files and folders can be moved using moveTo. Ref This method has added on July 27, 2020. When moveTo is used, the above script can be modified as follows.

function MoveFiles(){
  var folder = DriveApp.getFoldersByName("Karrie").next();
  var destination = DriveApp.getFolderById("ID NA");
  folder.moveTo(destination);
}

Reference:

Upvotes: 3

Lyloox
Lyloox

Reputation: 1

As of 2020, the easier way I found to move a file is to use this function:

function moveToFolder(file, sourceFolder, destFolder) {
   var file = DriveApp.getFileById(file.getId());
   destFolder.addFile(file);
   sourceFolder.removeFile(file);
}

Because of the first line, you can convert your Spreadsheet, Google Form, Document or others Google objects into a File.

Then we just use methods of the Folder class : https://developers.google.com/apps-script/reference/drive/folder

Notes:

  • If you created your file by using .create() your sourceFolder will be DriveApp.getRootFolder().

  • If you have multiple files to move, just iterate over them.

Upvotes: 0

Related Questions