B4R Question Delete all files in folder

Mostez

Active Member
Licensed User
Hello,
I want to delete all files in folder, I use Arduino Mega2560 and SD card module, I've tried for-each loop but number of files should be less than 30 in folder! I've increased stack buffer size from 1500 to 2000 but it made things go worse. Any ideas how to do that?

B4X:
private Sub DeleteFilesInFolder(FolderName As String) As String
    If Not(SD.Exists(FolderName)) Then
        Return ERR_NOT_FOUND
    End If
   
    LCD.Clear
    PutString(0,1,"Please Wait...",True)
    Dim MaxF As Byte = 0
   
    For Each f As File In SD.ListFiles(FolderName)
        'Log(GetFullFileName(f.Name,FolderName))
        SD.Remove(GetFullFileName(f.Name,FolderName))
        MaxF = MaxF + 1
        If MaxF > 30 Then 'stack overflow if number of files is > 30
            LCD.Clear
            Return ERR_MAX_STACK_OR_BUFFER_SIZE
        End If
    Next
   
    LCD.Clear
    Return True
End Sub
 
Private Sub GetFullFileName(FileName As String, FolderName As String) As String
    Dim FullFileName As String
    Private raf As RandomAccessFile
    Dim FileNameArr (FileName.Length + FolderName.Length + 1) As Byte
    raf.Initialize(FileNameArr, True)
    raf.WriteBytes(FolderName, 0, FolderName.Length, raf.CurrentPosition)
    raf.WriteBytes("/", 0, 1, raf.CurrentPosition)
    raf.WriteBytes(FileName, 0, FileName.Length, raf.CurrentPosition)
    FullFileName = BC.StringFromBytes(FileNameArr)
    Return FullFileName
End Sub
Thanks
 
Last edited:

Mostez

Active Member
Licensed User
I used this code, but is did not remove any files(f.name returns filename without folder name), I moved GetFullFileName code inside for-each loop, files deleted but still have stack problem.

B4X:
private Sub DeleteFilesInFolder(FolderName As String) As String
    If Not(SD.Exists(FolderName)) Then
        Return ERR_NOT_FOUND
    End If
    
    LCD.Clear
    PutString(0,1,"Please Wait...",True)
    
    For Each f As File In SD.ListFiles(FolderName)
        
        SD.Remove(f.name)
        
    Next
    
    LCD.Clear
    Return True
End Sub
 

Erel

Administrator
Staff member
Licensed User
B4X:
private Sub DeleteFilesInFolder(FolderName As String) As String
    If Not(SD.Exists(FolderName)) Then
        Return ERR_NOT_FOUND
    End If
    
    LCD.Clear
    PutString(0,1,"Please Wait...",True)
    
    For Each f As File In SD.ListFiles(FolderName)
        
        GetFullFileName(f.Name,FolderName)
        
    Next
    
    LCD.Clear
    Return True
End Sub

Private Sub GetFullFileName(FileName As String, FolderName As String) 
    Dim FullFileName As String
    Private raf As RandomAccessFile
    Dim FileNameArr (FileName.Length + FolderName.Length + 1) As Byte
    raf.Initialize(FileNameArr, True)
    raf.WriteBytes(FolderName, 0, FolderName.Length, raf.CurrentPosition)
    raf.WriteBytes("/", 0, 1, raf.CurrentPosition)
    raf.WriteBytes(FileName, 0, FileName.Length, raf.CurrentPosition)
    FullFileName = BC.StringFromBytes(FileNameArr)
   SD.Remove(FullFileName)
End Sub
 
Top