I have a directory that contains a lot of files. I want to delete the entire directory as well as all the files in it.
I want my code to wait until every File in that directory (including the directory itself) is deleted before the next command is executed.
How do i wait? My code is
public void wipeMemoryCard()
{
File deleteMatchingFile = new File(Environment
.getExternalStorageDirectory().toString());
try {
filenames = deleteMatchingFile.listFiles();
if (filenames != null && filenames.length > 0)
{
content = true;
for (File tempFile : filenames)
{
if (tempFile.isDirectory())
{
wipeDirectory(tempFile.toString());
tempFile.delete();
}
else
{
File file = new File(tempFile.getAbsolutePath());
file.delete();
}
}
}
else
{
deleteMatchingFile.delete();
Toast("No files to Delete");
}
}
catch (Exception e)
{
e.printStackTrace();
}
if(content == true)
{
if (filenames == null && filenames.length == 0)
{
Toast("Files Deleted");
}
}
}
private static void wipeDirectory(String name) {
File directoryFile = new File(name);
File[] filenames = directoryFile.listFiles();
if (filenames != null && filenames.length > 0)
{
for (File tempFile : filenames)
{
if (tempFile.isDirectory())
{
wipeDirectory(tempFile.toString());
tempFile.delete();
}
else
{
File file = new File(tempFile.getAbsolutePath());
file.delete();
}
}
} else
{
directoryFile.delete();
}
}