import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
class TxtFileNameFilter implements FilenameFilter
{
public boolean accept(File f, String s)
{
return s.toLowerCase().endsWith(".txt");
}
}
public class deletebutton
{
private static long getDate(){
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -30);
return c.getTimeInMillis();
}
public static void deleteTree (File path)
{
final long d = getDate();
for (File file : path.listFiles(new TxtFileNameFilter()))
{
if (file.isDirectory()&&path.lastModified()<d)
deleteTree(file);
file.delete();
//System.out.println(file);
}
path.delete();
//System.out.println(path);
}
public static void main (String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
JButton b = new JButton ("Löschen");
ActionListener al = new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
deleteTree (new File ("D:/Java/"));
System.exit(0);
}
};
b.addActionListener(al);
frame.add(b);
frame.pack();
frame.setVisible(true);
}
}