Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Find and query files in a directory in date format yyymmdd

Find and query files in a directory in date format yyymmdd

Scheduled Pinned Locked Moved Java
questionjavadatabasehelp
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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.

    H Offline
    H Offline
    hm9
    wrote on last edited by
    #4

    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());
    

    }

    }

    L 1 Reply Last reply
    0
    • H hm9

      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());
      

      }

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #5

      Obviously it is the opposite of what I suggested, as the list is in descending order.

      H 1 Reply Last reply
      0
      • L Lost User

        Obviously it is the opposite of what I suggested, as the list is in descending order.

        H Offline
        H Offline
        hm9
        wrote on last edited by
        #6

        I just double checked the output printed is in ascending order as shown below: 20180115.LOG 20180116.LOG 20180117.LOG 20180118.LOG

        L 1 Reply Last reply
        0
        • H hm9

          I just double checked the output printed is in ascending order as shown below: 20180115.LOG 20180116.LOG 20180117.LOG 20180118.LOG

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          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.

          H 1 Reply Last reply
          0
          • L Lost User

            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.

            H Offline
            H Offline
            hm9
            wrote on last edited by
            #8

            Yes. Just not sure how to do it in the code

            L 1 Reply Last reply
            0
            • H hm9

              Yes. Just not sure how to do it in the code

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              Just count the entries, or convert the list to an array and use the offset value I suggested.

              H 1 Reply Last reply
              0
              • L Lost User

                Just count the entries, or convert the list to an array and use the offset value I suggested.

                H Offline
                H Offline
                hm9
                wrote on last edited by
                #10

                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);

                L 1 Reply Last reply
                0
                • H hm9

                  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);

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  So you have a working solution.

                  H 1 Reply Last reply
                  0
                  • L Lost User

                    So you have a working solution.

                    H Offline
                    H Offline
                    hm9
                    wrote on last edited by
                    #12

                    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?

                    L 1 Reply Last reply
                    0
                    • H hm9

                      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?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #13

                      You just need to use a method to extract just the filename, such as: File (Java Platform SE 7 )[^]. Simple answers like this can usually be found in the documentation.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups