File and Folders Part 1
A very common task when writing a program is to get a list of files or folders and either display it for the user to choose one, or go through each file or folder and perform some action. This lesson shows some simple methods of listing and sorting files and gives some hints on the relevant LiveCode commands used.
Please download a copy of the supporting stack File Lister.rev for a working example of the code that follows
Getting a list of files or folders
LiveCode has a very simple way of obtaining lists of files and folders on a computer. The two functions files and folders and the property the defaultFolder are the only things you need. The basic idea is to set the defaultFolder to the folder you want to work with, and then use the two above-mentioned functions to return the lists of files or folders.
Choosing a folder
We will create a stack that allows us to select the folder we want to work with and displays the files and folders in that folder. Firstly add a field and a button to the new stack, which we will name File Lister.
Then we add some code to our "Choose folder" button(1), using the answer folder command to select the folder we want to work with. This puts the full path to the selected folder into the field we have named "Folder"(2)
on mouseUp local tFolder answer folder "Please choose the folder you want to work with" put it into tFolder if there is a folder tFolder then put tFolder into field "Folder" end if end mouseUp
Displaying a list of files
Now we will add a scrolling field called "List"(1) and a "List files" button(2) to our stack.
We then use the files function to get a list of the files in our selected folder and display the list in the field
on mouseUp local tFolder put field "Folder" into tFolder set the defaultFolder to tFolder put the files into field "List" end mouseUp
Displaying a list of folders
You can use the folders function in exactly the same way, just add another button and change the last line of the mouseUp handler to
put the folders into field "list"
Filtering the lists
On many file systems the folders "." and ".." refer to the current folder and the parent folder respectively. It is important to make sure that these two values do not appear in the list of folders or files because they are not real folders and more importantly as you can see later, they can cause problems in your program. A good way to safeguard yourself from this is to use the following functions instead of using the files or the folders. We add these functions to our stack script and change our buttons to call these functions.
# Filters the strings "." and ".." from a list function filterDots pList local tList put pList into tList filter tList without "." filter tList without ".." return tList end filterDots # Returns a filtered list of files in the current directory function filteredFiles return filterDots(the files) end filteredFiles # Returns a filtered list of folders in the current directory function filteredFolders return filterDots(the folders) end filteredFolders ## List Files button script on mouseUp local tFolder, tFileList put field "Folder" into tFolder set the defaultFolder to tFolder put filteredFiles() into field "List" end mouseUp
Listing the full paths to the files
Sometimes it will be useful to know the full path to a file, lets add another function to our stack script which will display the full path to each file in the "List" field. Again we will add a button to the stack that calls the handler on the card script.
function filteredFilesWithPaths local tFiles, tFilesWithPaths put filteredFiles() into tFiles ## Use a repeat loop ## For each file we put the defaultFolder before the file name, this gives the full path to the file ## We use this to build a new list which we return to be displayed repeat for each line tFile in tFiles put the defaultFolder & slash & tFile & return after tFilesWithPaths end repeat delete the last char of tFilesWithPaths return tFilesWithPaths end filteredFilesWithPaths
Performing an action for each file
Performing an action for each file in a folder is a common task. This handler will loop through each file in a folder and call the doSomething handler for each file, giving the handler the name of the file.
All that is left to do is write the doSomething handler. A simple action could be to make the name of the file upper case and write it to the message box for example:
# Use this template function to perform an action on each file in a folder on doForEachFile pFolder set the defaultFolder to pFolder ## Use the filtered list of files repeat for each line tFile in filteredFiles() ## Call the doSomething handler for each file doSomething (tFile) end repeat end doForEachFile on doSomething pFile ## As an example we will just make the filename upper case ## and print it to the message box put toUpper(pFile) & return after msg end doSomething
In the next lesson we will look at using recursion to perform similar tasks to those above but including subfolders and their files.