Android Tutorial recursive Directorystructures and what to do with this filelist

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:

DonManfred

Expert
Licensed User
Longtime User
I´ll put it there too. But i also think that an Tutorial will be good too for this.
BTW The principle of this can be used for a recursive ftp- and smb-listing too
I want to create examples for ftp and smb too and put it in this thread here.
 

DonManfred

Expert
Licensed User
Longtime User
Based on the above principle i´ll post an example here to copy recursive the contents of an Folder on your device to an SMB-Share when in WLAN...

Tags: sdcardtosmb, recursive upload to smb
 

Attachments

  • sdcard2smb.zip
    7.4 KB · Views: 511

aidymp

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

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

aidymp

Well-Known Member
Licensed User
Longtime User
Try this as a quick start

Amazing! similar to my execution, but without the errors! lol Thank you very much! I notice that I am making fundamental mistakes, it doesn't help having 12 month old twins in the same room! lol
 

XbNnX_507

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