Find a file extension?
#1
Posted 17 June 2015 - 11:33 AM
I was looking trough FS API documentation, but i dint find anything....
#2
Posted 17 June 2015 - 11:46 AM
#3
Posted 17 June 2015 - 01:25 PM
#4
Posted 17 June 2015 - 01:42 PM
fs.exists(awesomeStoring..".app").
#5
Posted 17 June 2015 - 01:51 PM
#6
Posted 17 June 2015 - 01:51 PM
lauriszz123, on 17 June 2015 - 01:42 PM, said:
fs.exists(awesomeStoring..".app").
http://www.computerc...fo/wiki/Fs.find
Meaning something like fs.find("*.app") should return all files with the extension .app
Edited by InDieTasten, 17 June 2015 - 01:53 PM.
#7
Posted 17 June 2015 - 01:55 PM
#8
Posted 17 June 2015 - 01:59 PM
local all = fs.list("")
local result = {}
for k,v in pairs(all) do
if(string.find(v,"[%a%d]+%.app")) then
table.insert(result,v)
end
end
result should then contain all files with extension .app
Creator, on 17 June 2015 - 01:55 PM, said:
_,_,file,extension = string.find("pacman.img","([%a%d]+)%.([%a%d]+)")
then file should contain "pacman" and extension "img"
Edit, fixed the patterns a bit. should work with numbers too this way. But be careful, it's still untested
Edited by InDieTasten, 17 June 2015 - 02:02 PM.
#9
Posted 17 June 2015 - 02:00 PM
#10
Posted 17 June 2015 - 02:04 PM
Creator, on 17 June 2015 - 02:00 PM, said:
Or do you mean point by position within the string? I thought about point as dot ".".
If you want to retrieve the rest of a string after a certain position, I would recommend: string.sub("your string", 6), which would return only "string", so the first 5 characters are cut.
Edited by InDieTasten, 17 June 2015 - 02:07 PM.
#11
Posted 17 June 2015 - 02:27 PM
InDieTasten, on 17 June 2015 - 01:59 PM, said:
It's not available in Lua, if that's what you're thinking. I did once write something similar once, though, just for giggles.
My main gripe with fs.find() is that it's case-sensitive, even if the file system you're using it on isn't. In my view, that limits its usefulness by quite a bit.
#12
Posted 17 June 2015 - 04:50 PM
local name, extension = string.match("pacman.test.img","(.*)%.(.*)")
Do note however that string.match will return nil if there will be no dot in the file name.
#14
Posted 17 June 2015 - 04:57 PM
Creator, on 17 June 2015 - 04:52 PM, said:
As this pattern is somewhat better fitting, it still has the issue, that an input string with multiple dots will put the first segment in the name, and the extension will include the rest. Ex: "my.weird.file" would be captured as "my" as filename and "weird.file" as extension. I think that can be improved with an extra symbol I forgot. But yeah, as this is not the topic of the OP, you may want to create your own topic for this creator, when you have more questions belonging to this. Also a read on lua patterns is always worth it
Edited by InDieTasten, 17 June 2015 - 05:05 PM.
#15
Posted 17 June 2015 - 04:58 PM
Creator, on 17 June 2015 - 04:52 PM, said:
Parenthesis inside the pattern tell what part of the match to return, in this case there are two of them: both are '(.*)'. A dot (.) is a special character which means any character and a star (*) means as many of the characters as possible or zero. So '.*' means: get as many characters as possible. If not possible, return empty string. Now, to actually detect a dot in a string you cannot just write a dot because it's a special character, so you have to escape it using '%.'. Now, the pattern says: get as many characters as possible until hit the last dot in the string, then get all the characters possible after the dot.
You can read more about patterns here.
#16
Posted 17 June 2015 - 05:07 PM
#17
Posted 17 June 2015 - 06:40 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











