File.Delete not working

noclass1980

Active Member
Licensed User
Longtime User
Hi, I create a temporary file at run-time which I want to delete on exit. I catch the back button event in the code below. If the user confirms the exit (pressing "Yes"), I want to delete the temporary file. Pressing "No" returns the user to the current page. However, when "Yes" is pressed, the app main page is displayed as expected but the file is still present in the sub folder. Can anyone tell me why and what I should do? Thanks.

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
MyTable.SaveTableToCSV(File.DirRootExternal & "/abc/def/" ,"TempTable.csv")
closeCounter = closeCounter + 1
Log("closeCounter = " & closeCounter)
If closeCounter < 2 Then
If Msgbox2("Do you really want to exit", "Exit", "Yes", "No", "", Null)=DialogResponse.Cancel Then
closeCounter = 0
Log("closeCounter is = " & closeCounter)
Return True
Else
closeCounter = 0
If File.Delete(File.DirRootExternal, "/abc/def/TempTable.csv")=True Then
ToastMessageShow("file deleted", True)
End If
Return False
End If
End If
End If
End Sub
 

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,
i found out the same problem; a file, which was just in use, was not deleted without any error. So i tried it with a small time between closing the file and the delete and it works. But the better way is, to look "behind", why this could be.

Greetings
Reinhard
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Thanks but how did you close the file? I just added a do until loop of 2 seconds to see it that made a difference - it didn't. I can workaround by not deleting the temp file on exit but it's neater if I can.
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
In my example i had opened the file for sending with the smtp client. Because the sending process is a background task, i is clear, that anybody can not delete a file, which is in the sending process. In your code you use the routine 'SaveTableToCSV' which writes the file. Maybe this process is not really ready at this time?
Greetings
Reinhard
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Tablecode

Hi, here is the SaveTableToCSV code from the Table Class

B4X:
'Saves the table to a CSV file.
Public Sub SaveTableToCSV(Dir As String, Filename As String)
'If File.Exists(Dir, Filename, ",", Data, headers)=False Then
   Dim headers(NumberOfColumns) As String
   For i = 0 To headers.Length - 1
      Dim l As Label
      l = Header.GetView(i)
      headers(i) = l.Text
   Next
   StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)
End Sub
The reason I want to delete the temporary file is so the user sees an empty table when starting the app. I use the temp file to save the table data so it can be reloaded on Activity_Resume (used after an orientation changed when going from the table panel to the home panel. I'm surprised that I don't get an error message when trying to delete the file.
Ah, just had a thought. I could clear the table using the clearall sub in the table module and then that would load a blank table at the start. That should work shouldn't it?
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Saving empty table didn't work

Ok, from my last post, clearing the table and saving didn't work as the line
B4X:
StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)
failed because the Data is an empty array. Any suggestions for a workaround? I could add a dummy data like
B4X:
For i = 1 To 8
   MyTable.AddRow(Array As String("", "", "", "", "", "", "","","","","","",""))
Next

but is this the best approach?

Ok, tried the dummy rows as above and it works the way I want it too but just means that I have a "permanent" temporary file. If anyone can suggest an alternative, please do! Thanks

Thanks
 
Last edited:
Upvote 0

a_carignan

Member
Licensed User
Longtime User
I had the same problem with File.Delete. After some testing, I've realized that the file read or copy does not seem to close in activity, which prevents deletion of the file. So I put the order and file.readlist dile.delete in a module and the file is removed. ;)
 
Upvote 0
Top