How do we "Loop" through subfolders to delete files?
Hi Everyone, I have some example code bellow it works and was a tutorial on the web somewhere I forget where I got it but I would like to modify it. I have been searching for days on this mod but with no luck so here is my question: How do you make this work to include or "LOOP" through all the subfolders and do the delete there as well?
Thanks in advance for any direction.
Code:
import java.io.File;
import java.io.FilenameFilter;
public class delfile {
/**
* @param args
*/
public static void main (String args[]) {
System.out.println("hello");
delfile td = new delfile();
td.deleteFiles("c:/temp2/", ".csv");
}
public void deleteFiles( String d, String e ) {
ExtensionFilter filter = new ExtensionFilter(e);
File dir = new File(d);
String[] list = dir.list(filter);
File file;
if (list.length == 0) return;
for (int i = 0; i < list.length; i++) {
file = new File(d + list[i]);
boolean isdeleted = file.delete();
System.out.print(file);
System.out.println( " deleted " + isdeleted);
}
}
class ExtensionFilter implements FilenameFilter {
private String extension;
public ExtensionFilter( String extension ) {
this.extension = extension;
}
public boolean accept(File dir, String name) {
return (name.endsWith(extension));
}
}
}