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

Re: Read the contents of folder

Post by vladislavbyk »

kikito wrote: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"
Nice, thanks you very much for explanation!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Read the contents of folder

Post by bartbes »

kikito wrote:his pattern will fail with files with a double extension, such as myfile.tar.gz
This is unfortunate, we have wildly different views here, then. Tar.gz is perhaps the only double extension I do care about, otherwise it's generally just a dot in the filename. Also, I wouldn't say the extension of ".bashrc" is "bashrc".
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 »

bartbes wrote:Tar.gz is perhaps the only double extension I do care about
To be completely honest it's the same case to me - in LÖVE I doubt I'll ever need any other "composed" file extension. But if I get a .tar.gz, I want the thing to work intuitively and remove the gz and the tar too, dammit! :)

In ruby on rails I there is a lot more of double/triple extension: index.html.erb and script.js.coffee.erb are not uncommon.
bartbes wrote:Also, I wouldn't say the extension of ".bashrc" is "bashrc".
I don't have a strong opinion about that; It was simpler to implement that way, so I did. I doubt many of LÖVERs will have to deal with .dotfiles anyway. The test is there to ensure that the functions work without error in all cases (i.e. not raising errors or returning nils) not to express a particular personal preference.
When I write def I mean function.
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 »

kikito wrote: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).
Ah yes, you are correct. Never thought about that since I never deal with double extensions these days.

EDIT: Just for the hell of it, here's how to extract the filename from a path with folders:

Code: Select all

path:match("([^/]+)$")
("foo/bar/image.png"):match("([^/]+)$") -- image.png
You could also run kikito's getBasename on the string before or after that match to remove the extension.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests