Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Subfolder Search and Stop (Google Apps Script / Javascript)

I've found some code here that almost does what I need, but I'm trying to break the recursive function when I locate the folder in question. Currently, my console.log returns "undefined."

This is probably something easy; I just can't see it.

Here's my (mostly stolen) code:

  function moveFile(e) {
  var FileNo = e.parameters.FileNo;
  var destFolder = e.formInput.input_field;

  // Get the Folder
  var Folders = DriveApp.searchFolders("title contains '"+FileNo+"' and trashed = false and hidden = false");
  var Folder = Folders.next().getId();

  var url = start(Folder, destFolder);

  console.log(url);
  
}

function start(FolderID, destFolder) {
 folder = DriveApp.getFolderById(FolderID);
 var response = listFolders(folder, destFolder);
 //console.log(response);
 return response;
}

function listFolders(folder, destFolder) {
  //starting point, we come here from start() or from the bottom of this function
  var destFolder = destFolder;
  //check the name
  var name = folder.getName();

  if (name.match(destFolder)) {
    //console.log(folder.getUrl());
    var response = folder.getUrl();
    return response; 
  }

  //now see if this folder has subfolders
  var subfolders = folder.getFolders();

  //if it has, we call this function again
  while (subfolders.hasNext()) {    
    listFolders(subfolders.next(), destFolder);
  }
  
}

Thank you to whomever feels generous enough to save this tired parent from himself.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check if a response has been returned:

while (subfolders.hasNext()) {    
  var tempRes = listFolders(subfolders.next(), destFolder);
  // if it returned a value, return the value
  if(tempRes)
    return tempRes;
}

Searching for test9 Folder:

  • Original script output:

orig

  • Applied change:

output

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!