Hi

I'm really struggling in finding an answer to my problem


How do I read a folder and then I want to find a file inside that folder read with specific extensions like .obj or .bmp and to do things to those files with those extensions


here is my attempt

require "lfs"





input = io.open("test.txt","w")

for dir_elem in lfs.dir("test folder") do
	
local target = "test folder" .. "/" .. dir_elem
	
if lfs.attributes(target).mode == "file" then

		
if #dir_elem > 4 and dir_elem:sub(#dir_elem-3, #dir_elem) == ".obj" then
			
local file = io.open(target, "r")
			

			
--searches match for v character
				
for match in file:lines("v") do


				
--Writes GL_Vertex3f for every occurence of v
				
input:write(" GL_Vertex3f ")

				
end



				
for match in file:lines("%A") do


					
 input:write("",match)


end
end

end
end

i forgot to mention im using lua for this coding ! :)

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

so instead of file:lines() i can just put this ? and this will allow my text output be created?

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.

oh okay thanks so how the hell would i read the contents in this case because that is what i'm stuck on at the moment ?

i was thinking I would read the actual files based of there filenames but i don't know how to actual read the files if there filenames are matched like you showed above ^^

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

would you be able to rewrite the code so that so it works fully so i can learn it will work so i can understand it better ? :)

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

okay so I made changes to my code but the output of objectoutput.txt doesn't show the %A and v doesn't show the gl_vertex3f in the objectoutput.txt am i missing something simple?

require "lfs"




	input = io.open("ObjectOutput.txt","w")



	filename = ".obj"



	pattern = "\.obj$"


	for dir_elem in lfs.dir("test folder") do


	local target = "test folder" .. "/" .. dir_elem



	if lfs.attributes(target).mode == "file" then



	if string.match(filename,pattern) ~= nil then



	local file = io.open(target, "r") do

--start here

	for line in file:lines() do

	if string.match(line, "%V") then do



	input:write(" GL_Vertex3f ")


	end
	end



	for line2 in file:lines() do

	if string.match(line2, patterna) then do



	input:write(" ", line2)



	end
	end





--END LINE
end
end
end
end
end
end

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()

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.

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()

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.