It is currently April 23rd, 2024, 10:33 pm
Creating a new text file from Lua
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Creating a new text file from Lua
assert does write to the log on error, which is fine. I prefer my approach for checking on file actions, but either works, and assert can certainly be very useful in other instances like checking that a data type is correct and all kinds of other things.
-
- Posts: 608
- Joined: February 7th, 2011, 7:27 pm
- Location: Thessaloniki, GR
Re: Creating a new text file from Lua
Regarding jsmorley's code in the previous posts:
Everything works well with that. But if I want to write the file in a subdirectory? Something like that:
I know that it doesn't work, I hope you get what I mean though.
UPDATE: Ok, I set another variable in the skin,
and then I got it inside the lua script. Is there any prettier way?
OFFTOPIC: I finally started messing with .lua!
Code: Select all
MyFile = io.open(CurrPath..'SomeFile.txt', 'w')
Code: Select all
MyFile = io.open(CurrPath..'SubFolder\SubFolder2\SomeFile.txt', 'w')
UPDATE: Ok, I set another variable in the skin,
Code: Select all
path=#CurrentPath#SubFolder\SubFolder2\
OFFTOPIC: I finally started messing with .lua!
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Creating a new text file from Lua
What you can do is create a file anywhere you want, as
certainly works fine (note the "/" instead of "\" for the path separators, you can also use "\\" as well, but "\" alone will be seen as an "escape".)
What you can't do is create a file in a sub-folder that does not already exist. I mean you can, but the only way is to use the os.execute() function and the DOS "MKDIR" command, which in the context of a Rainmeter skin will be ugly as it will pop open a DOS window for just a second. Assuming you only need to do it once when a user first runs your skin, that may be ok, but if that were the case, you can just create the folder in your skin folder before you distribute the skin anyway. If it is something you are going to do repeatedly, it will be ugly in a Rainmeter skin...
P.S. Here is a nice little function that will check to see if a folder exists, and only create it if needed.
Code: Select all
function Initialize()
CurrPath = SKIN:GetVariable('CURRENTPATH')
end
function Update()
MyFile = io.open(CurrPath..'Newfolder/SomeFile.txt', 'w')
if not MyFile then print('Cannot create file: SomeFile.txt'); return 'File Error'; end
MyFile:write('Hello World\n')
MyFile:close()
end
What you can't do is create a file in a sub-folder that does not already exist. I mean you can, but the only way is to use the os.execute() function and the DOS "MKDIR" command, which in the context of a Rainmeter skin will be ugly as it will pop open a DOS window for just a second. Assuming you only need to do it once when a user first runs your skin, that may be ok, but if that were the case, you can just create the folder in your skin folder before you distribute the skin anyway. If it is something you are going to do repeatedly, it will be ugly in a Rainmeter skin...
P.S. Here is a nice little function that will check to see if a folder exists, and only create it if needed.
Code: Select all
function CheckDir(path, bOneTime)
local tbl = GetDirListTable(path);
if tbl == "" then
os.execute('MKDIR "'..path..'"')
if bOneTime then
return CheckDir(path, false)
else
ShowMsg("New directory '"..path.."' could not be created, required for script to run");
return false;
end;
else
return true;
end;
end;
-
- Posts: 608
- Joined: February 7th, 2011, 7:27 pm
- Location: Thessaloniki, GR
Re: Creating a new text file from Lua
The path separator never stops to mess with me. Regarding to the folder creation, searching through the google results I bumped on the mkdir way of making folders, but fortunately my skin doesn't require it. I will have them made beforehand.
Thanks for the function you also provided, nice example of reference if I try to create one by myself. Could you explain what exactly is bOneTime? Is it a boolean value? As I understand, the Checkdir function replies in boolean (true/false).
Little offtopic: I don't know if it could work here, but I 've found a messy way of running a .cmd window without it appearing, using a .vbs file. It will be included in the skin I'm trying to make.
Thanks for the function you also provided, nice example of reference if I try to create one by myself. Could you explain what exactly is bOneTime? Is it a boolean value? As I understand, the Checkdir function replies in boolean (true/false).
Little offtopic: I don't know if it could work here, but I 've found a messy way of running a .cmd window without it appearing, using a .vbs file. It will be included in the skin I'm trying to make.
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Creating a new text file from Lua
Yeah, bOneTime is a boolean.KreAch3R wrote: The path separator never stops to mess with me. Regarding to the folder creation, searching through the google results I bumped on the mkdir way of making folders, but fortunately my skin doesn't require it. I will have them made beforehand.
Thanks for the function you also provided, nice example of reference if I try to create one by myself. Could you explain what exactly is bOneTime? Is it a boolean value? As I understand, the Checkdir function replies in boolean (true/false).
Little offtopic: I don't know if it could work here, but I 've found a messy way of running a .cmd window without it appearing, using a .vbs file. It will be included in the skin I'm trying to make.
As to your .vbs script, the problem is that in lua, running the vbs script itself will pop a DOS window. No way I have ever found to beat this unless and until we get some addon library to work with both 32 and 64 bit implementations of our Lua interface that can do Windows API calls and other clever stuff. Someday...