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"
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"localfunction getBasenameAndExtension(filename)return filename:match("^([^%.]*)%.?(.*)$")-- "myfile.lua" -> "myfile", "lua"endlocal files ={}for file in lfs.dir(directory)doif lfs.attributes(file,"mode")=="directory"thenprint("found dir, "..file)elseif ext then--if extension is given, return only files.extlocal filebasename, fileextension = getBasenameAndExtension(file)if fileextension == ext thentable.insert(files, file)endelsetable.insert(files, file)endendendreturn files
endlocal 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 listfor i,k inpairs(ftab)doprint(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 ]
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)
Comments
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
[edit: sorry pasted the wrong code snippet the first time ]
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975