How to read out the filelist of a directory and use it?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DevBoyLars
Posts: 216
Joined: Wed Feb 27, 2013 9:04 pm

How to read out the filelist of a directory and use it?

Post by DevBoyLars » Mon Mar 17, 2014 5:22 pm

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 :)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to read out the filelist of a directory and use it?

Post by FourthWorld » Mon Mar 17, 2014 5:26 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Neurox66
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 120
Joined: Tue May 22, 2012 1:57 pm
Contact:

Re: How to read out the filelist of a directory and use it?

Post by Neurox66 » Tue Mar 18, 2014 9:24 am

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
Paolo Borzini | paolo@borzini.it
The WhiteFly Software | www.thewhitefly.it
Service on line for printers | www.4pellicole.it

DevBoyLars
Posts: 216
Joined: Wed Feb 27, 2013 9:04 pm

Re: How to read out the filelist of a directory and use it?

Post by DevBoyLars » Tue Mar 18, 2014 9:30 am

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.

Neurox66
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 120
Joined: Tue May 22, 2012 1:57 pm
Contact:

Re: How to read out the filelist of a directory and use it?

Post by Neurox66 » Tue Mar 18, 2014 10:09 am

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
Paolo Borzini | paolo@borzini.it
The WhiteFly Software | www.thewhitefly.it
Service on line for printers | www.4pellicole.it

DevBoyLars
Posts: 216
Joined: Wed Feb 27, 2013 9:04 pm

Re: How to read out the filelist of a directory and use it?

Post by DevBoyLars » Tue Mar 18, 2014 10:44 am

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.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: How to read out the filelist of a directory and use it?

Post by jmburnod » Tue Mar 18, 2014 5:03 pm

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
https://alternatic.ch

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to read out the filelist of a directory and use it?

Post by jacque » Tue Mar 18, 2014 5:09 pm

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:

Code: Select all

get tList[3]
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:

Code: Select all

get line 3 of tList
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.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

DevBoyLars
Posts: 216
Joined: Wed Feb 27, 2013 9:04 pm

Re: How to read out the filelist of a directory and use it?

Post by DevBoyLars » Tue Mar 18, 2014 5:22 pm

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?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to read out the filelist of a directory and use it?

Post by jacque » Tue Mar 18, 2014 5:39 pm

"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. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply