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

168
Views
Find a file back in folder structure in multiple situations (Apps Script)

I need to find with Apps Script a specific Sheet back in folder structure of my actual Sheet (found filtered by title fragment for example). But sometimes is in the direct parent and another two or three folders behind, so I want the most versatile solution to work in different situations. Which will be the best and clean way?

[ FILE TO FIND ] [ FOLDER 1 ] [ ACTUAL FILE ]
                 [ FOLDER 2 ] [ ]

[ FILE TO FIND ] [ ACTUAL FILE ]

My code to find the file (just in the parent folder right now)

var carpetapanelc = carpetabasecs.getParents();
var carpetapanelcid = carpetapanelc.next().getId();
var carpetapanelcname = DriveApp.getFolderById(carpetapanelcid);
var searchFor ='title contains "Panel"';
var names =[];
var filePanelIds=[];
var filesPanel = carpetapanelcname.searchFiles(searchFor);
  while (filesPanel.hasNext()) {
var filePanel = filesPanel.next();
var filePanelId = filePanel.getId();// To get FileId of the file
filePanelIds.push(filePanelId);
var filePanelname = filePanel.getName();
var filePanelurl = filePanel.getUrl();
names.push(filePanelname);
}

Thanks!

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

0

Get Files in Parents That Include a String in their title

function getFilesInParentFolders(fileid="") {
    var file = DriveApp.getFileById(fileid)
    var fldrA = [];
    var filA = [];
    var folder = file.getParents();
    while (folder.hasNext()) {
      var f = folder.next();
      fldrA.push(f.getId());
      folder = f.getParents()
    }
    for(let i = 0;i<fldrA.length; i++) {
      let files = DriveApp.getFolderById(fldrA[i]).getFilesByType(MimeType.GOOGLE_SHEETS);
      while(files.hasNext()) {
        let file = files.next()
        if(file.getName().includes("Test")) {
          filA.push({name:file.getName(),id:file.getId(),parent:fldrA[i]});
        }
      }
    }
    
  Logger.log(JSON.stringify(filA));
}

This runs off of the folder id instead of a file id

function getFilesInParentFolders(folderid="") {
    var fldrA = [];
    var filA = [];
    var folder = DriveApp.getFolderById(folderid).getParents();
    while (folder.hasNext()) {
      var f = folder.next();
      fldrA.push(f.getId());
      folder = f.getParents()
    }
    for(let i = 0;i<fldrA.length; i++) {
      let files = DriveApp.getFolderById(fldrA[i]).getFilesByType(MimeType.GOOGLE_SHEETS);
      while(files.hasNext()) {
        let file = files.next()
        if(file.getName().includes("Test")) {
          filA.push({name:file.getName(),id:file.getId(),parent:fldrA[i]});
        }
      }
    }
    
  Logger.log(JSON.stringify(filA));
}
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!