How to remove all files from a folder?

2

I am trying to remove all the files from a folder using Cordova and Android, the problem is that all code I think, only shows how to remove FILES and not FILES FROM A FOLDER.

The problem is that I do not know the name of the files.

Here is a sample code to remove files (I need to remove FILES I do not know the name of a folder for)

var path =  cordova.file.applicationStorageDirectory;
var filename = "arquivoABC.txt";

window.resolveLocalFileSystemURL(path, function(dir) {
    dir.getFile(filename, {create:false}, function(fileEntry) {
              fileEntry.remove(function(){
                  // The file has been removed succesfully
              },function(error){
                  // Error deleting the file
              },function(){
                 // The file doesn't exist
              });
    });
});
    
asked by anonymous 15.03.2017 / 13:16

2 answers

2

You should use the removeRecursively() method. For example:

$cordovaFile.removeRecursively('/storage/sdcard/meudiretorio', "")
  .then(function (success) {
    // apagou com sucesso

}, function (error) {
    // erro ao apagar arquivos
});

Basic methods for archiving in Apache Cordova:

  • removeRecursively : Delete a directory and all its contents.
  • getMetadata : Search the metadata for a directory.
  • setMetadata : Define metadata in a directory.
  • moveTo : Moving a directory to a different location on the file system
  • copyTo : Copying a directory to a different location on the file system
  • toURL : Return a URL that can be used to find a directory.
  • remove : Delete a directory. The directory must be empty.
  • getParent : Searching the parent directory
  • createReader : Create a new DirectoryReader that can read entries from a directory.
  • getDirectory : Creating or searching a directory.
  • getFile : Create or browse a file.

See more details in the documentation .

    
15.03.2017 / 13:34
0

Resolved in a similar way to removeRecursively

I had the idea thanks to my friend Ack Lay, thank you.

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystem, fail);

            function onRequestFileSystem(fileSystem) {
                var directoryReader = fileSystem.root.createReader();
                directoryReader.readEntries(onReadEntries, fail);
            }

            function onReadEntries(entries) {
                var i;
                for (i = 0; i < entries.length; i++) {
                    if (entries[i].name.indexOf("pacoteapp_v") !== -1) {
                        window.resolveLocalFileSystemURL(entries[i].nativeURL, function (dir) {
                            dir.getFile(entries[i].name, { create: false }, function (fileEntry) {
                                fileEntry.remove(function () {
                                }, function (error) {
                                }, function () {
                                });
                            });
                        });
                    }
                }
            }
    
15.03.2017 / 14:02