Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Folder loop — Gideros Forum

Folder loop

Lono9000Lono9000 Member
edited April 2016 in General questions
Hi guys! I am really new to lua and I know this may be a very basic question but how do I loop through a folder and do something in pairs with every element? ... For example if I want to change the scale of every bitmap in the folder "Example"

Comments

  • piepie Member
    edited April 2016
    Hi @Lono9000 you need to use lfs to read directory content:
    http://docs.giderosmobile.com/reference/plugin/lfs , i think it's enabled by default in the latest gideros release.
    put each entry in a table, then parse the table with pairs/ipairs.

    This is how it happens in tntfx :)
    function retrieveFiles(directory, ext) --ext: "json", "png"...
    require "lfs"
     
    	local function getBasenameAndExtension(filename)
    	  return filename:match("^([^%.]*)%.?(.*)$") -- "myfile.lua" -> "myfile", "lua"
    	end
     
    local files = {}
    	for file in lfs.dir(directory) do 
    		if lfs.attributes(file,"mode")== "directory" then 
    		print("found dir, "..file)
    		else 
    			if ext then --if extension is given, return only files.ext
    				local filebasename, fileextension = getBasenameAndExtension(file)
     
    				if fileextension == ext then
    					table.insert(files, file)
    				end
    			else
    				table.insert(files, file)
    			end
    		end
    	end
    return files	
    end
     
     
    local ftab = retrieveFiles("|R|Example/", "bmp") --ftab is just a list of file names
    --|R| is assuming that your Example dir is in root of your gideros project 
     
    --do something on your list
    for i,k in pairs(ftab) do
    	print(i,k)
    	--create each bitmap object, scale it etc...
    end
    You can use as base directory /sdcard/, windows drive letters (C:/dir/...) and |R|,|D|,|T| (http://docs.giderosmobile.com/file_system.html)
    [edit: sorry pasted the wrong code snippet the first time ]
  • john26john26 Maintainer
    Maybe you can explain more what you are trying to achieve? You mean you want to alter the graphics files in a directory? So you've got a load of PNGs and JPG files and you want to resize them? You realise that Gideros can draw and resize/scale Bitmaps easily (here I refer to Gideros Bitmap class which inherits from Sprite and is a major drawing class in Gideros)
Sign In or Register to comment.