Page 1 of 1
How to read out the filelist of a directory and use it?
Posted: Mon Mar 17, 2014 5:22 pm
by DevBoyLars
Hi there,
ho can I read out the filenames in a directory and use them (maybe in an array)?
I used C# before
I need to read out a directory to use the filenames to open pictures for a slideshow.
Thank you

Re: How to read out the filelist of a directory and use it?
Posted: Mon Mar 17, 2014 5:26 pm
by FourthWorld
See the "files" function in the Dictionary; also, the "folders" function can be used for those. See also the "defaultFolder" global property to set the directory as needed.
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 9:24 am
by Neurox66
DevBoyLars wrote:Hi there,
ho can I read out the filenames in a directory and use them (maybe in an array)?
See here:
http://lessons.runrev.com/s/lessons/m/4 ... n-a-folder
and here:
http://lessons.runrev.com/s/lessons/m/4 ... ers-part-1
Paolo
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 9:30 am
by DevBoyLars
Thank you, that's what I'm looking for
Is there a way to select single filenames from this list? In other languages I would put all filenames of a directory in an array an read them out by array
, but I don't know how to get the list length and to read a special list-entry, like the 6th or so.
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 10:09 am
by Neurox66
Hi,
Code: Select all
put 0 into tInc
repeat for each line tFile in the files
add 1 to tInc
put tFile into tList[tInc]
end repeat
answer "First file: " & tList[1] &cr& "Last file:" & tList[tInc]
Paolo
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 10:44 am
by DevBoyLars
Neurox66 wrote:Hi,
Code: Select all
put 0 into tInc
repeat for each line tFile in the files
add 1 to tInc
put tFile into tList[tInc]
end repeat
answer "First file: " & tList[1] &cr& "Last file:" & tList[tInc]
Paolo
Sounds great. Jus to be sure: You put tFile into the array. How does LiveCode knows on which position it is, when it puts a filename into the array on position tInc?
And: Is there another more LC way to do that without an array? In my slideshow the user should scroll by swipe through the pictures of a directory. My first idea was to read out the directory and load the image with the filename at the position where the user swiped.
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 5:03 pm
by jmburnod
Hi Paolo,
Is there another more LC way to do that without an array?
Yes, but it is less elegant
Code: Select all
local sAllFilesImg,sCurfolder
on GetImagesFiles
answer folder "Open an images folder"
put it into sCurfolder
set the defaultfolder to sCurfolder
get the files
filter it without ".*" -- take off file ".DS_Store"
put 0 into tInc
put empty into sAllFilesImg
repeat for each line tFile in it --the files
add 1 to tInc
put tFile & cr after sAllFilesImg
end repeat
delete char -1 of sAllFilesImg
answer "First file: " & line 1 of sAllFilesImg & cr & "Last file:" & line -1 of sAllFilesImg
end GetImagesFiles
load the image with the filename at the position where the user swiped.
Code: Select all
on showImg pNum -- pNum = the num of a line
get line pNum of sAllFilesImg
set the filename of img "MyImage" to (sCurfolder & "/" & it)
end showImg
Best regards
Jean-Marc
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 5:09 pm
by jacque
You don't even need the repeat loop if you use the "split" command like this:
Code: Select all
put the files into tList
split tList by cr
This gives you an indexed array with the keys as numerical values. To pull out a particular line:
gives you the third line (i.e., the third file name.)
If you don't want to use an array you can just refer to the line number itself. Text chunking is LiveCode's forte. So if the file list is just a return-delimited list, this gives you the third line directly:
Either way works. I find plain text easier to debug than arrays, but it's a personal choice.
(The repeat loop in Jean-Marc's example isn't strictly necessary either, you can use the file list that LiveCode gives you directly. However, the way he filters the list without the leading dot is a good move, you'll want to do that to get rid of invisible files on Macs.)
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 5:22 pm
by DevBoyLars
Hi Jacque,
thank you for the great tip with the split-command. What does "by cr" means? And how to get the number of entries in tList, after I split it?
Re: How to read out the filelist of a directory and use it?
Posted: Tue Mar 18, 2014 5:39 pm
by jacque
"cr" is a LiveCode constant that means "carriage return". LiveCode carriage returns refer to any line ending from any platform -- ascii 10 ('nix) or 13 (Mac) or both (Windows) so you don't need to worry about where the text came from. (Internally LiveCode translates all those to ascii 10, but it doesn't matter here.)
You can read about the split command in the dictionary. If you supply only one delimiter (cr in this case) you will get a numerically indexed array. If you supply two, you will get an array with the keys being the first text chunk as defined by the second delimiter. I.e., splitting text by cr and tab will give you an array with the keys equal to the first tabbed item in each line.
If you don't use an array, you can use "the number of lines in tList" to get the total count. If you do use an array, "the number of lines in the keys of tList" will give you the line count. Or if your array is a numerical one, you can alternately use "the extents" to get the number. See the dictionary for all those.
