Read the contents of folder

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
vladislavbyk
Prole
Posts: 8
Joined: Mon Oct 01, 2012 7:44 pm

Read the contents of folder

Post by vladislavbyk »

How can I read the contents of a specific folder without file extensions?
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Read the contents of folder

Post by josefnpat »

Try using love.filesystem.enumerate

edit:

Then use Lua pattern matching to remove the extension
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
vladislavbyk
Prole
Posts: 8
Joined: Mon Oct 01, 2012 7:44 pm

Re: Read the contents of folder

Post by vladislavbyk »

josefnpat wrote: Then use Lua pattern matching to remove the extension
How..? :)
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: Read the contents of folder

Post by josefnpat »

vladislavbyk wrote:
josefnpat wrote: Then use Lua pattern matching to remove the extension
How..? :)
Take a look at the documentation.
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
vladislavbyk
Prole
Posts: 8
Joined: Mon Oct 01, 2012 7:44 pm

Re: Read the contents of folder

Post by vladislavbyk »

josefnpat wrote:
vladislavbyk wrote:
josefnpat wrote: Then use Lua pattern matching to remove the extension
How..? :)
Take a look at the documentation.
I really do not understand how it works :( It's hard in lua..
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Read the contents of folder

Post by dreadkillz »

I hate to be THAT guy, but you'll have to do some experimenting to really get it. Go open the Lua interpreter in a terminal/commandline and start messing around with strings.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Read the contents of folder

Post by Roland_Yonaba »

Using Lua pattern matching is not as complicated as you might think it is.
Some indications:
You can use string.gsub().
- First arg will be the filename,
- Second will be the pattern to capture the extension, to be replaced with the third argument.
- Third argument will be the empty string.

Now, have in mind that a file extension starts with a dot, followed by one or more characters (letters/numbers), and is anchored at the end of the full filename.

Hope that helps.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Read the contents of folder

Post by BlackBulletIV »

Here's the code for what Roland is saying:

Code: Select all

string.gsub(filename, "%.%w+$", "")
You may want to add support for hyphens for extensions like .sublime-project:

Code: Select all

string.gsub(filename, "%.([%w%-]+)$", "")
vladislavbyk
Prole
Posts: 8
Joined: Mon Oct 01, 2012 7:44 pm

Re: Read the contents of folder

Post by vladislavbyk »

Thanks guys :)
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Read the contents of folder

Post by kikito »

Come on guys. Lua's patterns have always been difficult. Give vlad a break.

Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong :) (his pattern will fail with files with a double extension, such as myfile.tar.gz).

Here's what I'd use to get the file name only (this is what vlad needs - probably there's a simpler pattern, but this one works very well):

Code: Select all

function getBasename(filename)
  return filename:match("^([^%.]*)%.?") -- "myfile.lua" -> "myfile"
end
Here's an extended version (compatible with the previous one) which does a little more; it returns the corresponding extension:

Code: Select all

function getBasenameAndExtension(filename)
  return filename:match("^([^%.]*)%.?(.*)$") -- "myfile.lua" -> "myfile", "lua"
end
Tests:

Code: Select all

for _,filename in ipairs({"myfile.lua", "myfile", ".bashrc", "myfile.tar.gz", "myfile.yeah-baby.yeah"}) do
  print(("getBasename(%q) = %q"):format(filename, getBasename(filename)))
  print(("getBasenameAndExtension(%q) = %q, %q"):format(filename, getBasenameAndExtension(filename)))
end
Result:

Code: Select all

getBasename("myfile.lua") = "myfile"
getBasenameAndExtension("myfile.lua") = "myfile", "lua"
getBasename("myfile") = "myfile"
getBasenameAndExtension("myfile") = "myfile", ""
getBasename(".bashrc") = ""
getBasenameAndExtension(".bashrc") = "", "bashrc"
getBasename("myfile.tar.gz") = "myfile"
getBasenameAndExtension("myfile.tar.gz") = "myfile", "tar.gz"
getBasename("myfile.yeah-baby.yeah") = "myfile"
getBasenameAndExtension("myfile.yeah-baby.yeah") = "myfile", "yeah-baby.yeah"
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests