B4R Question Problem deleting files with LittleFS

RJB

Active Member
Licensed User
Longtime User
Using LittleFs/ Esp32.

Trying to clear a directory with:
B4X:
Dim fs As LittleFS  'in Process_Globals

   For Each f As File In fs.ListFiles("/")
        fs.Remove(f.Name)
   Next
does not delete the files and gives the error like:
E (29523) esp_littlefs: Failed to unlink path "/Image30.jpg". Has open FD.
for each file.
Am I going something wrong? Or is this a bug? Or.....?

This code works but will have problems if there are more than 50 (or whatever value is set) files:
B4X:
    Dim FileString(50) As String
    Dim i As Int = 0
    For Each f As File In fs.ListFiles("/")
        FileString(i) = BC.StringFromBytes(f.name)
        i = i + 1
    Next

    For n = 0 To i -1
        fs.Remove(FileString(n))
    Next

Modifying it to the following partially works but one delete fails at each exit/ loop so isn't useable:

B4X:
    Dim FileString(5) As String
    Dim i As Int = FileString.Length
    Do While i = FileString.length
    i = 0
    For Each f As File In fs.ListFiles("/")
        FileString(i) = BC.StringFromBytes(f.name)
        i = i + 1
        If i > 4 Then Exit
    Next

    For n = 0 To i -1
        fs.Remove(FileString(n))
    Next
    Loop

Is there a better way?
Thanks
 

candide

Active Member
Licensed User
with a google search on "esp_littlefs: Failed to unlink path Has open FD" you will see similar issues found under arduino. (files are seen open at low level)

possible this one will help :
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Thanks. I had seen that.
It seems to be suggesting that you close the file but there doesn't seem to be a way to do that in B4r.
FS.close doesn't do it and there doesn't appear to be a way to close a file by name.
Am I wrong?
 
Upvote 0

candide

Active Member
Licensed User
yes, it seems we have to close each file before removal.

can you try something like that :
B4X:
Dim fs As LittleFS  'in Process_Globals

   For Each f As File In fs.ListFiles("/")
        fs.OpenRead(f.Name)
        fs.close
        fs.Remove(f.Name)
   Next
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
No, unfortunately that still gives the error per delete
Thanks for the help anyway.
 
Upvote 0

candide

Active Member
Licensed User
problem is with File.OpenNextFile on esp32, it is blocking the file in open state

code below seems working but it is clean to use joinString
B4X:
Dim Lname As String
    For Each f As File In lfs.ListFiles("/")
        If Lname.Length <> 0 Then lfs.Remove(Lname)
        Lname = JoinStrings(Array As String("/",f.name))
    Next
    If Lname.Length <> 0 Then lfs.Remove(Lname)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…