Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I try to get the files in the folder was updated last 15 minutes.
But am getting the files updated recently in code.
Any one please help me on this.

What I have tried:

Java
File[] files = dir.listFiles();
File lastModifiedFile = files1[0];
for (int i = 1; i < files1.length; i++)
{
   if (lastModifiedFile.lastModified() < files[i].lastModified()) {
      lastModifiedFile = files[i];
      System.out.println(lastModifiedFile.getName());
   }
}
Posted
Updated 8-Aug-18 7:00am
v2
Comments
Richard MacCutchan 8-Aug-18 5:14am    
Where is your test for the 15 minute time difference.
Member 13941960 8-Aug-18 5:23am    
I have edited the files in the directory and i try to get the filename which is updated last 15 minutes
Richard MacCutchan 8-Aug-18 5:30am    
And what is the problem?
Member 13941960 8-Aug-18 5:42am    
Am not able to get updated file name..
Richard MacCutchan 8-Aug-18 6:24am    
Try to stop and think about exactly what results you see versus what results you expect. Then please update your question with the exact code that you are using (not the above which will not even compile), and explain the details of exactly what is happening.

Try this:
Java
long last15 = Instant.now().toEpochMilli(); // number of milliseconds since the epoch
last15 -= 15 * 60 * 1000; // less 15 minutes
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files == null || files.length == 0) 
{
    return null;
}
for (int i = 0; i < files.length; i++)
{
    if (files[i].lastModified() > last15)
    { 
        System.out.println(files[i]);
    }
}
 
Share this answer
 
Comments
Member 13941960 9-Aug-18 0:03am    
Thank you it is working
Richard MacCutchan 9-Aug-18 3:00am    
Well I only copied Jochen's suggestion, nothing too difficult.
The lastModified time is represented in milliseconds since the epoch. So all you need is the current time and the difference in the same format.

For 15 minutes it could be
Java
import java.time.Instant;
// ...
Instant timestamp = Instant.now();
long timeLimitMillis = instant.toEpochMilli() - (15 * 60 * 1000);
// ...
if (files[i].lastModified() >= timeLimitMillis) 
{
    // Has been modified during the last 15 minutes
}
 
Share this answer
 
v2
Comments
Member 13941960 8-Aug-18 6:17am    
If am add this snippet to my code the program get terminated.
Jochen Arndt 8-Aug-18 6:31am    
The program or the compiler?
Probably the compiler because I forgot the parentheses for latsModified().
Member 13941960 8-Aug-18 6:36am    
I added and tried it's notworking
Jochen Arndt 8-Aug-18 6:58am    
I have no Java installed and can't test it therefore here.
So "not working" does not tell me anything useful to get it fixed.

For quick testing you can insert
System.out.println("Description" + value);
Richard MacCutchan 9-Aug-18 3:01am    
Just to let you know that I plagiarised your suggestion in my solution above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900