How to list the files in a folder
This lesson will show you how to get a list of all the files in a specific folder.
Create a new stack and drag on basic components
Start by creating a new stack and drag on a button for our script and a field to hold our list of files.
Key lines of code
The following lines of code are the key items we need for this particular task. The 'defaultfolder' is where LiveCode looks when you perform file or folder requests. So if we set the 'defaultfolder' to where our files are we will be able to read them very easily.
set the defaultfolder to tFolderPath
The second key line of code required is this one. It will return a list of all the files in the 'defaultfolder'.
put the files
Put them into practice
Edit the script of the button by selecting it (1) and clicking on the code icon in the toolbar or right clicking and choosing Edit Script. Paste the following script into the script window (2) and click Apply (3).
on mouseUp
# Pops up a dialog box letting the user select a folder
answer folder "Please select a folder"
# Exit this handler if the user click cancel on the dialog
if it is "cancel" then
exit mouseUp
end if
# Set the default folder to the folder selected by the user
# (this is stored in the 'it' variable
set the defaultfolder to it
# Put a list of all the files in field 1
put the files into field 1
end mouseUp
When you switch to run mode (1) and click on the button (2), a dialog should popup asking you to select a folder (3). All the files in the folder you select will be listed in the text field (4).
Tim Franklin
Nice, out of curiosity, how would one go about designing a, directory structure that could be navigated, thanks for taking time to post.
Glynn
great stuff.
Wayne E Peters
This work great but how do you get it to sort the list alphabetically?
thanks
Matthias Rebbe
@Wayne E Peters
You then have to put the file list into a container(variable) first and sort the content of that container. After sorting you put the content of the container into the field.
put the files into tFiles
sort tFiles ascending
put tFiles into field 1