sth313
sth313

Reputation: 13

Google Apps Script: add folder to folder

I used a script to upload a folder with an image to a (parent?) folder. When I do it one time is does create a folder structure the right way. But, when I try it a second time, it creates a new folder in the root with the same name.

How can I change my code so that there is a parent folder where image folders will be added to?

Currently:

I want:
/root/persons/*(folders)/ (images inside this folder)

My code:

/* The script is deployed as a web app and renders the form */
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html')
        .setSandboxMode(HtmlService.SandboxMode.NATIVE);
// This is important as file upload fail in IFRAME Sandbox mode.
}

/* This function will process the submitted form */
function uploadFiles(form) {

try {

/* Name of the Drive folder where the files should be saved */
var dropbox = form.myName + " " + form.myEmail;
var folder, folders = DriveApp.getFoldersByName(dropbox);

/* Find the folder, create if the folder does not exist */
if (folders.hasNext()) {
  folder = folders.next();
} else {
  var firstLevelFolder = DriveApp.createFolder("Persons");
  var folder = firstLevelFolder.createFolder(dropbox);
}

/* Get the file uploaded though the form as a blob */
var blob = form.myFile;    
var file = folder.createFile(blob);    

/* Set the file description as the name of the uploader */
file.setDescription("Uploaded by " + form.myName);

/* Return the download URL of the file once its on Google Drive */
return "File uploaded successfully " + file.getUrl();

} catch (error) {

/* If there's an error, show the error message */
return error.toString();
}

}

Upvotes: 1

Views: 6178

Answers (2)

Daniel Michael Daube
Daniel Michael Daube

Reputation: 11

This worked for me to create a folder in an existing folder

const parentFolder = DriveApp.getFolderById("1TukDeifX_nYZftbzODkUfD-hYlpx5TPg");
const folder1 = parentFolder.createFolder(Project + ", " + Date + ", " + Phone);

Upvotes: 1

Mogsdad
Mogsdad

Reputation: 45720

You aren't testing for the existence of the "Persons" folder, so every time you have a new user you re-create that folder. I suggest doing that earlier in the function, like this:

/* Find the first level folder, create if the folder does not exist */
var firstLevelFolderName = "Persons";
var folders = DriveApp.getFoldersByName(firstLevelFolderName);
var firstLevelFolder = (folders.hasNext()) ? folders.next() : DriveApp.createFolder(firstLevelFolderName);

Instead of an explicit if..then..else structure, note the use of the ternary operator ? to keep the assignment in one statement. The same pattern can be used for any iterator being evaluated for "find vs create".

Updated script:

/* This function will process the submitted form */
function uploadFiles(form) {
  
  try {
    /* Find the first level folder, create if the folder does not exist */
    var firstLevelFolderName = "Persons";
    var folders = DriveApp.getFoldersByName(firstLevelFolderName);
    var firstLevelFolder = (folders.hasNext()) ? folders.next() : DriveApp.createFolder(firstLevelFolderName);
    
    /* Find the user-specific folder, create if the folder does not exist */
    var dropbox = form.myName + " " + form.myEmail;    /* Name of the Drive folder where the files should be saved */    
    folders = DriveApp.getFoldersByName(dropbox);
    var folder = (folders.hasNext()) ? folders.next() : firstLevelFolder.createFolder(dropbox); 
    
    /* Get the file uploaded though the form as a blob */
    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    
    /* Set the file description as the name of the uploader */
    file.setDescription("Uploaded by " + form.myName);
    
    /* Return the download URL of the file once its on Google Drive */
    return "File uploaded successfully " + file.getUrl();
    
  } catch (error) {
    
    /* If there's an error, show the error message */
    return error.toString();
  }
  
}

Upvotes: 2

Related Questions