It is currently March 29th, 2024, 9:41 am

Over write file to "reset" - My first Lua Script

Discuss the use of Lua in Script measures.
Ranger
Posts: 6
Joined: October 20th, 2011, 9:30 pm
Location: Denmark

Over write file to "reset" - My first Lua Script

Post by Ranger »

Hi, just finished my first Script. :D
The Skin/Script helps me control an USB-relay unit with 8 channels. (see the attached image)
To make Lua read a file is quiet easy, but to make Lua write is more difficult.
My progress:
1. the Script didn't write to file at all.
2. the Script worked for the first time, but did only write the first line.
3. the Script worked better, but it did write the whole file in one line.
4. found out about the "\n" switch and how important single quotes can be.
And now it works like a charm.

I appreciate every comment.

Code: Select all

function Initialize()

end

function Reset()

	local FilePath = SKIN:MakePathAbsolute('ISTXb.inc')
	local File = io.open(FilePath, 'r')

	if not File then
	  print('Text1: unable to open file for read at ' .. FilePath)
	  return
	end

	local Contents = {}
	
	for Line in File:lines() do
	  table.insert(Contents, Line)
	end

	File:close()
	
	local FilePath = SKIN:MakePathAbsolute('ISTX.inc')
        local File = io.open(FilePath, 'w')

	if not File then
	  print('Text1: unable to open file for write at ' .. FilePath)
	  return
	end
	
        local iCount = #Contents
	
        for i = 1, iCount do
          File:write(Contents[i], '\n')
       end
	
       File:close()
	[color=#40FF80]--SKIN:Bang('!Refresh', 'Vista Rainbar\Power Chose', 'PowCho.ini')[/color]
	
end

You do not have the required permissions to view the files attached to this post.