Android Question Strange behaviour from File.listFile

DALB

Active Member
Licensed User
Hello, maybe the last days of this year for asking for help !

With this code:

listFile:
Dim lifi As List
    lifi.Initialize
    lifi.Add(File.ListFiles(File.DirInternal & "/zimaj"))

if I list all the items, I obtain (by msgboxAsync)

[matahari3_548_355_96dpi.PNG, <- note de the bracket [ at the beginning and the comma , at the end
sarahBernhardt1865.PNG, <- note the comma , at the end
... the list continues and at the end...
maudAdams1897.PNG] <- note de the bracket ] at the end

Because of these signs '[' , "," and "]" B4A doesn't recognize the name of the files.
How is it possible ?

My target is only to clear the folder containing these files.

Thanks much for the brains coming here!
 

LucaMs

Expert
Licensed User
Longtime User
You are viewing the entire List object (try a Log (lifi) and you will see the same thing).

You should scan the list:
B4X:
For Each FileName As String In lifi
    log(FileName)
Next
 
Upvote 0

DALB

Active Member
Licensed User
LucaMs, yes the list is like an array.

Erel, it's exactly what I wrote in my code, but the result is like in post #1

lifi:
Private Sub lblviderZimaj_Click
 
    Msgbox2Async("Confirm cleaning all the folder","cleaning folder 'zimaj'", _
    "yes","","no",Null,False)
    wait for msgbox_result(res As Int)
    If res=DialogResponse.NEGATIVE Then Return
    'effacement de toutes les images / clearing all the lines
    Dim lifi As List
    lifi.Initialize
  lifi.Add(File.ListFiles(File.DirInternal & "/zimaj"))

    For u=0 To lifi.Size-1
        File.Delete(File.DirInternal & "/zimaj",lifi.Get(0))
        lifi.RemoveAt(0)
    Next

    If lifi.Size=0 Then
        ToastMessageShow("Folder 'zimaj' is empty !",False)
    Else
        MsgboxAsync("folder zimaj can't be cleared :" & CRLF & _
        lifi.Size & " remain.","ERREUR")
        wait for msgbox_result
        Dim liste As String
        For u=0 To lifi.Size-1
            liste=liste & CRLF & lifi.Get(u)
        Next
        MsgboxAsync("the remaining files are : " & CRLF & liste,"list of non wiped files")
    End If
 
End Sub

Have I forgotten something ?
 
Upvote 0

DALB

Active Member
Licensed User
LucaMs, if I use you code, the app crashes.
If I do as Erel wrote, I have the problem seen in post #1

in other codes(subs) in the same sheet, it works !!!!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
LucaMs, if I use you code, the app crashes.
What is the error message???


My target is only to clear the folder containing these files.
Try this (my) class:
https://www.b4x.com/android/forum/threads/class-clsfileslists.39559/

Once added it to your project, your code should be something like (not tested):
B4X:
Dim FileLists As clsFilesLists
FileLists.Initialize

FileLists.DeleteFiles(File.DirInternal & "/zimaj", "*.*")
Make sure that folder exists.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
File.ListFiles returns an array.
1640771861399.png


So
lifi = ListFiles(...)
will work (although it would not be necessary, you could directly use the List returned by the method).
 
Upvote 0

DALB

Active Member
Licensed User
LucasMs, the apps crashed without any message.

Erel, ysterday, it doesn't work, I restarted the app, did the cleaning process. Now it goes well.
I expect that it can come again, regarding to the fact that it appeared one time..

Thanks you you two for you advices ! today, it works !
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
LucasMs, the apps crashed without any message.
Debug mode?


B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    File.WriteString(File.DirInternal, "File1.txt", "First file")
    File.WriteString(File.DirInternal, "File2.txt", "Second file")
    File.WriteString(File.DirInternal, "File3.txt", "Third file")
    LogFiles
    
    For Each FileName As String In File.ListFiles(File.DirInternal)
        File.Delete(File.DirInternal, FileName)
    Next
    LogFiles
    
End Sub

Private Sub LogFiles
    Log("Files in DirInternal: " & File.ListFiles(File.DirInternal).Size)
    For Each FileName As String In File.ListFiles(File.DirInternal)
        Log(FileName)
    Next
End Sub
 
Upvote 0
Top