BALA MURUGAN
BALA MURUGAN

Reputation: 2063

How to mange file system of android mobile using Cordova

For reading the mobile file system, I have used following Cordova code. Its working fine

window.resolveLocalFileSystemURL(path,
    function (fileSystem) {
        var reader = fileSystem.createReader();
        reader.readEntries(
          function (entries) {
             ..

Additionally, I want to provide following functionalities using Cordova

• Creating new folder

• Delete file/ files

• Rename

• Get details

• Remove files/ folders

• Cut, copy and paste – single file and folder / multiple files and folders

• Search function(in depth search like windows explorer)

Could you please provide any suggestion to implement these functionalities/ code blocks?

Upvotes: 0

Views: 388

Answers (2)

Mahen
Mahen

Reputation: 302

As you mentioned window.resolveLocalFileSystemURL,So you are familiar with file plugin of cordova. These are the things you can achieve using file plugin:-
To create a folder :-

var root = cordova.file.externalDataDirectory;
window.resolveLocalFileSystemURL(root,  
function(directoryEntry) {  
directoryEntry.getDirectory('your_dir_name',{create:true},successCallBack,errorCallBack);  
},function(e){});  

To Remove file:-

var root = cordova.file.externalDataDirectory/file.txt;
window.resolveLocalFileSystemURL(root,  
function(file) {  
file.remove(successCallBack,errorCallBack);  
},function(e){}); 

To Copy/move :-

window.resolveLocalFileSystemURL('YOUR_FILE_PATH/1.txt', function(fs) {
 var pathToCopy = cordova.file.externalRootDirectory+"/";
 var newName = "NEW_FILE_NAME"; //After copy/Move
 window.resolveLocalFileSystemURL(pathToCopy,function(directoryEntry) {
               fs.copyTo(directoryEntry, newName, function() {
                 console.log("File Copied To:"+pathToCopy);
               }, failFiles);
           });
}, failFiles);

To rename to file you can use both paths same and change the file name.

Hope this helps

Upvotes: 1

Homen
Homen

Reputation: 1212

There are two entry in cordova file API

  • FileEntry
  • DirectoryEntry

To perform operation on file e.g-Delete file,Get details of file you have to go to the file FileEntry. Similarly, to perform operation on folder e.g-Delete folder you have to go to the DirectoryEntry. And after going to that entry there are specific methods that you can perform.I just giving example of creating a folder as follows:

fileSystem.root.getDirectory("FolderName", {create: true});

For other operation i refer you to this Documentation: Phonegap Documentation

Upvotes: 1

Related Questions