How to find a folder?

Djembefola

Active Member
Licensed User
Longtime User
My application uses several gigabytes of multimedia data.

The data must be copied from DVD to a folder on the android device or is preinstalled on a blank sdcard.

The Folder name is constant, but it can be located in
file.DirRootExternal or on the sdcard.

(sdcard path is device-dependent and different on many android devices)

The only thing my application knows for sure, is the subfolder name.

How can i find the location of Folder "/xy"
- without knowing the complete path
- without asking the user to enter a path?
 

thedesolatesoul

Expert
Licensed User
Longtime User
Probably search for it. At least you know the subfolder name!

Files.ListFiles(Path) returns a list of files in the path.

The following is a very crude scanner which will scan all directories from root for a folder.
3 Problems.
1. It will scan the whole filesystem.
2. Another subdirectory can have the same name.
3. The arguments for isdirectory isnt correct in the given code.


B4X:
sub FindFile(Path as string)
fl = Files.ListFiles(Path)
for i = 0 to fl.size-1 
   if fl.get(i) = yoursubfoldername then
        yourpath = fl.get(i)
        return
   end if
   if file.IsDirectory(fl.get(i)) then
       FindFile(fl.get(i))
   end if
next
end sub

EDIT:
Sorry, in order to call it you can do:
B4X:
yourpath = ""
FileFile("/")
 
Last edited:
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
Thank you, thedesolatesoul. your sample code did not work but it pointed me in the right direction.

It is not as easy as it looks like:

Listfiles throws an exception or returns null for some system folders.
Furthermore there are some folders, which should be excluded from the recursive search, because otherwise strange things happen.

Finally i have written a procedure, which seems to work fine, but i'm not sure if this is the case on all android versions and devices:


B4X:
 Sub FindFolder(myPath As String, subfolder As String) As String

Dim fl As List
Dim i As Int
Dim p,f,r,dummy As String


  dummy=File.DirRootExternal 'Get sdcard Permission

  Log (mypath)  
  
  fl.Initialize
  
  Try
    'Listfiles throws Exception on some folders
    If mypath = "" Then
      fl = File.ListFiles("/")
    Else   
      fl = File.ListFiles(myPath)
    End If   
  Catch
    Return ""
  End Try
  
  'Listfiles returns null for some system folders
  If fl.IsInitialized = False Then Return ""
    
  For i = 0 To fl.size-1 
    p = mypath 
    f = fl.Get(i)
    Select p      'ignore some system folders
    Case "/dev"   
    Case "/proc"
    Case "/sys"
    Case "/system"
    Case Else
      If File.IsDirectory(p,f) Then
        If f = subfolder Then Return p  & "/" & f  '<--- success
        If File.IsDirectory(p, f) Then
           p = p & "/" & f
           p = FindFolder(p, subfolder)            '<---recursive search
           If p <> "" Then Return p                '<---success
        End If
      End If  
    End Select
  Next
  Return ""
  
End Sub
 
Last edited:
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Never expect my code to work...its usually just a brain dump of my head ;)

its always nice to see other people trying and i get to see the results for free!

btw...i dont think you should be looking in root ("/") for your subdirectory anyway, it will never be there.
I think that the sdcard is always in /sdcard (which is the same as /mnt/sdcard)...which should also be the same as File.DirRootExternal
Atleast this path should always be there?

and yes...sorry for sending you down /proc and /dev ...i wonder what happened down there...

however i am assuming you have a rooted rom and the emulator is rooted? otherwise how are we allowed to access these locations.
 
Upvote 0

Djembefola

Active Member
Licensed User
Longtime User
I think that the sdcard is always in /sdcard (which is the same as /mnt/sdcard)...which should also be the same as File.DirRootExternal
Atleast this path should always be there?

unfortunately not always.

see this thread

and yes...sorry for sending you down /proc and /dev ...i wonder what happened down there...

strange things... :eek:

however i am assuming you have a rooted rom and the emulator is rooted? otherwise how are we allowed to access these locations.

I'm not smart enough to root my phone. :eek: the code i've posted works on unrooted devices.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
unfortunately not always.

see this thread
I see your concern.
It is possible to look in /mnt to see all mounted filesystems. I would grep for something 'sd' or 'card', and they should return a mount point.
you could also use 'df' as some people have here: http://www.b4x.com/forum/basic4andr...ns/9093-codemodule-checksdcard.html#post64080

But I guess dealing with external sdcards, there is only so much you can do. Different manufacturers will mount it in different places.

I'm not smart enough to root my phone. :eek: the code i've posted works on unrooted devices.
You dont have to be smart! For me it was a one-click app I got from the market! ...however i suggest you dont bother for now. its better for testing.
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
Neat, I know where I have additional files stored. I'd like to launch an intent to open up a file browser to that specific folder.

I understand that android might not have a file browser? That true?
 
Upvote 0
Top