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

前端 未结 1 762
南方客
南方客 2021-01-26 09:57

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 fil

相关标签:
1条回答
  • 2021-01-26 10:49

    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--;
    }
    
    0 讨论(0)
提交回复
热议问题