In Google Apps Script, how would I get all subfolders and all subsubfolders and all subsubsub folders etc.?

I am attempting to rename every file in a specified folder and all files in all subfolders but I cannot figure out how to do so. I so far have this script that renames every file in a folder given its Id, but I need to do all subfolders.

function renameFilesInFolder(folderID) {
  var folder = DriveApp.getFolderById(folderID)
  var files = folder.getFiles(),
      fileNames = []
  

  while(files.hasNext()){
    var file = files.next()
    var oldFileName = file.getName()
    var checkIfMatch = oldFileName.substring(oldFileName.length - 3);
    var newFileName = ""
    if (checkIfMatch=="OBS"){
      newFileName = oldFileName
    } else {
      newFileName =oldFileName+"_OBS"
    }
    file.setName(newFileName)
    Logger.log(newFileName)
  }
}
Thank you!

Solution 1:

This will recurse through the folder writing them into a spreadsheet.

var level=0;
function getFnF(folder) {
  var folder= folder || DriveApp.getRootFolder();
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('FilesAndFolders')
  var files=folder.getFiles();
  while(files.hasNext()) {
    var file=files.next();
    var firg=sh.getRange(sh.getLastRow() + 1,level + 1);
    firg.setValue(Utilities.formatString('File: %s', file.getName()));
  }
  var subfolders=folder.getFolders() 
  while(subfolders.hasNext()) {
    var subfolder=subfolders.next();
    var forg=sh.getRange(sh.getLastRow() + 1,level + 1);
    forg.setValue(Utilities.formatString('Fldr: %s', subfolder.getName()));
    level++;
    getFnF(subfolder);
  }
  level--;
}

This does is a little faster

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--;
}

function getFilesAndFolders1() {
  const fldr = DriveApp.getFolderById(gobj.globals.projectsfolderid);
  const ss = SpreadsheetApp.getActive();
  ss.toast("Entry");
  const sh = ss.getSheetByName('Sheet1');
  sh.clearContents();
  SpreadsheetApp.flush();
  PropertiesService.getScriptProperties().setProperty('FnF', JSON.stringify({ txt: [], fwt: [], level: 0 }));
  getFnF1(fldr);
  //Logger.log(PropertiesService.getScriptProperties().getProperty('FnF'));
  let tree = JSON.parse(PropertiesService.getScriptProperties().getProperty('FnF'));
  const l = tree.level + 1
  tree.txt.forEach(r => {
    if (r.length < l) {
      //Array.from(Array(l - r.length).keys()).forEach(e => r.push(''));
      r.splice(r.length,0,...Array(l-r.length).fill(''));
    }
  });
  tree.fwt.forEach(r => {
    if (r.length < l) {
      //Array.from(Array(l - r.length).keys()).forEach(e => r.push('normal'));
      r.splice(r.length,0,...Array(l-r.length).fill('normal'));
    }
  });
  //Logger.log(JSON.stringify(tree));
  sh.getRange(1, 1, tree.txt.length, tree.level + 1).setValues(tree.txt);
  sh.getRange(1, 1, tree.fwt.length, tree.level + 1).setFontWeights(tree.fwt);
  PropertiesService.getScriptProperties().deleteProperty('FnF');
  ss.toast("EOF");
}