Jump to content




Find a file extension?


  • You cannot reply to this topic
16 replies to this topic

#1 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 17 June 2015 - 11:33 AM

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....

#2 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 June 2015 - 11:46 AM

You could fs.list() the directoy and do a pattern capture on the results. Then you've got all files with that file extension :)

#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 June 2015 - 01:25 PM

What pattern should be used?

#4 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 17 June 2015 - 01: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")
.

#5 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 17 June 2015 - 01:51 PM

Thanks to
InDieTasten

Edited by lauriszz123, 17 June 2015 - 01:51 PM.


#6 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 June 2015 - 01:51 PM

View Postlauriszz123, on 17 June 2015 - 01:42 PM, said:

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.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 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 June 2015 - 01:55 PM

How do you get the part after the point?

#8 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 June 2015 - 01: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

View PostCreator, on 17 June 2015 - 01:55 PM, said:

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 by InDieTasten, 17 June 2015 - 02:02 PM.


#9 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 June 2015 - 02:00 PM

What pattern will alow you to find the part after a point in a string?

#10 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 June 2015 - 02:04 PM

View PostCreator, on 17 June 2015 - 02:00 PM, said:

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 by InDieTasten, 17 June 2015 - 02:07 PM.


#11 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 June 2015 - 02:27 PM

View PostInDieTasten, on 17 June 2015 - 01:59 PM, said:

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.

#12 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 June 2015 - 04: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.

#13 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 June 2015 - 04:52 PM

View PostMKlegoman357, on 17 June 2015 - 04:50 PM, said:

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?

#14 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 17 June 2015 - 04:57 PM

View PostCreator, on 17 June 2015 - 04:52 PM, said:

View PostMKlegoman357, on 17 June 2015 - 04:50 PM, said:

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 by InDieTasten, 17 June 2015 - 05:05 PM.


#15 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 17 June 2015 - 04:58 PM

View PostCreator, on 17 June 2015 - 04:52 PM, said:

View PostMKlegoman357, on 17 June 2015 - 04:50 PM, said:

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.

#16 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 17 June 2015 - 05: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.

#17 LeDark Lua

  • Members
  • 369 posts
  • LocationLeLua

Posted 17 June 2015 - 06:40 PM

Thanks you guys for the comments, they help me learn A LOT!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users