6 Replies Latest reply: Oct 18, 2004 9:48 AM by 807575 RSS

    Browsing all folders and files in all drives

    807575
      Hello! every1, I am trying to access all files and subdirectories in all the drives A:\C:\D:\, this is my code but not been able to go subfolders.
      public class LocateAllFiles {
      public static void main (String args[]) {      
           
      File AllRoots[] = File.listRoots();
      for (int k=0; k<AllRoots.length; k++){
      if (AllRoots[k].exists()) {     
      System.out.println (AllRoots[k]+" Accessible");
      File files[] = AllRoots[k].listFiles();
      for (int i = 0; i < files.length; i++)
      System.out.println(files[ i ]);
      }
      else System.out.println (AllRoots[k]+" NOT Accessible."); } }}Thanx in advance
        • 1. Re: Browsing all folders and files in all drives
          807575
          Hi

          Try this

          import java.io.*;

          public class LocateAllFiles {
               public static void main (String args[]) throws Exception {
                    int i = 0 ;
                    int l = 0 ;
                    
               File AllRoots[] = File.listRoots();
               for (int k=0; k<AllRoots.length; k++){
               if (AllRoots[k].exists()) {
                    System.out.println (AllRoots[k]+" Accessible");
                    File files[] = AllRoots[k].listFiles();
                    for (;i < files.length; i++) {
                    System.out.println(files);
                         if(files[i].isDirectory()) {
                         System.out.println(l++) ;
                              File subfile[] = files[i].listFiles();
                              for( int m= 0; m< subfile.length; m++)
                              System.out.println(subfile[m]);
                                   
                    }     
                    }     
                    }
                    else
                         System.out.println (AllRoots[k]+" NOT Accessible.");
               }
               }
               
               
               }


          Thanks
          Venu
          • 2. Re: Browsing all folders and files in all drives
            807575
            thanx, but still it seems i cant figure out why the compiler rejects known methods, i got as below:
            symbol : method isDirectory ()
            location: class java.io.File[]
            if(files.isDirectory()) {
            ^
            symbol : method listFiles ()
            location: class java.io.File[]
            File subfile[] = files.listFiles();
            ^
            • 3. Re: Browsing all folders and files in all drives
              807575
              Hi

              Can you post your complete code here

              Thanks
              Srini
              • 4. Re: Browsing all folders and files in all drives
                807575
                Replace it with this code :
                if(files.isDirectory()) {
                          System.out.println("Directory Number : " + l++) ;
                               File subfile[] = files[i].listFiles();
                               for( int m= 0; m< subfile.length; m++)
                               System.out.println(subfile[m]);
                     }
                • 5. Re: Browsing all folders and files in all drives
                  807575
                  I am in the first hurdle, as u can c it shows available drives, parent folders and first step files, but not subfolders.
                  import java.io.*;
                  import java.io.File.*;

                  public class LocateAllFiles {

                       public static void main (String args[]) {
                            
                            /**scan all roots in the system, like A:\,C:\,D:\.,
                            and check whether they are available for accessing
                            their files or not.
                            */
                       
                            File AllRoots[] = File.listRoots();
                            for (int k=0; k<AllRoots.length; k++)
                            {
                                 
                                 if (AllRoots[k].exists())
                                 {     System.out.println (AllRoots[k]+" Accessible");
                                      File files[] = AllRoots[k].listFiles();                     
                                      for (int i = 0; i < files.length; i++)
                                 System.out.println(files[ i ]);
                                 
                  }

                                 else System.out.println (AllRoots[k]+" NOT Accessible.");
                            } }}
                  • 6. Re: Browsing all folders and files in all drives
                    807575
                    Hai how can i search for a particular file eg( names.txt) in the directory called customers.