Upload photo in a child folder with apps script

the post which allowed me to save photos allows it in a directory of the root of my google drive account but I would like to save photos in a child directory of which I provide the name in parameter
my research was unsuccessful I do not control at all the files, the id of files etc ...

All you need is the folder ID for the folder you want to save in, but your upload google apps script must accept the folder ID as a parameter.

Show your script and relevant blocks


i send the name of the folder as parameter of my web app my concern is to recover the id of a folder contained in a folder there are so many methods some of which are obsolete we are happy to find solutions but often they are no longer up to date
these lines allow to create my file in the parent folder of which I know the id

 var folder = DriveApp.getFolderById('17rDnVVLt2Wb19gq8Ceteee8WGfg')


So I understand, if you have a folder structure like this:


You know the folder ID of Folder1, but would like to know the folder IDs of Folders A,B & C ?

it's all done that anyway I'm struggling with a while loop

I just needed a yes or no?

I just needed a yes or no?

Here is a google apps script to return the folders within a folder:

function getListOfFoldersInFolder(targetID) {
  var parentFolder = DriveApp.getFolderById(targetID);
  var folders = parentFolder.getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
    folderList += folder.getName()+ "|" + folder.getId() + ",";
  var output = folderList.replace(/,\s*$/, "");  //removes last comma
  return ContentService
  .createTextOutput( output );


You may need to add the Drive api to your script project: Clicik on Services and scroll to Drive and switch it on.

from my side i tried this and i doesn't do anything ;

  var asus= e.parameters.asus ;
  var Folder = DriveApp.getFoldersByName("Profolder"); 
  var folders = Folder.next().getFolders(); 
  while (folders.hasNext()) 
  var folder = folders.next();
  if(folder.getName() === asus
  var lId = folder.getId();      


Two things:

  1. My script solution uses folder IDs. I don't know if the same works for folder names.
  2. You have a ) missing in your script:

here is the whole script that i found in a post

function doGet(e) {
return message("Error: no parameters in doGet");


function doPost(e) {
if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) {
return message("Error: Bad parameters in doPost");
} else {
var imgf = e.parameters.imageformat[0].toUpperCase();
var mime =
(imgf == 'BMP') ? MimeType.BMP
: (imgf == 'GIF') ? MimeType.GIF
: (imgf == 'JPEG') ? MimeType.JPEG
: (imgf == 'JPG') ? MimeType.JPEG
: (imgf == 'PNG') ? MimeType.PNG
: (imgf == 'SVG') ? MimeType.SVG
: false;
if (mime) {
var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
var blob = Utilities.newBlob(data, mime, e.parameters.filename);

 //var folder = DriveApp.getFolderById('17rDnVVLt2Wb19gq8CWkF')
  var projet= e.parameters.projet ;

  var Folder = DriveApp.getFoldersByName("asus"); 
  var folders = Folder.next().getFolders(); 
  while (folders.hasNext()) 
  var folder = folders.next();

  if(folder.getName() === projet)
   var lId = folder.getId();
   var file= DriveApp.getFolderById(lId).createFile(blob);

} } }

return }

originally this script allowed me to create files in the parent folder

I'll test the folder finding part when I get a moment.

Hopefully, a solution for you. Running scripts using folder names creates its own problems; folder does not exist with the chosen name, multiple folders with the same name, but this should resolve it for you:

Assumes folder setup like this and only one folder called Folder1 in the root of your google drive! :

Google Drive
    -Folder1       <<< your "base folder"

Script ( I used doGet() so that I could test in a browser). In the script 'Folder1' and 'FolderB' would be the parameters you send to the script.

function doGet() {

//first, get all the folders in the root of google drive
  var folders = DriveApp.getRootFolder().getFolders();
  while (folders.hasNext()) {
    var folder = folders.next();
//once you have these folders, test if your "baseFolder" is present
//and get the folderID for your folder
    if (folder.getName() == 'Folder1') {
      var baseFolder = folder.getId();
//now iterate over the folders in your baseFolder
      var parentFolder = DriveApp.getFolderById(baseFolder);
      var bfolders = parentFolder.getFolders();
      while (bfolders.hasNext()) {
        var bfolder = bfolders.next();
//test if the subFolder you want is present
//and then do what you want with that subFolder
//in this case, it returns the name and id of the folder as a string
        if (bfolder.getName() == 'FolderB') {
          var saveFolder = bfolder.getName() + " | " + bfolder.getId();
//ensure you have a return
      return ContentService.createTextOutput(saveFolder);

on my system this script returns:

FolderB | 1r17WAzH_4OotqhucCkAGM4xdO5ztXuNd

it's all good Tim!
as I know the parent folder I started from its id for the rest I had to return to a do get (e) ,
do post (e) and a last function to "save" the photo in the target folder by browsing the list of folders contained in the parent folder
there may be faster in any case it works
a big thank-you


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.