Trying to delete a directory

jdiperla

Member
Licensed User
Longtime User
As the title suggests. The instructions indicated tell me that this should work, but its not:

B4X:
Sub gUnIn_Click()
If File.Delete(File.DirRootExternal & "/Lock/" & Main.lockdir & "/", "") = True Then

Msgbox("lock deleted", "")
Activity.Finish
End If
End Sub
 

Mahares

Expert
Licensed User
Longtime User
You will need to delete all the files inside the folder before you can delete the folder. This works if the folder is empty:
B4X:
If File.Delete(File.DirRootExternal & "/jdiperla", "") = True Then
   Msgbox("folder deleted", "")
Else
   Msgbox("NOT deleted", "")
End If
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Is there a function that would delete the contents of the directory easily? Or am I going to have to use a loop of some sort?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can easily loop through the files and delete each programmatically like this:
B4X:
Dim MyList As List
MyList.initialize
Dim MyFile As String
MyList=File.ListFiles(File.DirRootExternal & "/perla")
For i= MyList.Size-1 To 0 Step -1
   MyFile=MyList.Get(i)
   File.Delete(File.DirRootExternal & "/perla",MyFile)
Next
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Cool Thanks. That works. Lol. I discovered MLFiles which simplifies the process, but I just tried yours as well. Both work well. Thanks again for your help!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@jdiperla: Could you please share your MLfiles code that is equivalent to mine to give us all different options, particularly since you think it is simpler.
Thanks
 
Upvote 0

jdiperla

Member
Licensed User
Longtime User
Sure! Just keep in mind you also need the smb/CIFS library installed alongside MLFiles. Here is my code:

B4X:
dim mlhandle as mlfiles
If mlhandle.rmrf(File.DirRootExternal  "/lock/") = True Then
Msgbox("lock has been uninstalled", "")
Activity.Finish
Else
Msgbox ("The lock has not been uninstalled","")
End If
 
Upvote 0

francoisg

Active Member
Licensed User
Longtime User
Hi, modified Mahares's code a bit to allow for auto sub-folder deletes as well.

Sub CheckPath (Path As String) As String
If Path.EndsWith("/") Then
Return Path
Else
Return Path & "/"
End If
End Sub

Sub ClearPath (Path As String, RemovePath As Boolean)
Dim fileList As List
fileList.initialize
fileList = File.ListFiles(Path)
For i=fileList.Size-1 To 0 Step -1
Dim fileName As String = fileList.Get(i)
If File.IsDirectory(Path, fileName) Then ClearPath(CheckPath (Path) & fileName, True) ' *** Clear contained directory 1'st ...
File.Delete(Path, fileName)
Next
' *** Remove folder if specified ...
If RemovePath Then
If Path.EndsWith("/") Then Path = Path.SubString2(0, Path.Length-1)
Dim p As Int = Path.LastIndexOf("/")
Dim dName As String = Path.SubString(p + 1)
Path = Path.SubString2(0, p + 1)
File.Delete(Path, dName)
End If
End Sub
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, modified Mahares's code a bit to allow for auto sub-folder deletes as well.

Sub CheckPath (Path As String) As String
If Path.EndsWith("/") Then
Return Path
Else
Return Path & "/"
End If
End Sub

Sub ClearPath (Path As String, RemovePath As Boolean)
Dim fileList As List
fileList.initialize
fileList = File.ListFiles(Path)
For i=fileList.Size-1 To 0 Step -1
Dim fileName As String = fileList.Get(i)
If File.IsDirectory(Path, fileName) Then ClearPath(CheckPath (Path) & fileName, True) ' *** Clear contained directory 1'st ...
File.Delete(Path, fileName)
Next
' *** Remove folder if specified ...
If RemovePath Then
If Path.EndsWith("/") Then Path = Path.SubString2(0, Path.Length-1)
Dim p As Int = Path.LastIndexOf("/")
Dim dName As String = Path.SubString(p + 1)
Path = Path.SubString2(0, p + 1)
File.Delete(Path, dName)
End If
End Sub

Hi, I use this to delete a large folder and sub directories, I don't get any errors, but it can take a long time, and everything in the app freeze's until its complete this can take upto 5 minutes! is there a way I can make this run without holding up the queue?

*****EDIT I have now inserted the DoEvents Command and my UI updates but stutters quite bad! If I could make it smooth It would be great!

Thanks

Aidy
 
Last edited:
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
You can use the code I posted here to recursively delete a folder: https://www.b4x.com/android/forum/threads/folder-remove.57304/#post-360773

I'm not sure whether it will be faster or not.

Thank you for that, its not the speed, thats the problem I have replaced the sub with the one you suggested, and added a DoEvents in the loop, its still quite jittery. Im wondering if I can either get the OS to do the deletion, or maybe some background service, the directory I want to delete can vary in size from 100meg to 2-3Gbs! as I said its not the speed, I would just like to show the user something is happening, rather than just say wait and display a jittery animation.

Thanks

Aidy
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the directory I want to delete can vary in size from 100meg to 2-3Gbs! as I said its not the speed, I would just like to show the user something is happening
Have you tried to use it in a service OR the threading library?
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Have you tried to use it in a service OR the threading library?

I haven't tried this but I will look now, i have never tried the modules as they are a bit above me, but im going to give it ago.

Thanks for the pointer.

Aidy
 
Upvote 0
Top