Find and query files in a directory in date format yyymmdd
-
You just need to list all files with the
.log
extension and find the last but one. So since array indices go from 0 to (count_of_entries - 1), the one at offset (count - 2) will be the last but one.Thanks for the suggestions. I made a start now by listing all the files with the extension .log and it works. since the files are in yyyymmdd.log format, i only want to list the last one - 1. then copy it to another file called logged.log for instance. Could you please help achieve this with the code below?. The code below navigate to the directory and list all files with the extension .log. The printed output is: 20180118.Log 20180117.Log 20180116.Log So I am interested in the second one: 20180117.Log
import java.util.List;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.regex.Pattern;class search
{
public static void main(String[] args) {String WILD\_CARD = ""; List fileList = new LinkedList(); File folder = new File("d:\\\\"); File\[\] listOfFiles = folder.listFiles(); if(WILD\_CARD!=null) { Pattern wildCardPattern = Pattern.compile(".\*"+WILD\_CARD+"(.\*)?.log",Pattern.CASE\_INSENSITIVE); for(File file: listOfFiles) { java.util.regex.Matcher match = wildCardPattern.matcher(file.getName()); while(match.find()){ String fileMatch = match.group(); if(file.getName().equals(fileMatch)) { fileList.add(file); // doesn't work } } } } else fileList = new LinkedList( Arrays.asList(folder.listFiles())); for (File f: fileList) System.out.println(f.getName());
}
}
-
Thanks for the suggestions. I made a start now by listing all the files with the extension .log and it works. since the files are in yyyymmdd.log format, i only want to list the last one - 1. then copy it to another file called logged.log for instance. Could you please help achieve this with the code below?. The code below navigate to the directory and list all files with the extension .log. The printed output is: 20180118.Log 20180117.Log 20180116.Log So I am interested in the second one: 20180117.Log
import java.util.List;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.regex.Pattern;class search
{
public static void main(String[] args) {String WILD\_CARD = ""; List fileList = new LinkedList(); File folder = new File("d:\\\\"); File\[\] listOfFiles = folder.listFiles(); if(WILD\_CARD!=null) { Pattern wildCardPattern = Pattern.compile(".\*"+WILD\_CARD+"(.\*)?.log",Pattern.CASE\_INSENSITIVE); for(File file: listOfFiles) { java.util.regex.Matcher match = wildCardPattern.matcher(file.getName()); while(match.find()){ String fileMatch = match.group(); if(file.getName().equals(fileMatch)) { fileList.add(file); // doesn't work } } } } else fileList = new LinkedList( Arrays.asList(folder.listFiles())); for (File f: fileList) System.out.println(f.getName());
}
}
-
I just double checked the output printed is in ascending order as shown below: 20180115.LOG 20180116.LOG 20180117.LOG 20180118.LOG
-
Then my first suggestion still applies. Is this really so difficult to understand? It seems quite straightforward to me; or maybe I am missing something.
-
Just count the entries, or convert the list to an array and use the offset value I suggested.
-
Ok. I tried somehting like this so I can print the last file - 1 it prints the file but with the file path as well
Object item = (((Deque) fileList).getLast()-1);
System.out.println(item); -
Not yet. It prints the path of the file rather than the file It should print:
20180117.LOG
but it is printing
d:\20180117.LOG
I only want the file so I can create a copy of it. Can you try it and see if works for you? may be format issue?