How To Detect The Selected Path Is A Folder Or A Drive

jothis

Active Member
Licensed User
Hi Friends

This is My Code Please Help Me
B4X:
        FolderChooser.New1
   i=FolderChooser.Show
   If(i=cOK)Then
   TextBox1.Text=FolderChooser.SelectedFolder
   Else
   TextBox1.Text="Select Folder"
   End If
I Got The Text Field Output As The Selected Folder Eg(D:\My)
My Problem Is If A Folder Is Selected I Want To Add a Back Slash At The End Eg(D:\My\)
I Writed This Code For That
B4X:
    PATH=TextBox1.Text&"\"
I Printed The String PATH
I Got The Out Put As D:\My\
But I Selected A Drive I Got Out Put As D:\\
This is A Invalid Path. Because I Want To Know How To Do This.
Can You Please Help Me?
Jothis
:sign0085::sign0085:
 

berndgoedecke

Active Member
Licensed User
Longtime User
workaround

Hey Jothis,
what do think about this:

Sub Globals
'Declare the global variables here.
Dim Proof(0)
End Sub

Sub App_Start
Form1.Show
Text1Str = PathProof("D:\My")
Text1Str = Text1Str & CRLF & PathProof("D:\")
Textbox1.Text = Text1Str
End Sub
Sub PathProof(Path)
Proof() = StrSplit(Path, "\")
'Return ArrayLen(Proof())
If Proof(1) = "" Then
Return Path
Else
Return Path & "\"
End If
End Sub

I hope it helps.
best regards
berndgoedecke
 

specci48

Well-Known Member
Licensed User
Longtime User
Hi jothis,

just test your result with DirExist or FileExist, e.g.:
B4X:
path = TextBox1.Text
If DirExist(path) Then
   path = path & "\"
End If


specci48
 

Discorez

Member
Licensed User
Longtime User
Other variant

B4X:
'Always returns value with backslash
Sub AddSlash(sPath) As String
  If StrAt (sPath, StrLength(sPath)-1) = "\" Then Return sPath  Else  Return sPath & "\"
End Sub

'Always returns value without backslash
Sub RemSlash(sPath) As String
  If StrAt (sPath, StrLength(sPath)-1) = "\" Then  Return StrRemove(sPath, StrLength(sPath)-1 ,1)  Else  Return sPath
End Sub
 
Last edited:

jothis

Active Member
Licensed User
Thankyou

Hi All

Thank you For Helping Me. berndgoedecke It Works Fine For me.Thankyou For That.
I Tried Eg Of specci48 But I Got Some Error

Eg:When I Select A Folder It's Work Fine But When I Select A Drive I Got Error Like This:-
selection Of Folder

D:\My --->Output Is D:\My\ It Is Correct

selection Of Drive

D:\ --->Output Is D:\\ It Is inCorrect

Any Way Thank you For Helping Me
Jothis
:sign0060::sign0060:
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…