How to remove all files from a folder?

Asked

Viewed 606 times

2

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

The problem is I don’t know the name of the files.

Here is an example of code to remove files (I need to remove FILES from which I don’t know the name of a folder)

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
              });
    });
});

2 answers

2


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

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

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

Basic methods regarding file in Apache Cordova:

  • removeRecursively: Delete a directory and all its contents.
  • getMetadata: Find the metadata of a directory.
  • setMetadata: Set metadata in a directory.
  • moveTo: Move a directory to a different location in the file system
  • copyTo: Copy a directory to a different location in the file system
  • toURL: Return a URL that can be used to locate a directory.
  • remove: Delete a directory. Directory must be empty.
  • getParent: Find the parent directory
  • createReader: Create a new DirectoryReader that can read entries from a directory.
  • getDirectory: Create or search a directory.
  • getFile: Create or search a file.

See more details in the documentation.

  • There’s no such thing as $cordovaFile in my app.

  • @Paulohdsousa In your case instead of being dir.getFile would be dir.removeRecursively.

  • I solved it a little differently, removeRecursively didn’t work, but I did something similar, follow.

0

I solved it in a similar way removeRecursively

I got 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 () {
                                });
                            });
                        });
                    }
                }
            }
  • In fact the removeRecursively() does what you did but recursively, which in theory is much more efficient.

  • @Acklay the problem is that I save the files at the root of Storage, and would end up deleting others that are not part of the app

  • The correct one then, would be you create a specific directory and put your files there not to happen this kind of problem. You have to be careful, because what is not work for you now, in the future can give a headache.

  • The problem is that it is already running this way, what I can do is save future files in a separate folder as you said.

  • It is up to you to improve the quality of your code to not have future problems (like this now). Anyway, good luck ae! =)

  • @Acklay I’ll do it then, thank you so much for the tips.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.