Results 1 to 17 of 17

Thread: find a specific directory

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    find a specific directory

    Does anyone know a way to find a file on a users system without knowing the path name, just the directory name? I know it's probably some recursive function, and if it is, could you kind of get me started on it, or point me in the right direction?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: find a specific directory

    I don't know if it would have to be recursive or not. Perhaps if the directory contained other directories.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: find a specific directory

    This might help.
    Code:
     import java.io.*;
    
     public class DirectoryLister{
      public static void main(String[] args){
    
      File entry;
      if(args.length == 0){
         System.out.println("Please specify a directory for scanning");
         return;
      }else {
        entry = new File(args[0]);
      }
      if(!entry.exists()){
        System.out.println("Unable to process request " + entry.getPath() + " is nonexistent ");
        return; 
      }
     listDirectory(entry);
     }
    
    public static void listDirectory(File entry){
      try{
       if(!entry.exists()){
          System.out.println(entry.getName() + "not Found");
      }
      if(entry.isFile()){
         System.out.println(entry.getCanonicalPath());
    
      }else if (entry.isDirectory()){
         System.out.println(entry.getPath());
         String[] filename  = entry.list();
          
         if(filename == null) return; 
         for(int i = 0; i < filename.length; i++){
           File item = new File(entry.getPath(), filename[i]);
         
         //   list it by a recursive call
            listDirectory(item);
         }
       }
      } catch(IOException e){System.out.print("Error:" + e);} 
     }
    }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: find a specific directory

    You would have to chew through the directory structure and get a list of all the files in each nested directory ie. String[] filename = entry.list(); Then loop through the array returned and compare each string with the name of the file that you are looking for.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: find a specific directory

    That was pretty awesome. The only thing is, lets say I don't know the starting directory, how would I do that? I tried something like this, but it doesn't work...

    Code:
       public File PathStart()
       {
    	for (char dirs='a'; dirs >= 'z'; dirs++)
    	{
    		File path = new File(dirs + ":/");
    			if (path.isDirectory())
    			{
    				startingPath = path;
    				System.out.println(startingPath.toString());
    			}
    	}
    	return startingPath;
       }
    I was hoping I could get the root directory from this, and then use the return val as the root dir.

    There's two problems with the code:
    1) lol it doesn't work
    2) the user could have cd drives, usb or whatever

    If it makes it easier, I can explain the program I'm TRYING to make.

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: find a specific directory

    Sorry, startingPath was a File Object declared as a global variable.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: find a specific directory

    You have the "/" backwards. It should be ":\" for windows platforms. You should also get in the habbit of writing platform independent code when the code accesses the underlying file system of the os.

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: find a specific directory

    Ok. I changed it, but if that's not a good way to do it, how could I write code that is platform independent and starts at the root directory?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: find a specific directory

    Windows has no root directory, so that's kinda hard to do. It's something really tricky, and I haven't yet seen a satisfactory solution.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: find a specific directory

    Take a look at the static members of the File class like File.separator they are pretty easy to use.
    Code:
    import java.io.File; 
    
    String s = new String("C:" + File.separator);

  11. #11

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: find a specific directory

    Yes, but how many one-drive systems are there? I mean, every computer has at least a floppy drive or a CD drive.

    Then there's network shares - don't get me started...

    On the other hand, there's the Windows Shell namespace, which has a single root, the desktop. The Shell namespace is what you browse through with Explorer.

    I'm currently trying to write a C++ library to capture this stuff. It's ... interesting.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

  14. #14

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: find a specific directory

    I wonder if we can somehow use this:

    Code:
    	String path = System.getProperty("user.dir");
    Do you think this would work, or is this just gonna give the directory that the program is currently running from?

  15. #15
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: find a specific directory

    the user.dir property you mention points to the hom directory of the current tuser on linux, or the my Documents on windows, you could use this as a starting point, and step backwards through the parents untill you reach a null, thenyou'd know where your 'root' was, however as dilenger said on windows you don't have a root as such c:\ is only the root of the c drive, where as on linux everything is mapped in the one filesystem.. i don't think Java offers the granularity of the actual underlying system, instead showing them as C: D: etc..

  16. #16
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: find a specific directory

    i've had a thought, you could get a FileSystemView, this is what makes up the core of the JFileChooser, there is a method on it that is called getRoots() this returns a File[], if this does what it says on the tin it should give you a starting point for a recursive search of your file system!

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: find a specific directory

    It does indeed work, but only for local drives.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width