B4J Question List folder

ivanomonti

Well-Known Member
Licensed User
Hi, group b4j

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

Thank 1000
 

rwblinn

Well-Known Member
Licensed User
Hi,

example --- could be other solutions as well.
B4X:
'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 String) As Boolean
      Dim fileO As JavaObject
      fileO.InitializeNewInstance("java.io.File", Array As Object(f))
      Return fileO.RunMethod("isDirectory", Null)
End Sub
 

Erel

Administrator
Staff member
Licensed User
I don't think that the code above is correct.

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

Sub ListFolders(folder As String) As 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
 

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
 

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.
 

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.
 

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
 

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
 

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.
 
Top