Page 1 of 2

Read the contents of folder

Posted: Mon Oct 01, 2012 7:47 pm
by vladislavbyk
How can I read the contents of a specific folder without file extensions?

Re: Read the contents of folder

Posted: Mon Oct 01, 2012 8:19 pm
by josefnpat
Try using love.filesystem.enumerate

edit:

Then use Lua pattern matching to remove the extension

Re: Read the contents of folder

Posted: Mon Oct 01, 2012 8:23 pm
by vladislavbyk
josefnpat wrote: Then use Lua pattern matching to remove the extension
How..? :)

Re: Read the contents of folder

Posted: Mon Oct 01, 2012 9:01 pm
by josefnpat
vladislavbyk wrote:
josefnpat wrote: Then use Lua pattern matching to remove the extension
How..? :)
Take a look at the documentation.

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 3:29 am
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..

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 3:42 am
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.

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 7:40 am
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.

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 9:35 am
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%-]+)$", "")

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 9:54 am
by vladislavbyk
Thanks guys :)

Re: Read the contents of folder

Posted: Tue Oct 02, 2012 10:04 am
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"