lfs.dir returns each directory entry's name as a string, so you can use string.match from the standard library to detect file name patterns.
Here's a simple example:
filename = "test.bmp"
pattern = "\.bmp$"
if string.match(filename, pattern) ~= nil then
print(filename)
else
print("NO MATCH")
end
gusano79
Practically a Master Poster
676 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6
so instead of file:lines() i can just put this ? and this will allow my text output be created?
The example I posted is just to decide whether the file name ends with ".bmp" or whatever other pattern you want to detect. You'll still have to read the contents of the file and do whatever it is you want to do with it. It's more of a replacement for if #dir_elem > 4 and dir_elem:sub(#dir_elem-3, #dir_elem) == ".obj" . IMO, the pattern is easier to read.
But maybe I misunderstood the question... file:lines() doesn't take any parameters. You can use pattern matching on the lines it returns, though. Something like:
for line in file:lines() do
if string.match(line, "v") then
--Writes GL_Vertex3f for every occurence of v
input:write(" GL_Vertex3f ")
end
end
Depending on what you're trying to do, the pattern might be different, but that's the basic idea.
gusano79
Practically a Master Poster
676 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6
Matching the file names doesn't replace the file name itself. You can still use the standard io library to read the files:
for entry in lfs.dir(folder) do
if string.match(entry, pattern) ~= nil then
local file = io.open(entry, "r")
-- TODO: Read stuff from the file here.
file:close()
end
end
gusano79
Practically a Master Poster
676 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6
would you be able to rewrite the code so that so it works fully
I don't know what your code is ultimately supposed to do, and I don't have a sample "test.txt" file to work on, so I can't guarantee that anything I write here will work.
so i can learn it will work so i can understand it better ?
I believe all of the pieces you need are here, but you seem to be having trouble with how they all work together. Compare the following code with the previous examples, and hopefully that will help. If you have any questions about why it's written the way it is, please ask.
require "lfs"
input = io.open("test.txt","w")
for entry in lfs.dir("test folder") do
if string.match(entry, "\.obj$") ~= nil then
local file = io.open(entry, "r")
for line in file:lines() do
if string.match(line, "v") then
input:write(" GL_Vertex3f ")
end
end
file:close()
end
end
input.close()
gusano79
Practically a Master Poster
676 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6
the code is ment to look in a directory for .obj files and then read those .obj files for the characters of v and any non character symbol like .30493093 then write to a text file for every v character match write characters of gl_vertex3f I also want it too write out the non character symbols to the text file thats about it but this seems to not be the case
Do you have a short .obj file that you can post here? It helps to have an example to work from.
gusano79
Practically a Master Poster
676 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6