B4J Question List folder

ivanomonti

Expert
Licensed User
Longtime User
Hi, group b4j

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

Thank 1000
 

rwblinn

Well-Known Member
Licensed User
Longtime 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
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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
 
Upvote 0

johnB

Active Member
Licensed User
Longtime 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
 
Upvote 0

johnB

Active Member
Licensed User
Longtime 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.
 
Upvote 0

johnB

Active Member
Licensed User
Longtime 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.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime 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.
 

Attachments

  • listfiles example.zip
    1.7 KB · Views: 352
Upvote 0

johnB

Active Member
Licensed User
Longtime 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
 
Upvote 0

johnB

Active Member
Licensed User
Longtime 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
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime 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.
 
Upvote 0
Top