Alfred
Alfred

Reputation: 59

Recursive function doesn't return all data

I want to list only the folder names from a specific drive in Drive. So I first call the function getFolderNames() and add the parent folder name there. Then I call the getChildFolders(parent) function which gets all the sub-folders and keeps getting called recursively for every sub folder.

My problem is that the variable folderatt (which is supposed to hold all the information about the folder names) has only the names of the first folder, so only the sub-folders of the parent folder. But when I log inside the recursive function, it displays all the folder and sub-folder names correctly.

function getFolderNames() {
    var folderatt = [];

    try {

        // If you want a tree of any sub folder
        var parent = DriveApp.getFoldersByName("Librari").next();

        var dhenat = getChildFolders(parent);


        folderatt.push(dhenat);

    } catch (e) {

        Logger.log(e.toString());

    }
    return folderatt;

}


function getChildFolders(parent) {
    folderNames = [];

    var childFolders = parent.getFolders();
    var thirr = 1;
    while (childFolders.hasNext()) {


        var childFolder = childFolders.next();
        var allFolders = {};
        allFolders.emri = childFolder.getName();
        allFolders.id = childFolder.getId();

        folderNames.push(allFolders);
        thirr++;
        folderNames.push(thirr);
        // Logger.log(childFolder.getName());
        //    Logger.log(folderNames);


        // Recursive call for any sub-folders
        getChildFolders(childFolder);

    }
    return folderNames;

}

Upvotes: 0

Views: 124

Answers (2)

Piyush
Piyush

Reputation: 81

JSFiddle - Have a look at this fiddle.

Declare and initialize your 'folderNames' variable outside the recursive function. That should solve your problem. Also, you need not to return it to 'dhenat' variable in your parent fucntion as now 'folderNames' will hold the results

Upvotes: 1

Icepickle
Icepickle

Reputation: 12806

There are a couple of problems I can see with your code, but first and for most would be that you don't really do anything with the result of the getChildFolders( childFolder ) call, so naturally, it doesn't get included in the result set, now possibly you could change it to this

// Recursive call for any sub-folders
folderNames.concat( getChildFolders( childFolder ) );

But you should be aware, that currently your folderNames variable is a global variable (as it is not really defined, you just assign it a value). As long as it is global, even the concat won't help you, so you also have to assign it, eg:

let folderNames = [];

The reason that it won't help would be that each time you call the getChildFolders function, the folderNames would be set to an empty array again in your example, so folderNames would only contain the results of the last time you called getChildFolders( childFolder ). By defining it locally, there won't be such a side effect

So in the end, the first function can be rewritten as:

function getChildFolders(parent) {
    let folderNames = [];
    let childFolders = parent.getFolders();
    while (childFolders.hasNext()) {
        let childFolder = childFolders.next();
        let allFolders = {
          emri: childFolder.getName(),
          id: childFolder.getId()
        };
        folderNames.push(allFolders);
        // Recursive call for any sub-folders
        folderNames.concat( getChildFolders( childFolder ) );
    }
    return folderNames;
}

Please note that I removed the thirrr variable, as it was unclear to me for what purpose you need it.

In the end this should give you a flattened array that contains your allFolders object. I also changed the var to let keywords as var is a function scoped keyword, that doesn't make sense to be defined inside a block (in this case, your while loop)

Upvotes: 1

Related Questions