PDA

View Full Version : very neat function by Petr



kryton9
21-03-2007, 00:53
Guys, I was studying some of the code that Petr has put into Topdown3D and ran across this function he created. I think many will find it very handy as it is very powerful and a good example of a very neat thinBasic command.


function GetFileList( path as string, mask as string ) as string

local FileNames as string
LOCAL pList AS LONG
LOCAL nItems AS LONG
local i as long

pList = DIR_List(path, mask, %FILE_NORMAL OR %FILE_HIDDEN OR %FILE_SYSTEM OR %FILE_ADDPATH)
nItems = LL_Count(pList)

for i = 1 to nItems
FileNames += LL_GetItem(pList, i )+","
next
FileNames = Rtrim$(FileNames, ",")

LL_Free(pList)

function = FileNames

end function

It would be called for example to get all bmp files in a textures folder, then to randomly select one from the list:

local BackgroundBMP as string = GetFileList(APP_SOURCEPATH + dirTextures+"\instant action level 01\", "ia*.bmp")
msgbox(0,BackgroundBMP)
BackgroundBMP = parse$(BackgroundBMP, ",", rnd(1, parsecount(BackgroundBMP, ",")))

Petr, this routine is really nice, thanks I would never have figured out how to use it otherwise!!

ErosOlmi
21-03-2007, 01:01
Ken,

that function is perfect.
But also consider the recent introduction of DIR_ListArray (http://www.thinbasic.com/public/products/thinBasic/help/html/dir_listarray.htm) that does the same but with few lines of code. This function was requested by Petr long time ago here: http://community.thinbasic.com/index.php?topic=339.0

Ciao
Eros

kryton9
21-03-2007, 01:08
Oh my even better Eros, thanks, just in time as I am going to start using these. Thanks for being so quick to reply and catch me in the nick of time!!