Siddharth
Siddharth

Reputation: 545

Recursively Read all files and folder structure using Cordova

IS there any way to read the files and folder structure recursively starting from root. The scope here is to scan all the files present in the directory along with there directory ,sub-directory using cordova

Upvotes: 1

Views: 2492

Answers (3)

Siddharth
Siddharth

Reputation: 545

have a look.This plugin can scan files based on filetype

https://github.com/siddmegadeth/Cordova-Media-Scanner

Upvotes: 0

siddharth chandra
siddharth chandra

Reputation: 1

scan : function(url,fileType,callback)
        {
             var fileTypeCollection = [];
             var defer = $q.defer();


                url.forEach(function(element, index) 
                {
                //requestLocalFileSystemURL
                log(element);
                window.resolveLocalFileSystemURL(element,onRequestFileSystem, fail);


                log("Ends resolve");
                });


            function onRequestFileSystem(fileSystem) 
            {
                var directoryReader = fileSystem.createReader();
                directoryReader.readEntries(onReadEntries,fail);
            } /*onRequestFile Ends*/

            function onReadEntries(entries) 
            {


                if(entries.length==0)
                {
                     log("Entries Length....Resolving");
                     defer.resolve(fileTypeCollection);
                }
                else
                {   
                    entries.forEach( function(element, index) 
                    {

                        if (element.isDirectory === true) 
                        {
                        // Recursive -- call back into this subdirectory

                         onRequestFileSystem(element);
                        } 

                        if(element.isFile == true) 
                        {

                                fileType.forEach(function(type)
                                {
                                    if(element.name.indexOf(type) != -1)
                                    {
                                        fileTypeCollection.push(element);
                                    }
                                }); 
                        } /*is File ENds*/
                    });  /*Entries For Each Ends*/
                }   

            }  /*OnRead Ends*/

            function fail(resp)
            {
                log(resp);
                defer.reject();
            }  /*Fail Ends*/

        return defer.promise;

    }  //Scan Function Ends

Upvotes: 0

eb1
eb1

Reputation: 2947

Take a look at the answer here: https://stackoverflow.com/a/29905718/346550. This is using the cordova file plugin.

Upvotes: 2

Related Questions