Click to See Complete Forum and Search --> : Copy folder based on latest date


Pankaj Kumar
05-07-2010, 07:54 AM
Hi,

Can anybody send me the java code to do following:

- A directory containing sub-folders with different name and date
- To copy the folder having the most recent date from the directory.

Thanks in advance.

Pankaj

JavaServlet
05-19-2010, 08:53 PM
Use File listFiles() and a comparator to sort your array by last mod date.
Apache Commons IO package has LastModifiedFileComparator that you can also use.

listFiles() example to help get you in the right direction:
public class FileWork
{
public static void main(String[] a)
{
File myFile = new File("C:" + File.separator);
for(File s: myFile.listFiles())
{
System.out.println(s);
}
}
}

Pankaj Kumar
11-14-2010, 11:21 AM
Thanks it helped.