how to find last modified file in a directory

Hi,

I am trying to find the last modified file in a directory .

I know how to read the directory and get the last modified date of a file but I need to find the ONE file which is the very recent file in a folder.

I have being trying various methods, like extract the modified date and compare it to the current date, but cannot convert the dates into a format where I can extract them from each other to find the lowest value so I can extract that file!

I would be grateful is anyone can advise or point me in the right direction to accomplish what I am trying to do,

thanks

[613 byte] By [thanua] at [2007-10-3 5:19:00]
# 1

Since you know how to get the information you need, here is some pseudo code:

mostRecentDate = null

mostRecentFile = null

for each file in the directory

if mostRecentDate is null or file modified date is after mostRecentDate

mostRecentDate = file modified date

mostRecentFile = file

end if

end for

This is assuming that by last modified you mean the most recently

modified file.

JayDSa at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

oops sorry. let me be clear.

well i read the files inside the folder and the last modified date and time of each file inside the folder.

now what i need is to find which file was modified most recently.

iam not able to compare the last modified date of each file.

will be thankful if u help me out to solve this

thanks

thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

> well i read the files inside the folder and the last

> modified date and time of each file inside the

> folder.

You have the last modified date of each file

> iam not able to compare the last modified date of

> each file.

so why can't you compare them?

JayDSa at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

here is my code. I got the last modified date of each file inside the folder and storing it in a vector. How to compare these dates and find the most recently modified file.

import java.io.File;

import java.util.Vector;

import java.util.Date;

import java.text.SimpleDateFormat;

import java.util.*;

import java.text.*;

public class DirectoryList {

public static void main(String[] args) {

String directoryName;

File directory;

String[] files;

Vector lastmoddate=new Vector();

directoryName = "D:/test";

directory = new File(directoryName);

if (directory.isDirectory() == false) {

if (directory.exists() == false)

System.out.println("There is no such directory!");

else

System.out.println("That file is not a directory.");

}

else {

files = directory.list();

System.out.println("Files in directory \"" + directory + "\":");

for (int i = 0; i < files.length; i++)

{

System.out.println("" + files[i]);

String fpath=directoryName+"/"+files[i]; //File Path

// System.out.println("Fpath:" +fpath);

File f=new File(fpath);

Date date = new Date(f.lastModified());

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");

System.out.println(dateFormat.format(date));

lastmoddate.add(dateFormat.format(date)); //Vector which holds last modified date of all the files in that directory

}

}

}

}

thanks

thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 5
> How to compare these dates and find the most recently modified file.The most recently modified file will be the one with the largest value returned from the lastModified() method. So you just loop through all the files to find the largest value.
camickra at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 6

hi!

try this out..

import java.io.File;

public class DirectoryList

{

public static void main(String[] args)

{

String directoryName;

File directory;

File[] files;

directoryName = "D:/woptest";

directory = new File(directoryName);

if (directory.isDirectory() == false)

{

if (directory.exists() == false)

System.out.println("There is no such directory!");

else

System.out.println("That file is not a directory.");

}

else

{

files = directory.listFiles();

File lastModifiedFile = null;

System.out.println("Files in directory \"" + directory + "\":");

for (int i = 0; i < files.length; i++)

{

if(files[i].isFile())

{

if(lastModifiedFile == null)

lastModifiedFile = files[i];

if(lastModifiedFile != null)

{

if(lastModifiedFile.lastModified() < files[i].lastModified())

lastModifiedFile = files[i];

}

}

else

{

//it is not file so handle it as you want..

}

}

System.out.println("Last Modified File is.. " + lastModifiedFile);

}

}

}

:)

Aniruddha-Herea at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 7
ya got it. thank u very much. thanks all. i also tried another solution. i used Collections.sort() to sort the vector and got the result. it also works well.well this forum really helps me a lot. thank u. it will be really useful for beginners like me.have nice day
thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 8

oh ya another problem. this works only for a folder. i need to check even subfolders and get the recently modified date.

for eg. i have a folder: Test which has 5 files and one subfolder.

subfolder has 3 files and a subfolder1

subfolder1 has 5 files and a subfolder2

so if the user specifies the root folder Test it should scan all the files inside subfolders and should display the recently modified file.

huh how to do this. is there any example?

thanks

thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 9
try for recursion ....
Aniruddha-Herea at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 10

anirudha thanks a lot. i tried with recursion but it gives last modified file of each subfolder.

eg:Last modified file of Root folder Test

Last modified file of subfolder

Last modified file of subfolder1

and so on.....

but i need only one output ie. the recently modified file in total

thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 11
hi!in that case keep File lastModifiedFile = null; variable as global, class member. and then you will get what you want i guess.:)
Aniruddha-Herea at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 12

ya thanks. i noticed that and declared it as global variable. now its working but problem is

my root folder Test has files and 3 subfolders

Test

->files

-->subfolder1

->files

->subfolder1

--> subfolder2

now problem is first it checks all the files in root folder Test and

then checks subfolder1,its files and subfolders. then it is not coming

back to subfolder2

huh what to do?

thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 13
sorry root folder Test has 2 subfolders. first it checks 1st subfolder and not coming back to 2nd subfolder.
thanua at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 14
hi!can you provide your current code that you are running?
Aniruddha-Herea at 2007-7-14 23:25:54 > top of Java-index,Desktop,Core GUI APIs...
# 15

here is the coding

public class file extends javax.swing.JFrame {

String root="D:/test";

String directoryName;

String subfolder;

File directory;

String[] files1;

File[] files;

Vector parsedate=new Vector();

File lastModifiedFile = null;

public static file SINGLETON;

Thread s=null;

public static file getInstance()

{

if(SINGLETON==null)

return SINGLETON=new file();

else

return SINGLETON;

}

/** Creates new form file */

public file() {

initComponents();

listfiles(root);

//Compare The Dates

Collections.sort(parsedate);

System.out.println("Last Modified File is: " + lastModifiedFile +" Time " +parsedate.lastElement());

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

file.getInstance().setVisible(true);

}

});

}

void listfiles(String dname)

{

System.out.println(dname);

//directoryName = "D:/test";

directoryName=dname;

directory = new File(directoryName);

if (directory.isDirectory() == false)

{

if (directory.exists() == false)

System.out.println("There is no such directory!");

else

System.out.println("That file is not a directory.");

}

else

{

files = directory.listFiles();

files1=directory.list();

System.out.println("Files in directory \"" + directory + "\":");

for (int i = 0; i < files.length; i++)

{

System.out.println("" + files1[i]);

String fpath=directoryName+"/"+files1[i];

// System.out.println("Fpath:" +fpath);

File f=new File(fpath);

Date date = new Date(f.lastModified());

DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");

System.out.println(dateFormat.format(date));

//Date u=dateFormat.format(date);

parsedate.add(dateFormat.format(date));

if(files[i].isFile())

{

if(lastModifiedFile == null)

lastModifiedFile = files[i];

if(lastModifiedFile != null)

{

if(lastModifiedFile.lastModified() < files[i].lastModified())

lastModifiedFile = files[i];

}

}

else

{

System.out.println("Not File" +files[i]);//Not a File

subfolder=files[i].toString();

listfiles(subfolder);

}

}

/* //Compare The Dates

Collections.sort(parsedate);

System.out.println("Last Modified File is: " + lastModifiedFile +" Time " +parsedate.lastElement());*/

}

}

thanua at 2007-7-21 10:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 16

hI!

try this now. i hope this time it will be perfect for you

import java.io.File;

import java.util.Date;

public class DirectoryList

{

private static final StringdirectoryName= "D:/woptest";

private static FilelastModifiedFile= null;

private static void listFiles(File a_dir)

{

File[] files;

files = a_dir.listFiles();

for (int i = 0; i < files.length; i++)

{

if (files[i].isFile())

{

if (lastModifiedFile == null)

lastModifiedFile = files[i];

if (lastModifiedFile != null)

{

if (lastModifiedFile.lastModified() < files[i].lastModified())

lastModifiedFile = files[i];

}

}

else

{

listFiles(files[i]);

}

}

}

public static void main(String[] args)

{

listFiles(new File(directoryName));

System.out.println("Last Modified File is.. " + lastModifiedFile + " and time is: " + new Date(lastModifiedFile.lastModified()));

}

}

:)

Aniruddha-Herea at 2007-7-21 10:58:51 > top of Java-index,Desktop,Core GUI APIs...
# 17
oh my god it works perfect. great yar. thanks a lot anirudha. really u helped me a lot to complete this. now iam moving to next module.thank u my dear.
thanua at 2007-7-21 10:58:51 > top of Java-index,Desktop,Core GUI APIs...