getting filenames

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
oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

getting filenames

Post by oldummy » Sun Jan 28, 2024 1:01 pm

The music player I am working on has a limitation that I cannot seem to work around.
In the playlist it will only play the music from the defaultfolder, so I cannot mix various artists in the playlist as the defaultfolder changes from artist to artist.
I've made some spectacularly awful attempts so far. Bad enough to send me running back to the forum.


How can I put a list of the filenames from a folder into a field rather than just putting "the files" into a field?


This
C:\Users\Gandalf\Desktop\Stick Music\Celtic\A Muhaire Bhruinneall.wma

rather than

A Muhaire Bhruinneall.wma

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: getting filenames

Post by Klaus » Sun Jan 28, 2024 1:18 pm

Hi oldummy,

since you have the path to the folder you can easily create the full path for your files,right! 8)

Code: Select all

...
## Hint: Internally LC ALWAYS uses the SLASH / as a path delimiter on ANY platform!
## On Windows the BACKSLASH is ONLY neccessary if you work with SHELL commands.

## So you can
put "C:/Users/Gandalf/Desktop/Stick Music/Celtic" into tFolder
put files(tFolder) into tFiles

## Now concatenate foldername and each file:
repeat for each line tFile in tFiles
  put tFolder & "/" & tFile & CR after tCompleteFiles
end repeat

## Get rid of trailing CR
delete char -1 of tCompleteFiles
put tCompleteFiles ibnto fld "all files with full path"
...
Best

Klaus

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: getting filenames

Post by Klaus » Sun Jan 28, 2024 3:17 pm

As you can see, there is really no need to set "the defaultfolder" any time!

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: getting filenames

Post by stam » Sun Jan 28, 2024 3:36 pm

Klaus wrote:
Sun Jan 28, 2024 3:17 pm
As you can see, there is really no need to set "the defaultfolder" any time!
Yeah I never use the defaultFolder any more either, I prefer to use the files(<folderPath>) and folders(<folderPath>)
The only thing I would advise to exclude filenames that begin with a dot (more of an issue on MacOS think as folders() returns ".." ie 1 level up as the first folder name, and invisible files like ".DS_Store" are common on MacOS).

So after using either Files() or Folders() I always filter out lines beginning with a dot:

Code: Select all

filter tFolders without ".*" // or tFiles
Almost always the return-delimited list returned is of no use as the path is relative to <folderPath>, so like Klaus I will also usually run a repeat loop (although because I'm lazy and don't want to create new variables, I will use the repeat with x =1 to the number of lines in <myVar> variation of repeat, because this allows me to edit the variable directly rather than having to create a new one like with the repeat for each variation.

so if you want to can add this function to your stack script and just pass it the full path to the folder you want get the file list from:

Code: Select all

function getFileList pFolder // full path to folder
    local tFiles
    put files(pFolder) into tFiles
    filter tFiles without ".*"
    repeat with x = 1 to the number of lines of tFiles
        put pFolder & slash & line x of tFiles into line x of tFiles
    end repeat
    return tFiles
end getFileList
note that this is not iterative, so will not examine any subfolders - which is doable if you need...

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: getting filenames

Post by stam » Sun Jan 28, 2024 4:18 pm

For bonus points:

here's a function that will recursively check each subfolder in the target folder and add the files listed.
It also allows you to filter the file list with a pattern - typically the extension - so that you can only list music files for example and not any accompanying text files:

Code: Select all

function recursiveFilesWithPattern pFolder, pPattern
    Local tPaths
    filter files(pFolder) with "*" & pPattern & "*"
    repeat for each line tFile in it
        put pFolder & slash & tFile & cr after tPaths
    end repeat
     
    filter folders(pFolder) without ".."
    repeat for each line tFolder in it
        put recursiveFilesWithPattern(pFolder & slash & tFolder, pPattern) after tPaths
    end repeat
     
    return tPaths
end recursiveFilesWithPattern
to show only a list of .wma files from both the main folder and all of it's subfolders you would

Code: Select all

put the recursiveFilesWithPattern(<folderPath>, ".wma") into field 1
or to list all files in a folder and all of it's subfolders:

Code: Select all

put the recursiveFilesWithPattern(<folderPath>,"") into field 1

oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Re: getting filenames

Post by oldummy » Sun Jan 28, 2024 5:19 pm

Thanks to all of you!! I will try all of your methods out.And yes, setting the defaultfolder does seem useless if I get these to work
Thanks again.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7239
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: getting filenames

Post by jacque » Thu Feb 01, 2024 10:11 pm

Here's a way to show the short file name in the list, while storing the file path invisibly using the metadata property. Metadata and linktext are similar, but linktext will underline the text, while metadata is completely invisible to the user.

Sample stack enclosed. The main action is in the card, and path retrieval is in the list field's script.

Edit: Stam's handler is in there but I forgot to credit it in the stack.
Attachments
File List [metadata].livecode.zip
(1.29 KiB) Downloaded 39 times
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

oldummy
Posts: 78
Joined: Mon Jun 13, 2016 3:21 pm

Re: getting filenames

Post by oldummy » Sat Feb 03, 2024 12:12 pm

I only just now saw your post Jaque.
Thank you Jaque. You've read my mind. That was the next issue I was going to deal with. Those fields I have will now be a lot more narrow!

golife
Posts: 103
Joined: Fri Apr 02, 2010 12:10 pm

Re: getting filenames

Post by golife » Mon Feb 05, 2024 8:00 pm

@Jacques -- I completely oversaw the property "metadata" and never used it. Thanks for the hint. Roland

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 135
Joined: Tue Feb 23, 2010 10:53 pm
Location: Saint Louis, Missouri USA

Re: getting filenames

Post by bobcole » Tue Feb 06, 2024 3:41 pm

Me too. I've never known about metadata before. A great feature of LiveCode.
Where is it stored? Is it like a global variable?
Bob

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7239
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: getting filenames

Post by jacque » Tue Feb 06, 2024 6:11 pm

It's stored in the htmltext of the field.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”