Languages

Menu
Sites
Language
File.listFiles filtering on file extension

I am trying to list the files with a specific extension (i.e. *.gpx), so I thought I would do

window.tizen.filesystem.resolve('documents', function (fs) {
  fs.listFiles(function (files) {
    for (var i in files) { console.log(files[i].fullPath); }; },
  null, { name: '%.gpx' });
});

However, that doesn't return any results (and, of course, there are some gpx files there); using a slightly different filter seems to work as expected:

window.tizen.filesystem.resolve('documents', function (fs) {
  fs.listFiles(function (files) {
    for (var i in files) { console.log(files[i].fullPath); }; },
  null, { name: 'A%.gpx' });
});

But that doesn't really do what I want... How can I get all files with a specific extension?

BTW, I am using the mobile 3.0 emulator...

Responses

3 Replies
Iqbal Hossain

hi, 

You can do it very easily... follow the below code..just change your extension

 var documentsDir;
    tizen.filesystem.resolve(
	    	  'images',
	    	  function(dir) {
	    	  documentsDir = dir;
	    	  dir.listFiles(onsuccess,onerror);
	    	  }, function(e) {
	    	  console.log("Error" + e.message);
	    	  }, "rw"
	    	 );

   function onsuccess(files) {
           for (var i = 0; i < files.length; i++) {
    	     if (files[i].isDirectory == false){
    	    	 var fields = files[i].fullPath.split('.');
    	    	 var extType=fields[1];
    	    	 if(extType === 'pdf'){
    	    		 console.log(files[i].fullPath);
    	    	 }
    	     }
    	   }
    	 }

    function onerror(error) {
    	 console.log("The error " + error.message + " occurred when listing the files in the selected folder");
    }

Also add these privileges in cofig

<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
<tizen:privilege name="http://tizen.org/privilege/filesystem.write"/>

 

Christof Meerwald

Well... yes... I am aware that you can work around this by doing the filtering myself. But my understanding from the documentation is that it should be possible to let the API do the filtering. The question really is, am I not using the API correctly or is it a bug?

Iqbal Hossain

May be not in API. I have searched... you can follow the upper workaround.