14 Replies Latest reply: Aug 7, 2008 3:33 PM by 800282 RSS

    finding files and folders

    800313
      Basically what I want is small search program kind of like the windows search program but a little more advanced, I want to be able to search for a varitey of files and when these are found copy them to a new file.

      1. What code can I use to get file-names from a windows os.

      2. If the directory that I am looking in has sub folders, how can you tell if it is a folder of a file

      Is it even best to use java, or would I be better of trying to write a bat file.

      Thanks
        • 1. Re: finding files and folders
          Herko_ter_Horst
          Take a look at [java.io.File|http://java.sun.com/javase/6/docs/api/java/io/File.html] and the [File IO tutorial|http://java.sun.com/docs/books/tutorial/essential/io/fileio.html] .
          • 2. Re: finding files and folders
            793897
            Hi,

            Yes, it is perfectly possible to traverse a directory structure and get all files found on route. Something like this should do it :
            import java.io.File;
            import java.util.Iterator;
            
            public class Find
            {
               public Find()
               {
               }
            
               private void processDirectory(File name)
               {
                  File files[] = null;
            
                      /* Do we have access to this directory? */
                      if (name.canRead())
                      {
                           files = name.listFiles();
            
                           /* Loop through our files */
                           for (int curFile = 0; curFile < files.length; curFile++)
                           {
                                /* Do we have a directory or a file? */
                                if (files[curFile].isDirectory())
                                {
                                     processDirectory(files[curFile]);
                                }
                                else
                                {
                                     /* It's a file! */
                                     System.out.println("Found file " + files[curFile].getPath());
                                }
                           }
                      }
                      else
                      {
                           System.err.println("Cannot access " + name + " permission denied");
                      }
               }
            
               public static void main(String args[])
               {
                      Find find = new Find();
                      File f = new File("<enter your starting directory here such as c:\\");
                      find.processDirectory(f);
               }
            }
            • 3. Re: finding files and folders
              800313
              Thanks bhills thats exactly what im looking for ill have a play around with the code tonight
              • 4. Re: finding files and folders
                800313
                Thanks again for the code, works great, I understand it, and have modified to my needs except for one small thing

                How does it transverse through the folder I give it, I mean it prints out all the files in some crazy order. The folder I gave it is already sorted by filename, how can I print out each file in alphabetcial order?

                Calypso
                • 5. Re: finding files and folders
                  843785
                  Sort.
                  • 6. Re: finding files and folders
                    800313
                    but they are already sorted in the actual folder in windows.

                    How does the current code choose which file to get next then?
                    • 7. Re: finding files and folders
                      843785
                      calypso wrote:
                      but they are already sorted in the actual folder in windows.

                      How does the current code choose which file to get next then?
                      Directories are not really sorted. The program you are using to list the files is sorting them for you.

                      demo:
                      import java.io.*;
                      import java.util.*;
                      
                      public class DisplayFiles {
                          public static void main(String[] args) {
                              display(new File("../.."));
                          }
                      
                          static void display(File f) {
                              try {
                                  System.out.println(f.getCanonicalPath());
                                  File[] children = f.listFiles();
                                  if (children != null) {
                                      Arrays.sort(children);
                                      for(File child : children) {
                                          display(child);
                                      }
                                  }
                              } catch (IOException e) {
                                  e.printStackTrace();
                              }
                          }
                      }
                      • 8. Re: finding files and folders
                        PhHein
                        calypso wrote:
                        but they are already sorted in the actual folder in windows.
                        So, if you randomly write down numbers from the telephone book they are in that order in the telephone book too?
                        • 9. Re: finding files and folders
                          800282
                          far too late...

                          Edited by: prometheuzz on 7-aug-2008 22:32
                          • 10. Re: finding files and folders
                            800313
                            thanks DrLaszloJamf for the code

                            @phHein - so what your saying is that the code prints out each file according to when it was added to the actual folder?
                            • 11. Re: finding files and folders
                              843785
                              Let's consult the API.

                              [listFiles:|http://java.sun.com/javase/6/docs/api/java/io/File.html#listFiles()]

                              <quote>
                              There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.
                              </quote>
                              • 12. Re: finding files and folders
                                DrClap
                                calypso wrote:
                                but they are already sorted in the actual folder in windows.

                                How does the current code choose which file to get next then?
                                Don't waste your time wondering about why some other application shows the file in a particular order. It's your application you're working on. And as DrLJ said, if you want to sort the files into a particular order then do that. The Arrays.sort() method works perfectly well for sorting an array of File objects alphabetically based on their names.
                                • 13. Re: finding files and folders
                                  843785
                                  DrClap wrote:
                                  calypso wrote:
                                  but they are already sorted in the actual folder in windows.

                                  How does the current code choose which file to get next then?
                                  Don't waste your time wondering about why some other application shows the file in a particular order.
                                  As they say at the Javaholics Anonymous meetings: "God, give us grace to accept with serenity the things that cannot be changed..."
                                  • 14. Re: finding files and folders
                                    800308
                                    and the courage to write the things that can be written,