I’m trying to concatenate every file in a folder in lua to compile a bunch of logs into one master log and send it off to someone. I’m using the ifs library to iterate through every file in a directory, then reading it all in and trying to append it to the master file.
for name in lfs.dir("logs") do
if(name ~= "." and name ~= "..") then
local path = "logs/"..name
print (path)
local file=io.open(path,"R")
print "2"
local content = io.read("*all")
print "3"
io.close(file)
local f=io.open("log.csv","A")
file:write(content)
io.close(f)
end
end
There are two issues.
The ifs library returns “.” and “..” before the other file names [is there a better way to ignore these than an if statement?]
using the bit I found here: How to load all files from a directory?
The important issue is that my command prompt keeps crashing when I test the file. It prints the path (a good one), then it crashes before getting to the “2” and I’m not sure why. The file exists and I can manipulate it by adding lines to it in another function.
Any help would be greatly appreciated.
To avoid checking for
"."and".."you should uselfs.attributesand itsmodefield to see if each item is a file or directory (or something else).Instead of
io.readyou probably wantfile:read— this might be the cause of your “crash.”I suggest you use
"r"and"a+"for theio.openmode arguments.Oh, and use
f:writeto writecontent