Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Read all files in a directory? — Gideros Forum

Read all files in a directory?

bigtunacanbigtunacan Member
edited April 2016 in General questions
I would like to scan a directory for all files and then read each of these in. I don't see a way to do this with the file API though.

Comments

  • antixantix Member
    edited April 2016
    Here's some old code I found in my projects folder, I can't rember where it originated from. Anyhoo, it should be enough to get you started down the right path :)
    require("lfs")
    workingdir="C:/"
    print(workingdir.." contains:")
     
    local files ={
    dircount = 0,
    filecount = 0,
    dirlist={},
    filelist={}
    }
     
    files.dirlist[0] = "hi there"
     
    print (files.dirlist[0])
     
    for file in lfs.dir(workingdir) do
      if lfs.attributes(workingdir..file,"mode") == "file" then -- process file found
        print("file: "..file)
        files.filelist[files.filecount] = file
        files.filecount = files.filecount + 1
     
      elseif lfs.attributes(workingdir..file,"mode")== "directory" then -- process dir found
        print("dir: "..file)
        files.dirlist[files.dircount] = file
        files.dircount = files.dircount + 1
      end
    end
     
    print()
    print("DIRECTORIES (" .. files.dircount .. ")")
    for f = 0, files.dircount - 1 do
      print(files.dirlist[f])
    end
     
    print()
    print("FILES (" .. files.filecount .. ")")
    for f = 0, files.filecount - 1 do
      print(files.filelist[f])
    end
  • @antix,

    I've seen the lfs project, but it looked like it wouldn't support Android/iOS; however I could be wrong about that?
  • antixantix Member
    @bigtunacan, I haven't tried this on an actual device but you would use a different path for Android like "|D|" and it should work.
  • ar2rsawseenar2rsawseen Maintainer
    Yes it won't work with absolute paths on android/ios but should work with in app paths, specifically |D| for files that you create and save

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    Hi
    for in app paths see here http://docs.giderosmobile.com/file_system.html

    In my experience you can also use:

    /sdcard/ (and subdirs) in android
    and
    drive_letter:/ (and subdirs) on PC/windows

    (check it in tntfx > "import json" or "import background" ) :)
  • MoKaLuxMoKaLux Member
    edited June 2023
    does lfs.dir work for you guys with html5 export?

    I have this code:
    function attrdir(xpath, xtable)
    	if application:getDeviceInfo() == "Web" then
    --		xpath = application:getNativePath(xpath)
    		for file in lfs.dir(xpath) do
    			local f = xpath..'/'..file
    			local attr = lfs.attributes(f)
    			if attr.mode == "file" then table.insert(xtable, f) end
    		end
    	else -- desktops
    		for file in lfs.dir(xpath) do
    			local f = xpath..'/'..file
    			local attr = lfs.attributes(f)
    			if attr.mode == "file" then table.insert(xtable, f) end
    		end
    	end
    end
     
    local t = {}
    attrdir("gfx/images", t) -- folder to fetch images from
    I have this error:
    [string "tiled/tiled_levels.lua"]:91: cannot open gfx/images: No such file or directory
    stack traceback:
    [string "tiled/tiled_levels.lua"]:91: in function attrdir
    [string "tiled/tiled_levels.lua"]:911: in function init
    ...


    This works fine on android and windows.

    note: I will try to add / at the end of the path

    EDIT: do you have a solution to this question without using LFS?

    Thank you :)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • hgy29hgy29 Maintainer
    In HTML export, all your assets are packed into a single file (the .GApp) file, so not directly visible on the pseudo FS exposed to LFS, but you have access to the GApp file itself with lua (/main.GApp), so you could read the list of all files in your app from there.
    Here is the how Gideros reads it at app start: https://github.com/gideros/gideros/blob/master/emscripten/applicationmanager.cpp#L859
    It should be as easy as reading the first four bytes of the GApp file into an uint32, using the value as an offset in the GApp file, and start reading zerto terminated file name strings from there, ending with an empty name.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.