Android Tutorial recursive Directorystructures and what to do with this filelist

Discussion in 'Tutorials & Examples' started by DonManfred, May 3, 2014.

  1. DonManfred

    DonManfred Expert Licensed User

    We start with a few DIMs for needed Variables
    Code:
    sub Globals
        
    Dim ffiles,ffolders As List
        
    Dim root As String
    End Sub
    and a main sub from this tutorial
    Code:
    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)
    Code:
    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: May 3, 2014
  2. LucaMs

    LucaMs Expert Licensed User

    Thanks, Manfred.

    Why did you not put this in the Snippets?
     
  3. DonManfred

    DonManfred Expert Licensed 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.
     
  4. DonManfred

    DonManfred Expert Licensed 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
     

    Attached Files:

  5. DonManfred

    DonManfred Expert Licensed User

    Based on the above principle i´ll post an example here to upload recursive the contents of an Folder on your device to ftp...

    Tags: sdcardtoftp, recursive upload to ftp
     

    Attached Files:

  6. DonManfred

    DonManfred Expert Licensed User

    As we are talking about recursion... Here you´ll find an example which reads an recursive ftp-directory-tree
     

    Attached Files:

  7. aidymp

    aidymp Active Member Licensed User

    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
     
  8. DonManfred

    DonManfred Expert 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
     
  9. aidymp

    aidymp Active Member Licensed User

    Hi Thanks for that, I am unsure how to add it to a service but will read and try now.

    Thanks

    Aidy
     
  10. DonManfred

    DonManfred Expert Licensed User

    Try this as a quick start
     

    Attached Files:

    aidymp likes this.
  11. aidymp

    aidymp Active Member Licensed User

    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
     
    LucaMs likes this.
  12. XbNnX_507

    XbNnX_507 Active Member Licensed User

    Code:
    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.;):)
     
  13. noeleon

    noeleon Member Licensed User

    Can someone please share a version of this using the ExternalStorage class?
     
  14. DonManfred

    DonManfred Expert Licensed User

    You should create a new thread in the questionsforum for any issue you have.
     
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