07 April, 2012

Delete All Files From Directory

import java.io.File;
public class DeleteFilesInDirectory
{
//********************************************

public static void DeleteFilesInDirectory(String DirectoryName)
{
String dirname = DirectoryName;
File f1 = new File(dirname);
if (f1.isDirectory())
{
    System.out.println("Directory of " + dirname);
    String s[] = f1.list();
    for (int i=0; i <s.length; i++)
    {
    File f = new File(dirname + "/" + s[i]);
    if (f.isDirectory())
    {
          System.out.println(s[i] + " is a directory in " + dirname);
    }
    else
    {
          //System.out.println(s[i] + " is a file");
          File d1=new File("Data\\"+s[i]);
          d1.delete();
    }
     }
}
else
{
    System.out.println(dirname + " directory is not found");
}

}
//********************************************

public static void main(String args[])
{
String DirName="Data";
DeleteFilesInDirectory(DirName);
}

//********************************************
}

No comments:

Post a Comment

Thank You !