Android Tutorial recursive Directorystructures and what to do with this filelist

DonManfred

Expert
Licensed User
We start with a few DIMs for needed Variables
B4X:
sub Globals
    Dim ffiles,ffolders As List
    Dim root As String
End Sub
and a main sub from this tutorial
B4X:
Sub ReadDir(folder As String, recursive As Boolean)
    'Log("ReadDir("&folder&")")
    Dim lst As List = File.ListFiles(folder)
  For i = 0 To lst.Size - 1
      If File.IsDirectory(folder,lst.Get(i)) Then
            Dim v As String
            v = folder&"/"&lst.Get(i)
            'Log("v="&v)
            ffolders.Add(v.SubString(root.Length+1))
            If recursive Then
                ReadDir(v,recursive)
            End If
        Else
            ffiles.Add(folder&"/"&lst.Get(i))
        End If
  Next
    'Log(ffolders.Size&" Ordner / "&ffiles.Size&" Dateien")
End Sub
You can enable the LOG-outputs if you want to see more
The point is that we are using two global variables (LISTs) which holds all Folders and Files.
The sub will put all folders to list ffolders and al files to list ffiles and call itself recursive

But first we need to Initialize the lists and then we can read the Directory-Listing(s)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ffiles.Initialize
    ffolders.Initialize
    root = File.DirRootExternal
    ReadDir(root,True)
    Log(ffolders.Size&" folder / "&ffiles.Size&" files")
End sub
 
Last edited:

aidymp

Active Member
Licensed User
We start with a few DIMs for needed Variables
B4X:
sub Globals
    Dim ffiles,ffolders As List
    Dim root As String
End Sub
and a main sub from this tutorial
B4X:
Sub ReadDir(folder As String, recursive As Boolean)
    'Log("ReadDir("&folder&")")
    Dim lst As List = File.ListFiles(folder)
  For i = 0 To lst.Size - 1
      If File.IsDirectory(folder,lst.Get(i)) Then
            Dim v As String
            v = folder&"/"&lst.Get(i)
            'Log("v="&v)
            ffolders.Add(v.SubString(root.Length+1))
            If recursive Then
                ReadDir(v,recursive)
            End If
        Else
            ffiles.Add(folder&"/"&lst.Get(i))
        End If
  Next
    'Log(ffolders.Size&" Ordner / "&ffiles.Size&" Dateien")
End Sub
You can enable the LOG-outputs if you want to see more
The point is that we are using two global variables (LISTs) which holds all Folders and Files.
The sub will put all folders to list ffolders and al files to list ffiles and call itself recursive

But first we need to Initialize the lists and then we can read the Directory-Listing(s)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ffiles.Initialize
    ffolders.Initialize
    root = File.DirRootExternal
    ReadDir(root,True)
    Log(ffolders.Size&" folder / "&ffiles.Size&" files")
End sub
Hi, Im trying to count the number of files in a large directory, This was the closest thing I found, as the directory has LOTS of sub directories. and I think around 40,000 files! the problem is it just quits!? with the error

GC_CONCURRENT freed 389K, 64% free 1869K/5148K, paused 0ms+1ms, total 10ms
140 folder / 142 files
threadid=3: reacting to signal 3
Wrote stack traces to '/data/anr/traces.txt'

Is it due to the size of the directory?

Thanks

Aidy
 

aidymp

Active Member
Licensed User
probably too much files and folders. maybe try it from a service so it may be allowed to use more time for it. Please note that the mainthread is hold while the recursive search is running
Hi Thanks for that, I am unsure how to add it to a service but will read and try now.

Thanks

Aidy
 

XbNnX_507

Active Member
Licensed User
B4X:
Sub search(path As String, r As Boolean)
  Dim currentFilesFolders As List = File.ListFiles( path )
  If currentFilesFolders.IsInitialized = False Then Return
  ffolders.Add( path )
  For Each fd As String In currentFilesFolders
      If File.IsDirectory( path, fd) Then
        Dim dir As String = File.Combine( path, fd )
        If r Then
            search( dir, r )
        Else
            ffolders.Add( dir )   
        End If
    Else
        ffiles.Add( fd )
    End If   
  Next
End Sub
This code does the same.;):)
 
Top