Android Question How to get then list of files from subfolder DirAssets

Hi all! Please help the new guy. How to get a list of files in the Dirassets subfolder. I read that this is possible, provided that the characters are lowercase.
On line 630 ---spisfile=File.ListFiles(File.DirAssets & "/" & cur_f.GetString("name_tab"))--- I get an error. - the subfolder is not a folder.
To work in my gallery, I need to copy preset photos to DirInternal. Preset photos are stored in subfolders in DirAssets. I'm grateful for any help. Sorry for the English! Translator

1.jpg
 
opyfileinternal:
Sub copyfileinternal(n As Int) As ResumableSub
    Dim cur_f As Cursor
    Dim spisfile As List
    Dim pathAsset As String
    Dim pathInter As String
    Dim nameNewFile As String
    
    spisfile.Initialize
    cur_f=db.ExecQuery("SELECT * FROM  Mod_ind_tab")
    If cur_f.RowCount>0 Then
        
        For i=0 To cur_f.RowCount-1
            cur_f.Position=i
            spisfile.Clear
            pathAsset=File.DirAssets & "/" & cur_f.GetString("name_tab")
            pathInter=File.DirInternal & "/" & cur_f.GetString("name_tab")
            
                    If File.Exists(File.DirInternal, cur_f.GetString("name_tab")) = False Then
                        File.MakeDir(File.DirInternal, cur_f.GetString("name_tab"))
                    End If
                    
                
            
            spisfile=(File.ListFiles(pathAsset))
            For j=0 To spisfile.Size-1
                nameNewFile=spisfile.Get(j)&"ass"
                Wait for (File.CopyAsync(pathAsset, spisfile.Get(j), pathInter, nameNewFile)) Complete ( Success As Boolean )
                    
            Next
                
        Next
        
    End If
    cur_f.Close
        
    Return 1
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
There are no subfolders in Files tab. The "subfolders" are group of files.
You don't need to read by any level. All the files are stored in Files directory in one level.

B4X:
Wait For (File.ListFilesAsync(File.DirAssets)) Complete (Success As Boolean, Files As List)
For Each f As String In Files
    Log(f)
Next
 
Upvote 0
Thank you very much for your response. But this code does not show what is in the group of files. It shows all files and file group names. Only the name of the group!
Did I understand correctly? It is impossible to get a list of files for a specific group(subfolder)! Thank you
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Did I understand correctly? It is impossible to get a list of files for a specific group(subfolder)!
I assume the exclamation is not to express anger.
My answer is, no, it is not impossible.
There is a creative way. Just not straight forward.

edit: After investigation, it seems it is not possible in B4A without a info file as mentioned by DonManfred.
However, this may be possible in B4J as we still can read the project source file and find out the files under which group.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you want to copy the files from assets to internal into different folders according to group read from a database then here is an example.

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim HasDB As Boolean
    If File.Exists(File.DirInternal, "demo.db") Then
        HasDB = True
    End If
    db.Initialize(File.DirInternal, "demo.db", True)
    If Not(HasDB) Then
        db.ExecNonQuery("CREATE TABLE Mod_ind_tab (group_name TEXT, file_name TEXT)")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (group_name, file_name) VALUES ('tab1', 'file1.txt')")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (group_name, file_name) VALUES ('tab1', 'file2.txt')")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (group_name, file_name) VALUES ('tab2', 'file3.txt')")
    End If
    CheckCopiedFiles
End Sub

Sub CopyFileInternal As ResumableSub
    Try
        Dim rs As ResultSet = db.ExecQuery("SELECT group_name, file_name FROM Mod_ind_tab")
        Do While rs.NextRow
            Dim group As String = rs.GetString("group_name")
            Dim asset As String = rs.GetString("file_name")
        
            File.MakeDir(File.DirInternal, group)

            Dim NewSubFolder As String = File.Combine(File.DirInternal, group)
            Dim NewFileName As String = asset & ".ass"
            File.Copy(File.DirAssets, asset, NewSubFolder, NewFileName)
        Loop
        rs.Close
    Catch
        Log(LastException)
        Return False
    End Try
    Return True
End Sub

B4X:
Before:
[virtual_assets]
  file1.txt
  file2.txt
  file3.txt
  file4.txt
  mainpage.bal
  _assets_map
demo.db
demo.db-journal
 
Done=true

After:
[virtual_assets]
  file1.txt
  file2.txt
  file3.txt
  file4.txt
  mainpage.bal
  _assets_map
demo.db
demo.db-journal
[tab1]
  file1.txt.ass
  file2.txt.ass
[tab2]
  file3.txt.ass
 

Attachments

  • CopyFilesToInternal.zip
    15.6 KB · Views: 104
Upvote 0
I assume the exclamation is not to express anger.
.
I am very grateful to you! When I was at the academy, there were few programming lessons. The lessons were only in Pascl, Basic, Visual Basic. I am an engineer. I found out about B4x a year ago.
B4X gave me many new opportunities and ideas. Now this is my hobby. I'm really grateful to you. I didn’t think that my simple questions could be answered so quickly and specifically. There is a war in my country, B4X is like fresh air to me. Thank you
 
Upvote 0
Если вы хотите скопировать файлы из ресурсов во внутренние в разные папки в соответствии с группой, прочитанной из базы данных, вот пример.

B4X:
Частная подписка B4XPage_Created (Root1 As B4XView)
    Корень = Корень1
    Root.LoadLayout("Главная страница")
    Dim HasDB как логическое значение
    Если File.Exists(File.DirInternal, «demo.db») Тогда
        ХасДБ = Истина
    Конец, если
    db.Initialize(File.DirInternal, "demo.db", True)
    Если нет (HasDB), то
        db.ExecNonQuery("CREATE TABLE Mod_ind_tab (имя_группы ТЕКСТ, имя_файла ТЕКСТ)")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (имя_группы, имя_файла) ЗНАЧЕНИЯ ('tab1', 'file1.txt')")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (имя_группы, имя_файла) ЗНАЧЕНИЯ ('tab1', 'file2.txt')")
        db.ExecNonQuery("INSERT INTO Mod_ind_tab (имя_группы, имя_файла) ЗНАЧЕНИЯ ('tab2', 'file3.txt')")
    Конец, если
    Проверить скопированные файлы
Конец субтитра

Sub CopyFileInternal как ResumableSub
    Пытаться
        Dim rs As ResultSet = db.ExecQuery("ВЫБЕРИТЕ имя_группы, имя_файла ИЗ Mod_ind_tab")
        Делать пока rs.NextRow
            Тусклая группа As String = rs.GetString("group_name")
            Тусклый ресурс As String = rs.GetString("имя_файла")
      
            File.MakeDir(File.DirInternal, группа)

            Dim NewSubFolder As String = File.Combine(File.DirInternal, группа)
            Dim NewFileName As String = актив и ".ass"
            File.Copy(File.DirAssets, актив, NewSubFolder, NewFileName)
        Петля
        rs.Close
    Ловить
        Журнал (Последнее исключение)
        Вернуть ложь
    Конец попытки
    Вернуть истину
Конец суб[/КОД]

[CODE lang="b4x" Highlight="24-28"]До:
[виртуальные_активы]
  файл1.txt
  файл2.txt
  файл3.txt
  файл4.txt
  главная страница.бал
  _assets_map
demo.db
demo.db-журнал
 
Готово=истина

После:
[виртуальные_активы]
  файл1.txt
  файл2.txt
  файл3.txt
  файл4.txt
  главная страница.бал
  _assets_map
demo.db
demo.db-журнал
[таб1]
  файл1.txt.ass
  файл2.txt.ass
[таб2]
  file3.txt.ass[/КОД]
[/QUOTE]

Thanks for the detailed answer. Now everything is clear to me. This is a rich experience for me. I will test it.
 
Upvote 0
Top