Found a nice code to delete a directory including subdirs but is it possible to implement that a (sub)directory which for example exists (.data) is not being deleted?
Thanks for the help.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
try this
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children[i].equals("data")||children[i].equals(".data"))
continue;
else
if (!deleteDir(new File(dir, children[i])))
return false;
}
}
return dir.delete();
}
Maybe you can try this:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
if(children.equals("data")){
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
}
return dir.delete();
}
Related
I have a content menu where it is pop up a menu of rename and delete when you press the item in few seconds. But i dont know how to get the correct directory of one file. Here is my code:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.rename:
// edit stuff here
return true;
case R.id.delete:
File dir = new File(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
Use below util function to either delete File or Directory.
public static boolean delete(File path) {
boolean result = true;
if (path.exists()) {
if (path.isDirectory()) {
for (File child : path.listFiles()) {
result &= delete(child);
}
result &= path.delete(); // Delete empty directory.
} else if (path.isFile()) {
result &= path.delete();
}
return result;
} else {
return false;
}
}
Usage:
File dir = new File(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
delete(dir);
use below function to delete file from Folder, just pass folder path in perameter like
File fDir = new File(PERENT_PATH);
DeleteRecursive(fDir);
// where PERENT_PATH = Environment.getExternalStorageDirectory()+"/folderName"
public static void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
DeleteRecursive(child);
if (fileOrDirectory.exists()) {
boolean b = fileOrDirectory.delete();
if (b) {
Log.e( "delete dir", "delete dir");
} else {
Log.e( "not delete dir", "not delete dir");
}
}
}
Please check the Below code
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
.........
case R.id.delete:
deleteDirectory(Environment.getExternalStorageDirectory()+"/Music/MusicPlayer");
.........
}
}
static public boolean deleteDirectory(File path) {
if( path.exists() ) {
File[] files = path.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
return( path.delete() );
}
Instead of use String[] children = dir.list() try to use:
File dir = new File(PATH);
File[] children = dir.listFiles();
for(int i = 0; i < children.length; i++) {
children[i].delete();
}
I am working on app and saved data in sharedpreference and cache, I want clear only cache of application not sharedpreference data. How can I do it? It's my code
public void clearApplicationData(Context context)
{
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
} public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
System.out.println("Directory not null");
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
when i run application it's clear all the data of local memory
That is because of this line:
File appDir = new File(cache.getParent());
Get rid of it. Get rid of every reference to appDir.
If you want to delete getCacheDir(), then delete getCacheDir(), not other things, like the getParent() of getCacheDir().
Delete the Cache Directory
public static void deleteCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
you need permissions
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
I have implemented offline caching in my app and for that I am storing images in external storage.I want that once my cached images folder limit reaches 30 then it starts replacing the older images with new ones.For that i implemented following deletion algorithm-
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
if(children.length>30)
{
int exceed=children.length-30;
for (int i = 0; i <exceed; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
else
{
Log.e("deleted","file deleted");
}
}
}
}
return dir.delete();
}
But the above algorithm doesn't work as expected.It might deletes the newly added images.I also tried implementing below algorithm.But it also not working as expected.I failed to understand where I am going wrong.
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory())
{
String[] children = dir.list();
if(children.length>30)
{
int exceed=children.length-30;
int destroy=(children.length-exceed)-1;
for (int i = children.length; i >destroy; i--)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
else
{
Log.e("deleted","file deleted");
}
}
}
}
return dir.delete();
}
Try this
public static void deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files.length > 30) {
Arrays.sort(files, new Comparator<File>() {
#Override
public int compare(File o1, File o2) {
if (o1.lastModified() > o2.lastModified()) {
return 1;
} else if (o1.lastModified() < o2.lastModified()) {
return -1;
}
return 0;
}
});
for (int i = 0; i < files.length - 30; i++) {
files[i].delete();
}
}
}
}
In My application i tried to delete other applications cache memory using the following code. But it is deleting my application cache only Not the others applications.
code like this:
public void clearApplicationData()
{
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir)
{
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
This is any way to delete only .jpg files from folder?
This is my remove method:
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
How can I remove only .jpg files from folder?
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
String filename = children[i];
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg"))
new File(dir, filename).delete();
}
}
or you prefer the for-each version
if (dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
if (child.endsWith(".jpeg") || child.endsWith(".jpeg"))
new File(dir, child).delete();
}
}
Try like this
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
if(children[i].endsWith('.jpg' || children[i].endsWith('.jpeg'))
{
new File(dir, children[i]).delete();
}
}
}
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
Then call
walkdir(dir);
public void walkdir(File dir) {
String Patternjpg = ".jpg";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(Patternjpg)){
//Do what ever u want
listFile[i].delete();
}
}
}
}
}