-1

I made this script to list all subfolders of a given folder, so later I can look up values in another worsheet. But the script is not picking up all the folders. Why's that

function ListaPasta() {
  
  var foldername= 'Main folder name';
  var folderlisting = 'folders';
  
  var mainfolder = DriveApp.getFoldersByName(foldername).next();
  var folders = mainfolder.getFolders();
  
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['Nome'] );
  while(folders.hasNext()) {
    var folder = folders.next();
    var nome = folder.getName();
    sheet.appendRow([nome]);
   
    
  };
}
  

Edit: I don't want the subfolders of the folders inside the main folder, so I don't need recursion.

The script is not listing all folders and I'm sure of it, because when I look for some folder in the listing (via LOOK functions and using the filter) I can't find it, but if I search in the drive I find it inside the main folder(not as a subfolder of other folders).

Now since the time I posted this question I noticed that when I opened some folders looking for a file it would show the folder as empty, but if I searched for the file name it would find it. After I opened the file and closed it, and then went to the folder and opened it the file would be there. Could be a similar behaviour causing the script to not find some folders?

Not sure if relevant, but the main folder is part of a shared drive.

  • 1
    You need to use recursion – Cooper Dec 09 '20 at 16:57
  • 2
    the code works for me. So either you want something else or you don't know what the code does. Essentially, you generate a new spreadsheet file named `folders` where you put all the data there. If you expect to get the data to a bound spreadsheet then you should replace: `var ss = SpreadsheetApp.create(folderlisting);` with `var ss = SpreadsheetApp.getActive();` – Marios Dec 09 '20 at 17:40
  • Do you want to list just immediate children, or all descendants of a folder? If it's the former, your code should work; if it's the latter, I think the answer provided by Tanaike will be useful to you. – Iamblichus Dec 10 '20 at 08:19
  • I don't need recursion, I don't want subfolders. I just want to list the folders of the main folder. But I know for sure that is not listing some folders, because i've looked for a folder in the list given by the script and the folder wasn't there, but if I got the main folder it is theere. – GuilhermeAraujo Dec 10 '20 at 17:13

1 Answers1

2

I believe your goal as follows.

  • You want to retrieve the folder names of all subfolders in the specific folder using Google Apps Script.
  • You want to put the retrieved folder names to Google Spreadsheet.
    • From your script, you want to put each folder name to each row of the 1st tab of the created new Spreadsheet.

Modification points:

  • In your script, the folders in the specific folder are retrieved. In this case using getFolders(), only the folders just under the specific folder are retrieved. I think that this is the reason of your issue. When you want to retrieve all subfolders in the specific folder, as one of several methods Ref, how about recursively traverse all subfolders using the recursive function?
  • In your script, appendRow is used in a loop. In this case, the process cost will be high.

When above points are reflected to your script, it becomes as follows.

Modified script:

function ListaPasta() {
  const foldername= 'Main folder name';  // Please set the folder name of the specific folder.
  const folderlisting = 'folders';  // Please set the Spreadsheet name.


  // 1. Retrieve the folder names including all subfolders.
  const getAllSubfolders = (f, folders = [foldername]) => {
    const fols = f.getFolders();
    const temp = [];
    while (fols.hasNext()) {
      const fol = fols.next();
      temp.push({name: fol.getName(), folder: fol});
    }
    if (temp.length > 0) {
      folders.push(temp.map(({name}) => name));
      temp.forEach(({folder}) => getAllSubfolders(folder, folders));
    }
    return folders.flat().map(a => [a]);
  }
  const mainfolder = DriveApp.getFoldersByName(foldername);
  if (!mainfolder.hasNext()) throw new Error("No folder.");
  const folderNames = getAllSubfolders(mainfolder.next());

  // 2. Create new Spreadsheet and put the retrieved folder names to the Spreadsheet.
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getSheets()[0];
  sheet.getRange(1, 1, folderNames.length, 1).setValues(folderNames);
}
  • In this modified script, the top row is the folder name of the specific folder. If you don't want to include it, please modify folders = [foldername] to folders = [].
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for taking the time to answer and even optimze the code. I don't want the subfolders of the folders inside the main folder, so I don't need recursion. The script is not listing all folders and I'm sure of it, because when I look for some folder in the listing (via LOOK functions and using the filter) I can't find it, but if I search in the drive I find it inside the main folder(not as a subfolder of other folders). – GuilhermeAraujo Dec 10 '20 at 17:55
  • Now since the time I posted this question I noticed that when I opened some folders looking for a file it would show the folder as empty, but if I searched for the file name it would find it. After I opened the file and closed it, and then went to the folder and opened it the file would be there. Could be a similar behaviour causing the script to not find some folders? Not sure if relevant, but the main folder is part of a shared drive. – GuilhermeAraujo Dec 10 '20 at 17:56
  • @GuilhermeAraujo Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about your replying. Can I ask you about the detail of the current issue? By this, I would like to try to understand it. – Tanaike Dec 11 '20 at 00:08