Extract File Path

Vinians2006

Active Member
Licensed User
Longtime User
Hi friends! I used to program in Delphi and its have a function called "ExtractFileName" and "ExtractFilePath" my question is there are functions like theses in B4A ? Thanks in advance!
 

Vinians2006

Active Member
Licensed User
Longtime User
This will show all the Folders in the Root of the device:

B4X:
Dim ml As List
ml.Initialize
ml = File.ListFiles("/mnt")
InputList(ml, "Root Directory", -1)
Thanks but its not what I need. Below an explanation:
"/folder/other/file.txt"
I want a function that return only the path part from above string, so this function should return:
"/folder/other"
I know I can create a function to handle it but my question is if already exist that function or not like "ExtractFilePath()" Delphi function.
Thanks!
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I wrote these a while back and they have been working flawlessly:

B4X:
Sub GetFileName(FullPath As String) As String
   Return FullPath.SubString(FullPath.LastIndexOf("/")+1)
End Sub

Sub GetFileExt(FullPath As String) As String
   Return FullPath.SubString(FullPath.LastIndexOf(".")+1)
End Sub

Sub GetParentPath(Path As String) As String
   Dim Path1 As String 
   If Path = "/" Then
      Return "/"
   End If
   L = Path.LastIndexOf("/")
   If L = Path.Length - 1 Then
      'Strip the last slash
      Path1 = Path.SubString2(0,L)
   Else
      Path1 = Path
   End If
   L = Path.LastIndexOf("/")
   If L = 0 Then
      L = 1
   End If
   Return Path1.SubString2(0,L)
End Sub
 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
Thank you

Thanks for this code. Exactly what I needed!
 
Upvote 0
Top