Community discussions

MikroTik App
 
User avatar
k6ccc
Forum Guru
Forum Guru
Topic Author
Posts: 1497
Joined: Fri May 13, 2016 12:01 am
Location: Glendora, CA, USA (near Los Angeles)
Contact:

Writing variables to a file in a script

Sat Oct 30, 2021 3:35 am

First of all, I fully admit that I understand MT's scripting language only slightly better than I understand Greek (none). And that portion of the Wiki might as well be written in Greek...

I have a script that pings a few external devices, and if the times are excessive, writes the variables into an E-Mail and sends it to me. This script runs every 5 minutes and works perfectly. Because of a short term issue, I want to have the script write the resulting data (a few local variables) into a file which will end up in Excel. So every time the script runs, it appends one line to the file. Should be simple, but everything I have tried results in the script unable to run at all if my print to file command is there. So obviously I seriously don't have the syntax right...

This is an extract of the E-Mail command (the E-Mail body is more verbose to make it more human readable - not needed for the import to Excel)
/tool e-mail send to jim@<redacted> subject=$Sub1 body=($AvgRtt1, $AvgRtt2, $AvgRtt3, $AvgRtt4, $AvgRtt5")
For the write to file, I just need the five variables that are in the E-Mail body sent to a file. Each time the script runs, another line gets added to the file.
Help...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Writing variables to a file in a script

Sat Oct 30, 2021 1:34 pm

On MikroTik, working with a file is limited to 64KB.

You can create .csv for Excel, but you must keep one eye on file size, if 64KB is reached, the script no longer works.
The file everytime have .txt at the end and you must rename it inside Windows removing .txt to open it with excel.
Set WinSep with the character used on your OS on Excel for separate values on "Comma Separated Values" file.
On Italy the character is ; on your OS can be , or other. Check yourself.
{
:global WinSep ";"
:global WinNewLine "\r\n"
:local xline "$AvgRtt1$WinSep$AvgRtt2$WinSep$AvgRtt3$WinSep$AvgRtt4$WinSep$AvgRtt5"

/file
:if ([:len [find where name="test.csv.txt"] ] = 0) do={ print file=test.csv; :delay 2s; set "test.csv.txt" content="" }
set "test.csv.txt" contents="$[get "test.csv.txt" contents]$xline$WinNewLine"
}
 
User avatar
k6ccc
Forum Guru
Forum Guru
Topic Author
Posts: 1497
Joined: Fri May 13, 2016 12:01 am
Location: Glendora, CA, USA (near Los Angeles)
Contact:

Re: Writing variables to a file in a script

Sat Oct 30, 2021 7:40 pm

Thank you very much. That is working fine. I think I even understand all the commands, so maybe I learned something...

As for the file size, should not be an issue. It's only adding 17 bytes every 5 minutes, and will run for about the next 12 hours. If this is something I leave running long term, I will have it E-Mail the file to me and then delete it on some regular schedule.

Who is online

Users browsing this forum: No registered users and 22 guests