Hello, have you ever made your own command or any other kind of script and you wanted to save some information in a text file?
Okay, here we go; your dream has just became true! :thumbup:
Code for saving to files:
This script saves the text "HERE GOES THE TEXT" BUT it also reset the whole file when it reach 1000 lines :blink:
If you don't want that:
And keep \n there, it makes the newline, otherwise it will all come like "[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE" and you don't want that to happen, huh?
Also, if the file doesn't exist it will be created!
The "write modes" for io.open() is:
And a simple fileExists function:
Returns 1 if file exists, and 0 if not exists.
An other version:
Returns true if file exists, and false if not exists.
The date formats:
Gimme rep if you like/will use it
Regards,
Colandus
Okay, here we go; your dream has just became true! :thumbup:
Code for saving to files:
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true, linesMax=1000}
if (saveFile.saveLog == true) then
local lines = 0
for _ in io.lines(saveFile.folder .. saveFile.filename) do
lines = lines+1
end
local file = io.open(saveFile.folder .. saveFile.filename, (lines < linesMax-1 and "a") or "w")
file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n")
file:close()
end
This script saves the text "HERE GOES THE TEXT" BUT it also reset the whole file when it reach 1000 lines :blink:
If you don't want that:
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true}
if (saveFile.saveLog == true) then
local file = io.open(saveFile.folder .. saveFile.filename, "a")
file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n")
file:close()
end
And keep \n there, it makes the newline, otherwise it will all come like "[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE" and you don't want that to happen, huh?
Also, if the file doesn't exist it will be created!
The "write modes" for io.open() is:
That's for io.open("file", "a")"r" read mode (the default);
"w" write mode;
"a" append mode;
"r+" update mode, all previous data is preserved;
"w+" update mode, all previous data is erased;
"a+" append update mode, previous data is preserved, writing is only allowed at the end of file.
And a simple fileExists function:
PHP:
function fileExists(filename)
file=io.open(filename, "r")
return (file == nil and 0) or file:close() and 1
end
PHP:
if fileExists(filename) == 1 then
...
end
An other version:
PHP:
function fileExists(filename)
file=io.open(filename, "r")
return (file ~= nil and file:close())
end
PHP:
if fileExists(filename) then
...
end
The date formats:
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´
Gimme rep if you like/will use it
Regards,
Colandus
Last edited: