Reputation: 21
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);}
Upvotes: 2
Views: 2764
Reputation: 201683
I understood that MoveFiles()
in your script doesn't work. If my understanding is correct, how about this modification?
addFolder()
is used to the folder, the folder has 2 parents.
getFolderByName()
. Please use getFoldersByName()
.function MoveFiles(){
var folder = DriveApp.getFoldersByName("Karrie").next();
var destination = DriveApp.getFolderById("ID NA");
destination.addFolder(folder);
folder.getParents().next().removeFolder(folder);
}
"Karrie"
supposes only one in the Google Drive, respectively.If I misunderstand your question, please tell me. I would like to modify it.
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)
}
Please confirm and consider the following points.
sample()
and confirm folder ID of "Karrie".DriveApp.getFoldersByName("Karrie").next().getId()
, I propose to use directly the folder ID of "Karrie".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);
}
Upvotes: 3
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