Monday, December 10, 2012

Delete all files from a directory


import java.io.File;


public class AllFilesDelete {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
   String dirPath="C:\\Junk\\"; //change the Directory accordingly
   File dir_path = new File(dirPath);
   File[] files = dir_path.listFiles();

   for(int i=0; i < files.length; i++){
   String fileName = files[i]+"";

File f1 = new File(fileName);
boolean success = f1.delete();

if (!success){
 System.out.println("Deletion failed.");
 System.exit(0);
}else{
 System.out.println(fileName + " - deleted.");
}
   }
 
if(files.length<1){
System.out.println("No files to be deleted");
}else
   System.out.println(files.length + " files deleted");
}
}

No comments:

Post a Comment