Implement/adapt recursiveGetFiles into aeq.file

Issue #34 resolved
Zack Lovatt
created an issue

Something like this? Or as a flag to the existing aeq.file.getFiles() function

    /**
     * Recursively scan folder for all files matching extension
     *
     * @param {Folder} folder Root folder to serach in
     * @param {string} extension File extension to search
     * @returns {File[]} Array of found files
     */
    function recursiveGetFiles (folder, extension) {
        var foundItems = aeq.arrayEx();

        var folderObject = aeq.file.getFolder(folder);
        if (aeq.isNullOrUndefined(folderObject))
            return foundItems;

        var items = aeq.file.getFiles(folderObject);
        if (aeq.isNullOrUndefined(items))
            return foundItems;

        items.forEach(function (item) {
            if (aeq.isFolder(item)) {
                foundItems = foundItems.concat(recursiveGetFiles(item, extension));
            } else if (aeq.file.getExtension(item) === extension){
                foundItems.push(item);
            }
        });

        return foundItems;
    }

Comments (3)

  1. Zack Lovatt reporter

    Here's a version w/ filter--

        /**
         * Recursively scan folder for all files matching filter
         *
         * @param {Folder|String}   folder - Folder or path to get files from
         * @param {String|Function} filter - Filter string or function
         * @returns {File[]}               - Array of found files
         */
        function recursiveGetFiles (folder, filter) {
            var foundItems = aeq.arrayEx();
    
            var folderObject = aeq.file.getFolder(folder);
            if (aeq.isNullOrUndefined(folderObject))
                return foundItems;
    
            // Find all folders within folderObject, and recursively append anything it finds to foundItems
            aeq.file.getFiles(folderObject).filter(function (item) {
                return aeq.isFolder(item);
            }).forEach(function (folderItem) {
                foundItems = foundItems.concat(recursiveGetFiles(folderItem, filter));
            });
    
            var filesInFolder = aeq.file.getFiles(folderObject, filter);
            if (!aeq.isNullOrUndefined(filesInFolder))
                foundItems = foundItems.concat(filesInFolder);
    
            return foundItems;
        }
    
  2. Log in to comment