getting filenames
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
getting filenames
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
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
Re: getting filenames
Hi oldummy,
since you have the path to the folder you can easily create the full path for your files,right!
Best
Klaus
since you have the path to the folder you can easily create the full path for your files,right!
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"
...
Klaus
Re: getting filenames
As you can see, there is really no need to set "the defaultfolder" any time!
Re: getting filenames
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
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
Re: getting filenames
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:
to show only a list of .wma files from both the main folder and all of it's subfolders you would
or to list all files in a folder and all of it's subfolders:
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
Code: Select all
put the recursiveFilesWithPattern(<folderPath>, ".wma") into field 1
Code: Select all
put the recursiveFilesWithPattern(<folderPath>,"") into field 1
Re: getting filenames
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.
Thanks again.
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: getting filenames
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.
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 50 times
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: getting filenames
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!
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!
Re: getting filenames
@Jacques -- I completely oversaw the property "metadata" and never used it. Thanks for the hint. Roland
-
- VIP Livecode Opensource Backer
- Posts: 137
- Joined: Tue Feb 23, 2010 10:53 pm
- Location: Saint Louis, Missouri USA
Re: getting filenames
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
Where is it stored? Is it like a global variable?
Bob
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: getting filenames
It's stored in the htmltext of the field.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com