B4R Question Deleting file on SD card...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
How to delete an existing file on SD card? I was unable to find a method to do the same.
 

Cableguy

Expert
Licensed User
Longtime User
From the arduino SD lib, there should be a remove method, which will delete the passed file...
Since I haven't yet tried my SD shield, I do not know if this method is present in the B4R library
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
The remove method is not exposed . Can anyone please do it? I am stuck because of that , in the middle of an urgent project.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear cableguy,
I have literally no knowledge about C . On following the example I land up with error for conversion to string.
@Erel pls help.
I tried the following:
B4X:
 RunNative("fileremove",filename)

B4X:
#if C
#include <SD.h>
void fileremove(B4R::Object* o) {
     SD.remove(b4r_main::_filename);
 
}
#End if
 
Last edited:
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
Thanks for the quick update, but on issuing the command the controller resets , the logs are as follows:
B4X:
AppStart
Connected: 1
Connecting to NIST server...
ets Jan  8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1264, room 16
tail 0
chksum 0x0f
csum 0x0f
~ld
I am using WeMos Mini.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Tested:
The remove command crashes the microcontroller (WeMos mini board) even after testing the condition whether the file exists or not.
Is there a way to overwrite the existing file , as a workaround for deleting the file? In my case the new data is appended to the existing file which is undesirable.

Found a workaround!
I can use sd.Position=value , to start from beginning of the file value=0 and increment value to the number of bytes written!
 
Last edited:
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
Even though I could get away with the problem, it is important to investigate as to why ESP8266 crashes because of the specific command. Please do so whenever you get time. We want to make sure our B4X is as bug free as possible !
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
Dear Erel,
Even though I could get away with the problem, it is important to investigate as to why ESP8266 crashes because of the specific command. Please do so whenever you get time. We want to make sure our B4X is as bug free as possible !

Hello,
(old post I know ....)
I am stuck a similar point.
Using NodeMCU V1.0 and CatalexV1.0 SD card shield.

I can init the card OK
I can openwrite OK
I can actually write to a file OK
I can close the file
I can list the files on the card (and see their size and file/directory status)

I cannot delete (any) file
I cannot openreadwrite (therefore cannot read from file)
I cannot sd.exists(filename)

Any of these last three operations causes the NodemCU to crash immediately, and start dumping hex into the log ...
The only way I have to recover from this, is to comment out the offending line(s) and recompile/load ....

....
Somewhat puzzled .....
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
I'm waiting for a new SD card shield to arrive. Will test it then.
Thanks Erel,

I'm posting my test code below, with probem line commented out.
  • As it is, it writes to file NEW1.txt.
  • With the sd.exist(fname) line uncommented, the NodeMCU crashes and dumps.

I have noticed too, that an error is displayed in the initial compiler messages (error in b4r_main.ccp) that looks like the variable for StackMemory is unused.
I think this is unrelated.... but here is the error message anyway.
"C:\android\B4R_PR~1\TEst1\Objects\bin\sketch\b4r_main.cpp:57:12: warning: unused variable 'cp' [-Wunused-variable] const UInt cp = B4R::StackMemory::cp;"

I am hoping I am just using sd.exists incorrectly...

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim SD As SD
    Dim GPIO4 = 4 As Int
    Dim append = True As Boolean
    Dim overwrite = False As Boolean
    Dim BC As ByteConverter
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    SD.Initialize(GPIO4)
    Log("SD initialised")
   
    Log("Test SDwrite..................")
    For a = 1 To 5
        Log("---------------- test ", a, " ------------------------")
        SDlist
        Log("Writing to file")
        SDwrite("NEW1.TXT", "testdata",append)
    Next
   
   
    Log("Test App finnishes ...")
End Sub


Sub SDlist
    Log("SDlist here")
    Dim a As Int
    a= a+1
    For Each f As File In SD.ListFiles("/")
        Log(a, "  Name: ", f.Name, ", Size: ", f.Size)
    Next
End Sub


Sub SDwrite(fname As String, data As String, Append_flag As Boolean)
    Dim buffer() As Byte
    Log("SDwrite here")
    Log(" - file - ", fname)
    Log(" - data=", data)
    Log(" - Append = ", Append_flag)
    Dim OK As Boolean
    Log(" - Test exists file ")
    ' -----------------------------------------------
    ' The following line causes NodeMCU to crash !!!!
''''''    OK = SD.Exists(fname)
''''''        report(OK)
    'now open the file, append a line of text and then close it ..    
    OK = SD.OpenReadWrite(fname)
    report(OK)
    If Append_flag Then SD.Position = SD.CurrentFile .Size Else SD.Position=0
    buffer = BC.StringToBytes(data)
        SD.Stream.WriteBytes(buffer,0,buffer.Length)
    buffer = BC.StringToBytes(CRLF)
        SD.Stream.WriteBytes(buffer,0,buffer.Length)          
    Log("closing file")
    SD.Close
End Sub

Sub report(value As Boolean)
    If value Then Log("REPORT:Success") Else Log("REPORT:FAILED!")
End Sub
 
Upvote 0

wcieslik

Member
Licensed User
Longtime User
Just updating on where I'm at with my testing ...
The only two things I can't get to work are :
  • SD.EXISTS
  • SD.REMOVE
Either of these cause the NodeMCU to crash ....

I can work around SD.EXISTS by modifying the SDLIST example, but removing a file has me defeated ...



sd.exists test

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim SD As SD
    Dim GPIO4 = 4 As Int
    Dim FoundInList As Boolean
   
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    FoundInList=False
       
    If SD.Initialize(GPIO4) Then 
        Log("SD initialised")
    Else
        Log("SD failed")
    End If   

    FoundInList = SDlist("NEW1.TXT")
    Log("My foundinlist = ", FoundInList)
   
    Log("test sd.exists")
    If SD.Exists("NEW1.TXT") Then
        Log("file exists")
    Else
        Log("file not found")
    End If
   
    Log("Test App finnishes ...")
End Sub

Sub SDlist(name As String) As Boolean
    Log("SDlist start ----------------------")
    Dim a As Int
    Dim R=False As Boolean
   
    For Each f As File In SD.ListFiles("/")
        a= a+1
        Log(a, "  Name: ", f.Name, ", Size: ", f.Size)
        If f.Name = name Then R = True
    Next
    Log("SDlist end ----------------------")
    Return R
End Sub



sd.remove test
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Dim SD As SD
    Dim GPIO4 = 4 As Int
    Dim BC As ByteConverter
    Dim LF() As Byte = Array As Byte(10)
    Dim CR() As Byte = Array As Byte(13)
End Sub

Private Sub AppStart
    Dim test As Int
    Dim fname="JUNK.TXT" As String
    Serial1.Initialize(115200)
    Log("AppStart")
   
    SD.Initialize(GPIO4)
    Log("SD initialised")
    Log("[1] No file added yet")
    SDlist(fname)
    Log("[2] Adding file junk.txt")
    test = SDwrite(fname, "testdata-1",True)
    test = SDwrite(fname, "testdata-2",True)
    test = SDwrite(fname, "testdata-3",True)
    SDlist(fname)
    Log("[3] deleteing :", fname)
    'SD.Remove(fname)
    SDlist(fname)
    Log("Test App finnishes ...")
End Sub

Sub SDlist(name As String)
    Log("SDlist start ----------------------")
    Dim a As Int
    Dim str As String
   
    For Each f As File In SD.ListFiles("/")
        a= a+1
        str = ""
        If f.Name = name Then str = " <- found!"
        Log(a, "  Name: ", f.Name, ", Size: ", f.Size, str)
    Next
    Log("SDlist end ----------------------")
End Sub

Sub SDwrite(fname As String, data As String, Append As Boolean) As Int
    Dim buffer() As Byte
    Dim result As Int
    Dim OK As Boolean
    Dim N As Int
    result = 0
   
    OK = SD.OpenReadWrite(fname)
    If Append Then SD.Position = SD.CurrentFile .Size Else SD.Position=0
    buffer = BC.StringToBytes(data)
        n=SD.Stream.WriteBytes(buffer,0,buffer.Length)
        If n <> data.Length Then result = 2
    buffer = BC.StringToBytes(CRLF)
        n=SD.Stream.WriteBytes(buffer,0,buffer.Length)
        If n <> data.Length Then result = 3
    SD.Close
    Return result
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Phew @wcieslik, I thought that I was the only one who had an issue with SD.REMOVE...

********************* PROGRAM STARTING ****************
AppStart
Exception (3):
epc1=0x401003e9 epc2=0x00000000 epc3=0x00000000 excvaddr=0x4005be6d depc=0x00000000
ctx: sys
sp: 3ffee630 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffee7d0: 00000000 00000000 00000000 00000000
3ffee7e0: 00000000 00000000 000017de 3ffee7ec
3ffee7f0: 00000010 3ffee880 00000000 4010053d
3ffee800: 00000000 00000001 00000000 3ffee7ec
3ffee810: 00000010 3ffee8a0 00000000 3ffee8c4
3ffee820: 00000010 3ffee880 3ffee880 40204100
3ffee830: 3fff13e4 0000000c 3ffee880 4020414f
3ffee840: 00000010 3ffee8a0 3ffee880 40204185
3ffee850: 3fffdad0 00000000 3ffee880 402041d6
3ffee860: 3fff13e4 3ffee8c0 3ffee8a0 40204185
3ffee870: 3fffdad0 00000000 3ffee4b4 40202142
3ffee880: 00000000 00000000 00000000 40204185
3ffee890: 3fffdad0 00000000 3ffee4b4 40202149
3ffee8a0: 3fff13e4 0000000f 0000000c 40204185
3ffee8b0: 3fffdad0 00000000 3ffee4b4 40202149
3ffee8c0: 3fff13cc 0000000f 0000000c 40204185
3ffee8d0: 3fffdad0 00000000 3ffee4b4 40202149
3ffee8e0: 3fff13b4 0000000f 0000000c 40204185
3ffee8f0: 3fffdad0 00000000 3ffee4b4 40202149
3ffee900: 3fff139c 0000000f 0000000c 40204185
3ffee910: 3fffdad0 00000000 3ffee4b4 40202149
3ffee920: 3fff1384 0000000f 0000000c 40204185

Etc etc etc...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I think that I know why it happens. It is related to a bug fixed in the standard SDK which wasn't fixed in the ESP8266 version: https://github.com/arduino/Arduino/issues/3999

Please copy the attached rSD.cpp file to rSD folder and test it (overwrite the existing file).
 

Attachments

  • rSD.zip
    821 bytes · Views: 411
Upvote 0

wcieslik

Member
Licensed User
Longtime User
I think that I know why it happens. It is related to a bug fixed in the standard SDK which wasn't fixed in the ESP8266 version: https://github.com/arduino/Arduino/issues/3999

Please copy the attached rSD.cpp file to rSD folder and test it (overwrite the existing file).

That's terrific Erel.
Thank you very much !
I tested both functions and found both to be working fine with the new rSD.cpp.

I found I was having the same (or very similar) issue using the Arduino IDE...
If I called the functions with the filename as a variable, I had the same crash. eg:
B4X:
'arduino IDE
String FILENAME = "test.txt";
SD.exists(FILENAME);

but if called with the filename implicitly, works ok. eg:
B4X:
'arduino IDE
SD.exists("test.txt");

Might be a similar omission in the Arduino SD.cpp ?

Oh yes ... Just followed the link you supplied ... Had been there myself but really didn't understand the implications of the change ... Makes more sense now.
Looks like I can fix the Arduino libs too ! :)

Thanks again Erel, very much appreciated.
 
Last edited:
Upvote 0
Top