This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LeDark Lua's profile picture

Find a file extension?

Started by LeDark Lua, 17 June 2015 - 10:33 AM
LeDark Lua #1
Posted 17 June 2015 - 12:33 PM
I want to know how could I find a file extension [ for ex: fs.findFileExt("veryOriginalDirName/*.ikr") ]
I was looking trough FS API documentation, but i dint find anything….
InDieTasten #2
Posted 17 June 2015 - 12:46 PM
You could fs.list() the directoy and do a pattern capture on the results. Then you've got all files with that file extension :)/>
Creator #3
Posted 17 June 2015 - 02:25 PM
What pattern should be used?
LeDark Lua #4
Posted 17 June 2015 - 02:42 PM
Oh I got it! I just check for every file in a directory, then I would store them in a table and restore them in another table with .app cut out, then when I check the file, i have for ex: awesomeName and I check
fs.exists(awesomeStoring..".app")
.
LeDark Lua #5
Posted 17 June 2015 - 02:51 PM
Thanks to
InDieTasten
Edited on 17 June 2015 - 12:51 PM
InDieTasten #6
Posted 17 June 2015 - 02:51 PM
Oh I got it! I just check for every file in a directory, then I would store them in a table and restore them in another table with .app cut out, then when I check the file, i have for ex: awesomeName and I check
fs.exists(awesomeStoring..".app")
.
That would be a quite dirty implementation. I'm not exactly sure what you are trying to receive. Also note, that you can use wildcards from version 1.6 upwards.

http://www.computercraft.info/wiki/Fs.find

Meaning something like fs.find("*.app") should return all files with the extension .app
Edited on 17 June 2015 - 12:53 PM
Creator #7
Posted 17 June 2015 - 02:55 PM
How do you get the part after the point?
InDieTasten #8
Posted 17 June 2015 - 02:59 PM
If you are on an older version, you could look up the implementation of fs.find in the mods zip archive. Something like the following would do the job I think:

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

How do you get the part after the point?
for that you can capture:

_,_,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 on 17 June 2015 - 01:02 PM
Creator #9
Posted 17 June 2015 - 03:00 PM
What pattern will alow you to find the part after a point in a string?
InDieTasten #10
Posted 17 June 2015 - 03:04 PM
What pattern will alow you to find the part after a point in a string?
Thats what I wrote last post. The string in the example is "pacman.img" and the extension var should contain only "img", which is the rest of the string after the point. You could also sub it to make it split after each dot or comma or basically whatever you want.

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 on 17 June 2015 - 01:07 PM
Bomb Bloke #11
Posted 17 June 2015 - 03:27 PM
If you are on an older version, you could look up the implementation of fs.find in the mods zip archive.

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.
MKlegoman357 #12
Posted 17 June 2015 - 05:50 PM
Here's a better pattern:


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.
Creator #13
Posted 17 June 2015 - 05:52 PM
Here's a better pattern:


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.

How does that pattern work?
InDieTasten #14
Posted 17 June 2015 - 05:57 PM
Here's a better pattern:


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.

How does that pattern work?
a dot "." represents any character. The parenthethis define the captions(the stuff that match or find will return), the "%." in the middle will require an actual dot in the string, and the ".*" star says, that there can be any amount of any character in the caption, so there can be zero to infinite characters in front and after the dot, thats seperating them

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 ;)/> Google shall do the job
Edited on 17 June 2015 - 04:05 PM
MKlegoman357 #15
Posted 17 June 2015 - 05:58 PM
Here's a better pattern:


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.

How does that pattern work?

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.
Creator #16
Posted 17 June 2015 - 06:07 PM
Thank you a lot. And don't think I haven't read about patterns. It is just that I find some parts of them challenging.
LeDark Lua #17
Posted 17 June 2015 - 07:40 PM
Thanks you guys for the comments, they help me learn A LOT!