Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: recursive search of all local disks

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question recursive search of all local disks

    How to search all the local disks and list all the files present in the disk? how to detect the removable disk, so that i can search removable disks also. i will be helpfull if i can get a code snippet. thank u in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: recursive search of all local disks

    There are several reasons why you may not be able to do this:

    protected locations, and the many different ways different OS's organize drives. Protected locations are impossible to access without hacking (possible legal issues here) or having all the passwords/access rights. OS files as well as many Virus Protection Software files have vastly restricted access, as well as any files that a certain user wishes to mark as protected.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: recursive search of all local disks

    I have a method for this, however it will take you a very long time depending on the number of files you have on your disk.

    WARNING: Use with caution

        public static List<File> listFiles(final String path, final boolean recursive) {
            final List<File> files = new ArrayList<File>();
            final File startPath = new File(path);
     
            if (startPath != null && startPath.listFiles() != null) {
                for (final File file : startPath.listFiles()) {
                    if (file.isDirectory() && recursive) {
                        files.addAll(listFiles(file.getAbsolutePath(), recursive));
                    } else if (file.isFile()) {
                        files.add(file);
                    }
                }
            }
     
            return files;
        }

    And to invoke this with your drive root, call it like this.

            final List<File> files = listFiles(File.separator, true);

    // Json

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: recursive search of all local disks

    Quote Originally Posted by Json View Post
    I have a method for this, however it will take you a very long time depending on the number of files you have on your disk.

    WARNING: Use with caution

        public static List<File> listFiles(final String path, final boolean recursive) {
            final List<File> files = new ArrayList<File>();
            final File startPath = new File(path);
     
            if (startPath != null && startPath.listFiles() != null) {
                for (final File file : startPath.listFiles()) {
                    if (file.isDirectory() && recursive) {
                        files.addAll(listFiles(file.getAbsolutePath(), recursive));
                    } else if (file.isFile()) {
                        files.add(file);
                    }
                }
            }
     
            return files;
        }

    And to invoke this with your drive root, call it like this.

            final List<File> files = listFiles(File.separator, true);

    // Json
    Mr.Json, thank u for ur help. yet i have some doubts.Is "List<File> listFiles" in 1st line the class name or simply a method ? If it is a method, is it just enough to create an object and pass (File.separator, true) as parameters?

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: recursive search of all local disks

    The first code block is the actual method which will call itself, the second code block is the line of code which will start the process off.

    // Json

Similar Threads

  1. Arrays.sort or iterative search?
    By igniteflow in forum Collections and Generics
    Replies: 1
    Last Post: September 16th, 2009, 02:07 AM
  2. Recursive Solution to Knights tour
    By budder8818 in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 4th, 2009, 03:31 PM
  3. How to highlight search keyword in text?
    By Mohd in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: February 1st, 2009, 06:35 AM
  4. Implementation of search function for Patricia Trie
    By javaguy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 21st, 2008, 01:54 PM

Tags for this Thread