Results 1 to 18 of 18
  1. #1
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Question folder-subfolders, get data

    hi everyone,

    i'm having the following problem:

    i've got a folder (directory C:\Schoolschaak), where my program adds subfolders which have all specified names. In those subfolders, there are some txt files with all the same names (like Name.txt, Age.txt,...)..
    now my question is if there is a function which searches in this directory (C:\Schoolschaak) and returns a list of all subfolders in an array?

    thanks anyway,

    Questionmark =)

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Review: File

    Java Code:
    File.listFiles(...);

    Java Code:
    File.isDirectory();
    My Hobby Project: LegacyClone

  3. #3
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    thanks, but i don't really understand, with this method i can get files and folders, but i need only the names of the folders... or am i just missing it? :p

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    yea, you get the names from the File object returned in the array of results as mrmatt1111 said , with the File.getName() method

    Java Code:
      /** gets an array of string folder names of things inside a folder that are subdirectories
      public String[] getChildFolderNames(String startFolder) {
        List<String> folderNames = new ArrayList<String>();
    
        File aFolder = new File(startFolder);
        File[] allFiles = aFolder.listFiles();
        for (File f : allFiles) {
          if (f.isDirectory()) {
            folderNames.add(f.getName());
          }
        }
        return (String[]) folderNames.toArray(new String[0]);
      }

  5. #5
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    ooh i see xD
    thanks a LOT ^^ :D

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Questionmark View Post
    ooh i see xD
    thanks a LOT ^^ :D
    You really should have made an effort to read the excellent link that was posted and try to think about how to implement the solution given the methods suggested. Getting copy pastable code may seem helpful to you now but ruins your programming career.

  7. #7
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    i did... just don't understand the return types of the list functions...
    this piece of code stops adding Strings to the array when 1 subfolder has been found, so I have understood and edited the code... it's not that i didn't do the effort of reading that link, it's just that i'm a kid of 15 years old, not speaking English as a motherstongue, trying to program java and not able to understand all explications that have been given...

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What did you edit it to do? Care to share what you did?

  9. #9
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    it doesn't work as i want it to, it gives still an error, but i'm working on it.
    something like this, but it's one complete mess. doesn't work at all, but only have been busy 20 mins :p tonight i'll try harder =)


    public static String[] getChildFolderNames(String startFolder) {
    List<String> folderNames = new ArrayList<String>();

    File aFolder = new File(startFolder);
    File[] allFiles = aFolder.listFiles();
    int numberOfFiles = allFiles.length;
    File[] f;
    int i = 0;
    f = aFolder.listFiles();
    do{
    i++;
    if (f[i].isDirectory()) {
    folderNames.add(f[i].getName());
    subfoldersindir ++;
    }
    }while (i<numberOfFiles);
    return (String[]) folderNames.toArray(new String[0]);
    }

    public static int GetNOFID() {
    int returnint = subfoldersindir;
    subfoldersindir = 0;
    return subfoldersindir;
    }

    }

  10. #10
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    ignore the GetNOFID ^^

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you want to get all folders and sub folders then think recursion.

  12. #12
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    hey, good idea :D i'll give it a try :)

  13. #13
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    hmm, did it without recursion ;)
    it was the list that gave a lot of errors, probably me not understanding the list completely :p but i changed the list into an array of Strings and the returntype into String[], and changed a few things, like the condition of the for loop, and now it works =)

    Java Code:
     public static String[] getChildFolderNames(String startFolder) {
    	 		subfoldersindir = 0;
    	 		String[] folderNames = {null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};
    	 	   File aFolder = new File(startFolder);
    		    File[] allFiles = aFolder.listFiles();
    		  int i = 0;
    		  int a = 0;
    			int numberOfFiles = allFiles.length;
    			File[] f = allFiles; 
    		    for (i=0; i<numberOfFiles; i++) {
    		      if (f[i].isDirectory()) {
    		    	 
    		        folderNames[a] = f[i].getName();
    		        subfoldersindir ++;
    		        a++;
    		      }
    		    }
    		    return folderNames;
    Last edited by Questionmark; 11-08-2009 at 06:14 PM. Reason: [code]

  14. #14
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    don't mention the values of 'folderNames', it gave errors (out of bounds) ^^

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by travishein View Post
    yea, you get the names from the File object returned in the array of results as mrmatt1111 said , with the File.getName() method
    Another way to get just folders is to use a FileFilter:
    Java Code:
      public static File[] getSubDirectories(File file) {
        FileFilter filter = new FileFilter() {
    
          public boolean accept(File pathname) {
            if (pathname == null) {
              return false;
            }
            return pathname.isDirectory();
          }
          
        };
        return file.listFiles(filter);
      }

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Questionmark View Post
    public static String[] getChildFolderNames(String startFolder) {
    subfoldersindir = 0;
    String[] folderNames = {null,null,null,null,null,null,null,null,null,null ,null,null,null,null,null,null,null,null,null,.... .....
    What the heck??? Sorry, but that code should be taken out back and shot.

  17. #17
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What the heck??? Sorry, but that code should be taken out back and shot.
    do you know a way to have those null values disappear? I was just glad I found a function that worked xD yes, I was annoyed with it too, but i can't seem to find any other way. i'll try yours =)

  18. #18
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    i need a function that returns the names of the folders, so if you work this example out to a function that returns an array of Strings, you'll have the same outofboundsexception, and in my case to make it work I just added those null values... is there another way to have my code without the null values?

Similar Threads

  1. bin and lib folder
    By sdkanhere in forum New To Java
    Replies: 1
    Last Post: 10-07-2009, 04:46 PM
  2. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  3. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  4. Replies: 3
    Last Post: 09-26-2008, 06:05 PM
  5. How to create folder(s) and store data?
    By Grom in forum New To Java
    Replies: 2
    Last Post: 08-20-2008, 09:01 AM

Tags for this Thread

Posting Permissions

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