B4J Question List folder

Discussion in 'B4J Questions' started by ivanomonti, Feb 3, 2015.

  1. ivanomonti

    ivanomonti Well-Known Member Licensed User

    Hi, group b4j

    how can I get a list of folders in a directory!!!

    Thank 1000
     
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    example --- could be other solutions as well.
    Code:
    'Example of listing the file & folders from the dirapp folder
    Sub ListFolders
        
    Dim l As List = File.ListFiles(File.DirApp)
        
    For i = 0 To l.Size - 1
            
    Log(l.Get(i) & " = Folder: " & isDir(l.Get(i)))
        
    Next
    End Sub

    'Check if a file is a folder
    'Return true if file is folder
    Sub isDir(f As StringAs Boolean
          
    Dim fileO As JavaObject
          fileO.InitializeNewInstance(
    "java.io.File"Array As Object(f))
          
    Return fileO.RunMethod("isDirectory"Null)
    End Sub
     
    jinyistudio likes this.
  3. ivanomonti

    ivanomonti Well-Known Member Licensed User

  4. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that the code above is correct.

    This code is:
    Code:
    Sub AppStart (Args() As String)
       
    Log(ListFolders("c:\temp"))
    End Sub

    Sub ListFolders(folder As StringAs List
       
    Dim folders As List
       folders.Initialize
       
    For Each f As String In File.ListFiles(folder)
         
    If File.IsDirectory(folder, f) Then folders.Add(File.Combine(folder,f))
       
    Next
       
    Return folders
    End Sub
     
    jmon likes this.
  5. johnB

    johnB Active Member Licensed User

    Does anybody know if there is any way to sort the Folders to the top, the same as when you click on the "Name" column in Windows File Explorer. It's taking 30 to 40 seconds to go thru some of my folders to find if a folder contains any Sub Folders

    Regards
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Sorting the whole list will not be faster than searching for a single item in the list (unless you need to search for many items).
     
  7. johnB

    johnB Active Member Licensed User

    Sorry, I obviously didn't explain properly.

    Some of my image folders have thousands of images and they may also have sub folders as well

    I want to find the sub folders only and searching the whole folder, using your routine above, can take 30 to 40 seconds.

    If I can sort the items in the folder so the folders come first, when I find an entry containing a ".", I can assume there are no more folders, so can exit the loop.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    It is probably the File.ListFiles call which is slow. Try it without any other code.

    You cannot make it faster. The only solution is to use a background thread to list the files.
     
  9. johnB

    johnB Active Member Licensed User

    Thanks Erel

    I was sort of resigned to it.

    I tried the MFLib library and found that it was faster (quite a bit) on the computer that the .jar was running on but it was a lot slower when accessing a Server.
     
  10. Daestrum

    Daestrum Well-Known Member Licensed User

    You may be able to modify this to suit your needs, it can list > 3000 files in about 800ms.
    Plus you can get file names or just the folders in a directory.
     

    Attached Files:

    johnB and inakigarm like this.
  11. johnB

    johnB Active Member Licensed User

    Absolutely brilliant Daestrum, it took 6800ms (instead of the 20 minutes) before to go thru 89000 files and 600 folders.

    I know nothing about java but had a feeling that java could do the trick, but I searched the Internet for any references and couldn't find anything, not that I could have coded it even if I did find a reference.

    Thanks again, that's terrific, it will make my app usable rather that having 20 minutes to get an index of the folders
     
  12. johnB

    johnB Active Member Licensed User

    I'm having a problem with the java script in B4A
    the error message I'm getting is :

    "package java.nio.file does not exist"

    Apparently java.nio is available from java V 7

    When I run (in the comand line) : java -version I get version 1.8.0.31

    In the config file in B4A and B4J, I've set up version 1.7.0.55

    The java code is running fine in B4J

    Regards
     
  13. Daestrum

    Daestrum Well-Known Member Licensed User

    It's probably caused by android 'java.nio' being a subset of 'java.nio' the android version has no java.nio.file as far as I can see from the api.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice