Recursive Method Traversing SdCard

rtesluk

Member
Licensed User
Longtime User
Oct 29 2011
01:10 Hours

I have been looking for code to be able to recurse all of the folders and files on SdCard. I found some VB6 code and converted it to B4A.

Here is the essential code of this project:

B4X:
Sub SdCard(CurPath As String)
Dim FileList, FolderList As List
Dim F As String

FileList.Initialize 
FolderList.Initialize 

If CurPath = "" Then
   CurPath = "/mnt/sdcard"
End If

If File.IsDirectory(CurPath,"") Then
   Try
   FileList = File.ListFiles(CurPath)
   If FileList.IsInitialized Then
      If FileList.Size > 0 Then
         F = CurPath
         Listview1.AddSingleLine(F)
         CodeModule.ViewList.Add(F) 
         For i = 0 To FileList.Size - 1 
            F = FileList.Get(i)
            If File.IsDirectory(CurPath & "/" & F,"")Then
               FolderList.Add(CurPath & "/" & F) 
            Else
               Listview1.AddSingleLine(CurPath & "/" & F)
               CodeModule.ViewList.Add(CurPath & "/" & F) 
               Counter = Counter + 1
               LabelCounter.Text = Counter
            End If
            DoEvents
         Next
      End If
   End If
   Catch
      Msgbox(LastException & " " & CurPath & " " & FileList.Size,"File")
   End Try
   'Recursive Code
   Try
   If FolderList.IsInitialized Then
      If FolderList.Size > 0 Then
         For i = 0 To FolderList.Size - 1 
            F = FolderList.Get(i)
            SdCard(F)         
            DoEvents
         Next
      End If      
   End If
   Catch
      Msgbox(LastException & " " & FolderList.Size & " " & F,"Folder")
   End Try
End If
End Sub

Download the project and try it.

Please leave comments.

Thanks :)
 

Attachments

  • TweetRecursive20111028.zip
    5.8 KB · Views: 518

NJDude

Expert
Licensed User
Longtime User
Nice code, however, you should clear the ListView everytime the SDCard Sub is called to avoid duplicates in case you tap on the Search button more than once.
 
Upvote 0

koaunglay

Member
Licensed User
Longtime User
Oct 29 2011
01:10 Hours

I have been looking for code to be able to recurse all of the folders and files on SdCard. I found some VB6 code and converted it to B4A.

Here is the essential code of this project:

B4X:
Sub SdCard(CurPath As String)
Dim FileList, FolderList As List
Dim F As String

FileList.Initialize
FolderList.Initialize

If CurPath = "" Then
   CurPath = "/mnt/sdcard"
End If

If File.IsDirectory(CurPath,"") Then
   Try
   FileList = File.ListFiles(CurPath)
   If FileList.IsInitialized Then
      If FileList.Size > 0 Then
         F = CurPath
         Listview1.AddSingleLine(F)
         CodeModule.ViewList.Add(F)
         For i = 0 To FileList.Size - 1
            F = FileList.Get(i)
            If File.IsDirectory(CurPath & "/" & F,"")Then
               FolderList.Add(CurPath & "/" & F)
            Else
               Listview1.AddSingleLine(CurPath & "/" & F)
               CodeModule.ViewList.Add(CurPath & "/" & F)
               Counter = Counter + 1
               LabelCounter.Text = Counter
            End If
            DoEvents
         Next
      End If
   End If
   Catch
      Msgbox(LastException & " " & CurPath & " " & FileList.Size,"File")
   End Try
   'Recursive Code
   Try
   If FolderList.IsInitialized Then
      If FolderList.Size > 0 Then
         For i = 0 To FolderList.Size - 1
            F = FolderList.Get(i)
            SdCard(F)        
            DoEvents
         Next
      End If     
   End If
   Catch
      Msgbox(LastException & " " & FolderList.Size & " " & F,"Folder")
   End Try
End If
End Sub

Download the project and try it.

Please leave comments.

Thanks :)
Thanks a lot! I like so much. And I'd like to know how I can write some code in Mediaplayer. I mean I want to make all mp3 files read in internal or sd card. My English is not good. So I can't write very well.
 
Upvote 0
Top