B4R Question SD read files in folder

Mostez

Well-Known Member
Licensed User
hello,
I want to read number of files one after one in SD folder, i use rSD list files to get files list then read files, the problem is when joining folder name with file name to get full path, MEGA starts over and some times timer stop working. for testing purpose i hard-coded file name then everything worked OK

file name is like '01234567.DAT' folder name like 'dat' i want to get 'dat/01234567.DAT' i tried join strings but it did not work too. any ideas


B4X:
private Sub UploadAllUserAccounts(SendFileInfo As Boolean,StreamSource As AsyncStreams) As Boolean
    Dim Size As ULong = 0
    SetSD(ON)
    LCD.Clear
    PutString(0,1,"Please Wait...",True)
    If SendFileInfo Then
        For Each Fl As File In SD.ListFiles("dat") ' get total file size in folder
            Size = Size + 66 + 2 'only first four fields
        Next
        'send file info only for once, upload user files as stream of ONE united file
        Dim Tmp(18) As Byte
        Dim RAF As RandomAccessFile
        
        RAF.Initialize(Tmp,False)
        RAF.WriteByte(0x02,RAF.CurrentPosition)
        RAF.WriteBytes("USERS.DB".GetBytes,0,8,RAF.CurrentPosition)
        RAF.WriteByte(SYS_DATA_FILES_DELIMITION_CHR,RAF.CurrentPosition)
        RAF.WriteBytes(NumberFormat(Size,8,0).GetBytes,0,8,RAF.CurrentPosition) 'file size
        SetSD(OFF)
        SendStreamMessage(Tmp,Tmp.Length,True,StreamSource)
        SetSD(ON)
        
    End If
    
    'Dim cb(101) As Byte
    Dim cb(66) As Byte
    For Each f As File In SD.ListFiles("dat")
        If SD.OpenRead(GetUserAccountFileName(f.Name)) = False  Then 'GetUserAccountFileName is the problem
            LCD.Clear
            Return False
        End If
        'SD.Stream.ReadBytes(cb,0,101) 'read 101 bytes
        SD.Stream.ReadBytes(cb,0,66)
        SD.Stream.Flush
        Delay(10)
        SD.close
        SetSD (OFF)
        SendStreamMessage(cb,cb.Length,True,StreamSource)
        SetSD (ON)
    Next
    LCD.Clear
    SetSD (OFF)
    SendStreamMessage(Array As Byte (0x04),1,True,StreamSource)'end of transmission
    'SetSD (True)
    Return True
    
End Sub

B4X:
private Sub GetUserFileName(ItemID As String) As String
    Dim filename As String
    Private raf As RandomAccessFile
    Dim FileNameArr (16) As Byte
    Dim filename As String
    raf.Initialize(FileNameArr, True)
    raf.WriteBytes("dat/", 0, 4, raf.CurrentPosition)
    'raf.WriteBytes(Str.LeadChar(ItemID,Asc("0"),8), 0, 8, raf.CurrentPosition) 'leading zeros
    raf.WriteBytes(NumberFormat(ItemID,8,0), 0, 8, raf.CurrentPosition) 'leading zeros
    raf.WriteBytes(".DAT", 0, 4, raf.CurrentPosition)
    filename = BC.StringFromBytes(FileNameArr)
    Return filename
End Sub
 

Mostez

Well-Known Member
Licensed User
the purpose of GetUserFileName is to compose full file name of user-data-file, i pass filename i.e. 01234567 and it returns 'dat/01234567.dat' , stack buffer size is 1500.
 
Upvote 0

Mostez

Well-Known Member
Licensed User
it works OK now :), many thanks Erel
will it work OK too with whatever number of files say 400 or so?
 
Upvote 0
Top