delete or format sd card

fanfalveto

Active Member
Licensed User
Longtime User
please,i know how delete a single file,but how i can delete all (folders and files from sd card) at the same time?
i try with
B4X:
Dim allsdcard As List
allsdcard.Initialize
allsdcard.AddAll(File.ListFiles(File.DirRootExternal))
For i=0 To allsdcard.Size-1
 File.Delete(File.DirRootExternal,allsdcard )
Next
but nothing with this.
thank you
 

Mahares

Expert
Licensed User
Longtime User
Try this code. It should do it:
B4X:
Dim allsdcard As List
Dim MyFile As String
Dim i as Int
allsdcard.Initialize
allsdcard.AddAll(File.ListFiles(File.DirRootExternal))
For i= allsdcard.Size-1 To 0 Step -1
 MyFile = allsdcard.Get(i)
 File.Delete(File.DirRootExternal,MyFile)
Next
Be careful. On some devices DirRootExternal is not necessarily the SD card!
 
Last edited:
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
i do
B4X:
Dim archivos As List
archivos.Initialize
archivos.AddAll(File.ListFiles(File.DirRootExternal))
For i=0 To archivos.Size-1
 File.Delete(File.DirRootExternal,archivos.Get(i))
Next
and delete files but not folders and files in foders.
your code do the same delete files but not folders
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If a folder contains files, the files must be deleted first before the folder is deleted. Therefore, you can use the rootine to delete the files in each folder, then you can delete the empty folders. You can use File.IsDirectory to check for folders. I did not test this part here because I do not want to risk deleting all my files and data.
 
Last edited:
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
Ok,i undestand you,now i try to search how to delete the files into a folder,then how to delete the folder.
Thank you
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You may need something like this to give you an idea. I have not tested it because I do not want to risk losing all my files and data. Also, be extremely careful when deleting files and folders as in some devices File.DirRootExternal is not necessarily the Sd card. Also when deleting you need to do this: For i= allsdcard.Size-1 To 0 Step -1 instead of For i=0 To allsdcard.Size-1

B4X:
Dim allsdcard As List
Dim MyFile As String
Dim i,j As Int
allsdcard.Initialize
allsdcard.AddAll(File.ListFiles(File.DirRootExternal))
For i= allsdcard.Size-1 To 0 Step -1
 MyFile = allsdcard.Get(i)
 If  File.IsDirectory(File.DirRootExternal, MyFile)=True Then
    Dim MyfileChild As String
   Dim allsdcardChild As List
   allsdcardChild.initialize
   allsdcardChild.AddAll(File.ListFiles(File.DirRootExternal & "/" & MyFile))
   For j= allsdcardChild.Size-1 To 0 Step -1
     MyfileChild= allsdcardChild.Get(j)
      File.Delete(File.DirRootExternal& "/" & MyFile) ,MyfileChild )
   Next
 End If
 File.Delete(File.DirRootExternal,allsdcard )
Next
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
Thank you Mahares,finally i do that
B4X:
Sub deleteall
Dim sd As String
sd = fs.Sdcard
fs.rmrf(sd)
End Sub
with MLfiles library.
 
Upvote 0
Top