0

I'm trying to find a file which may be in a subfolder, but it could also be inside a subfolder of a subfolder. I've followed this answer's approach, but still...

When I place the file inside the 'second subfolder, the script is not finding it.

function updateClientWb() {
  var clientId = 'AA001'
  var parent_folderID = 'folderidsafafdasadasda2D-U';

  var parent_folder = DriveApp.getFolderById(parent_folderID);
  var subFolders = parent_folder.getFolders();
  var searchForClientId = "title contains '" + clientId + "'";
  var clientSeoProjectWb = [];

  while (subFolders.hasNext()) {
    var thisSubFolder = subFolders.next();//THIS IS PROBABLY WHERE THE FLAW IS
    var clientFiles = thisSubFolder.searchFiles(searchForClientId);
    while (clientFiles.hasNext()) {
      var files = clientFiles.next();
      if (files.getName().indexOf('File Name Part') > -1) {
        clientSeoProjectWb.push(files.getId());
      }
    }
  }
  return;
}

Appreciate your help

onit
  • 2,275
  • 11
  • 25
  • shouldn't `thisSubFolder` be `var` instead of `const`? on the line you think is the issue? – pgSystemTester May 22 '22 at 21:38
  • Hi, @pgSystemTester! Just changed it all and tested it. Still gets the file when it's in the subfolder, but not in a subfolder's subfolder... Thanks for your input! – onit May 22 '22 at 22:01
  • 1
    You need to have the function call itself running through the folder. I'll try to post an example. – pgSystemTester May 22 '22 at 22:08

2 Answers2

1

Try something like this. Note that the clientSeoProjectWB is defined in the script file, not a function so both functions can interact with it. I didn't test it, but I think this would put you on the proper track. Make sure your first folder is identified in the startSearch function.

//need to have shared array
var clientSeoProjectWb =[];

function startSearch(){
  searchFolder_(DriveApp.getFolderById('???topFolderID'))
  //Final array with all files...
  clientSeoProjectWb.forEach(x => Logger.log(x));
}

function searchaFolder_(_aFolder) {
  var someFiles = _aFolder.getFiles();

  while(someFiles.hasNext()){
    var aFile = someFiles.next();
    if(aFile.getName().indexOf('File Name Part') > -1){
      //Found file add to array
      clientSeoProjectWb.push(aFile.getId());
    }
  }
  var someFolders = _aFolder.getFolders();
    while(someFolders.hasNext()){
    var newFolder = someFolders.next();
    //launch function again with new folder
    searchaFolder_(newFolder);
  }
}
pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
  • 1
    I'll have to include the ```searchFiles()``` so that I get the the ones related to an ID I'll be passing, but this does iterate through all subfolders and I thank you very much for that! Minor typo - Correction needed in line 5: ```searchaFolder...``` Thanks a lot! – onit May 22 '22 at 22:27
  • Hi! The script is supposed to find only one file with that name. So would a ```break;``` right after ```clientSeoProjectWb.push(aFile.getId());``` help the script finish executing sooner? Thank you! – onit May 25 '22 at 18:44
1

Files in folders

function getFilesAndFolders() {
  const fldr = DriveApp.getFolderById(gobj.globals.projectsfolderid);
  SpreadsheetApp.getActive().getSheetByName('Sheet0').clearContents();
  SpreadsheetApp.getActive().toast('Entry');
  getFnF(fldr);
  SpreadsheetApp.getActive().toast('EOF');
}

function getFnF1(folder = DriveApp.getRootFolder()) {
  let tree = JSON.parse(PropertiesService.getScriptProperties().getProperty('FnF'));
  //Logger.log(JSON.stringify(tree));
  if (tree.level < level) {
    tree.level = level;
    PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
  }
  const files = folder.getFiles();
  let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = folder.getName(); } else { x = ''; } return x; }));
  tree.txt.push(row);
  row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'bold'; } else { x = 'normal'; } return x; }));
  tree.fwt.push(row);
  PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
  if (files.hasNext()) {
    let row = Array.from([...Array(level).keys()], ((x, i) => { if (i == level - 1) { x = 'Files:'; } else { x = ''; } return x; }));
    tree.txt.push(row);
    tree.fwt.push(['normal']);
    PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
  }
  while (files.hasNext()) {
    let file = files.next();
    let row = Array.from([...Array(level + 1).keys()], ((x, i) => { if (i == level) { x = file.getName(); } else { x = ''; } return x; }));
    tree.txt.push(row);
    tree.fwt.push(['normal']);
    PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify(tree));
  }
  const subfolders = folder.getFolders()
  while (subfolders.hasNext()) {
    let subfolder = subfolders.next();
    level++;
    getFnF1(subfolder);
  }
  level--;
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Suppose this one is faster as the number of folder and files increases... Thank you! – onit May 23 '22 at 01:49