Read the contents of folder
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 8
- Joined: Mon Oct 01, 2012 7:44 pm
Read the contents of folder
How can I read the contents of a specific folder without file extensions?
- josefnpat
- Inner party member
- Posts: 955
- Joined: Wed Oct 05, 2011 1:36 am
- Location: your basement
- Contact:
Re: Read the contents of folder
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
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
-
- Prole
- Posts: 8
- Joined: Mon Oct 01, 2012 7:44 pm
Re: Read the contents of folder
How..?josefnpat wrote: Then use Lua pattern matching to remove the extension

- josefnpat
- Inner party member
- Posts: 955
- Joined: Wed Oct 05, 2011 1:36 am
- Location: your basement
- Contact:
Re: Read the contents of folder
Take a look at the documentation.vladislavbyk wrote:How..? :)josefnpat wrote: 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
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
-
- Prole
- Posts: 8
- Joined: Mon Oct 01, 2012 7:44 pm
Re: Read the contents of folder
I really do not understand how it worksjosefnpat wrote:Take a look at the documentation.vladislavbyk wrote:How..?josefnpat wrote: Then use Lua pattern matching to remove the extension

- dreadkillz
- Party member
- Posts: 223
- Joined: Sun Mar 04, 2012 2:04 pm
- Location: USA
Re: Read the contents of folder
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.
- 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
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.
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.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Read the contents of folder
Here's the code for what Roland is saying:
You may want to add support for hyphens for extensions like .sublime-project:
Code: Select all
string.gsub(filename, "%.%w+$", "")
Code: Select all
string.gsub(filename, "%.([%w%-]+)$", "")
-
- Prole
- Posts: 8
- Joined: Mon Oct 01, 2012 7:44 pm
Re: Read the contents of folder
Thanks guys 

- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Read the contents of folder
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):
Here's an extended version (compatible with the previous one) which does a little more; it returns the corresponding extension:
Tests:
Result:
Besides, getting the pattern is actually tricker than it looks. BlackBulletV, who is very experienced with Lua, got it wrong

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
Code: Select all
function getBasenameAndExtension(filename)
return filename:match("^([^%.]*)%.?(.*)$") -- "myfile.lua" -> "myfile", "lua"
end
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
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests