B4R Question SD read files in folder

Discussion in 'B4R Questions' started by Mostez, Jul 16, 2018.

  1. Mostez

    Mostez Active Member

    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


    Code:
    private Sub UploadAllUserAccounts(SendFileInfo As Boolean,StreamSource As AsyncStreamsAs 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(18As 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(66As 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
    Code:
    private Sub GetUserFileName(ItemID As StringAs String
        
    Dim filename As String
        
    Private raf As RandomAccessFile
        
    Dim FileNameArr (16As Byte
        
    Dim filename As String
        raf.Initialize(FileNameArr, 
    True)
        raf.WriteBytes(
    "dat/"04, raf.CurrentPosition)
        
    'raf.WriteBytes(Str.LeadChar(ItemID,Asc("0"),8), 0, 8, raf.CurrentPosition) 'leading zeros
        raf.WriteBytes(NumberFormat(ItemID,8,0), 08, raf.CurrentPosition) 'leading zeros
        raf.WriteBytes(".DAT"04, raf.CurrentPosition)
        filename = BC.StringFromBytes(FileNameArr)
        
    Return filename
    End Sub
     
  2. tigrot

    tigrot Well-Known Member Licensed User

    You must use wild char like *.
    Change current directory to the one containing the files. Then use "*.dat" to show all files.
     
    Peter Simpson likes this.
  3. Mostez

    Mostez Active Member

    would you please post simple example code?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    It is a stack overflow error. What is the purpose of GetUserFileName?

    What is the size of #StackBufferSize?
     
  5. Mostez

    Mostez Active Member

    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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Move the code inside the loop to a different sub. This way the stack could be unwinded every iteration.
     
  7. Mostez

    Mostez Active Member

    it works OK now :), many thanks Erel
    will it work OK too with whatever number of files say 400 or so?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    I cannot say without testing it. You can check the output of Log(StackBufferUsage) inside the loop. See how much it increases each loop to understand the limit.
     
  9. Mostez

    Mostez Active Member

    OK Erel, will check it
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice