It is currently April 25th, 2024, 7:11 am

Creating a new text file from Lua

Discuss the use of Lua in Script measures.
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Creating a new text file from Lua

Post by FatherCode »

I tried this:

Code: Select all

	local f = io.open(name..".txt", "w")
	f:write("<Title=\""..name.."\"/>\n")
	f:close()
Just a quick little test to see if I could create text files from within Lua but it fails =/
Is there any way to create text files from Lua?
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a new text file from Lua

Post by jsmorley »

If you simply want to to create a new file, or overwrite the contents of an existing file, you can use:

function Initialize()

CurrPath = SKIN:GetVariable('CURRENTPATH')

end

function Update()

MyFile = io.open(CurrPath..'SomeFile.txt', 'w')
if not MyFile then print('Can't create file: SomeFile.txt'); return 'File Error'; end

MyFile:write('Hello World\n')

MyFile:close()

end

Getting the skin's path is important, as when Lua is running, the working directory is NOT your skin's path...
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Creating a new text file from Lua

Post by FatherCode »

But that's the problem =/
I can't seem to create text files, opening one works but not creating.

The function (for now) is:

Code: Select all

function newDeck (name)
	local f = io.open(name..".txt", "w")
	f:write("<Title=\""..name.."\"/>\n")
	f:close()
end
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a new text file from Lua

Post by jsmorley »

FatherCode wrote:But that's the problem =/
I can't seem to create text files, opening one works but not creating.

The function (for now) is:

Code: Select all

function newDeck (name)
	local f = io.open(name..".txt", "w")
	f:write("<Title=\""..name.."\"/>\n")
	f:close()
end
See my edited post...
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Creating a new text file from Lua

Post by FatherCode »

Oh...
It works now, thanks =x

While I'm at it, I might as well ask:
What happened to this link?
http://code.google.com/p/rainmeter/wiki/BuildSummary
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a new text file from Lua

Post by jsmorley »

FatherCode wrote:Oh...
It works now, thanks =x

While I'm at it, I might as well ask:
What happened to this link?
http://code.google.com/p/rainmeter/wiki/BuildSummary
http://code.google.com/p/rainmeter/wiki/Building
FatherCode
Posts: 21
Joined: October 8th, 2011, 4:01 pm

Re: Creating a new text file from Lua

Post by FatherCode »

Thanks you :D
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Creating a new text file from Lua

Post by Yggdrasil »

If you work with files and io.open, assert is your best friend :)
local f,e = io.open("myfile.txt","w")
assert(f,e)
If the program is unable to open the file, you can see the error message description in the logs.
Image
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Creating a new text file from Lua

Post by jsmorley »

Yggdrasil wrote:If you work with files and io.open, assert is your best friend :)
local f,e = io.open("myfile.txt","w")
assert(f,e)
If the program is unable to open the file, you can see the error message description in the logs.
Yes, and that is one good approach. I do actually prefer:

MyFile = io.open(CurrPath..'SomeFile.txt', 'w')
if not MyFile then print('Can\'t create file: SomeFile.txt'); return '-1'; end

As not only do I get a very specific (which file?) error in the log, rather than a generic "file or directory doesn't exist", but this also gracefully breaks out of the Update() function in our standard Lua script for Rainmeter, and returns control to the skin. It also returns a value (-1 in this case) that can be tested for in the skin to take some additional action if the file access fails.
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Creating a new text file from Lua

Post by Yggdrasil »

You get three values back from io.open, the 3rd one is the error code.
Well, maybe assert is not the proper function, maybe you should rewrite it in application side (overwrite it) to somehow interact with the user, when something goes wrong.
By the way, the error message from io.open contains the file and the path too:

Code: Select all

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> f,e,i = io.open("H:/test.txt","w")
> print(f,e,i)
nil     H:/test.txt: No such file or directory  2
> f,e,i = io.open("c:/test.txt","w")
> print(f,e,i)
file (734D1BD8) nil     nil
>
Image