Community discussions

MikroTik App
 
OsirisS13
just joined
Topic Author
Posts: 14
Joined: Wed Oct 08, 2008 3:15 am

Problems writing to file multiple times

Thu Dec 11, 2008 1:50 am

Hello!

I am trying to make a script that counts the number of trial users online and writes it to a file. I want it to count the number of users every hour, and add the result to a new line in the file (to be used as a CSV file). However, I think I am doing the re-write wrong, as the file includes the file name of every file on the router as well as the results.

How should I be handling this re-writing?
#Initialising Variables

#Varible to set the frommaddress to the systemname@some.server
:global system [/system identity get name]
:global time [/system clock get time]
:global date [/system clock get date]

#Variable for user name
:local userno

#Variable for text file contents
:local filetext
:local headings

:set headings ("Date,Time,Users\n")

#check if file already exists, if not create it
:local check [/file find name=usersperhourreport.txt]
:if ( $check = "" ) do= {
#Create file
	/file print file=usersperhourreport
	
#Set the headings for the columns (\n for new line)
	/file set usersperhourreport.txt contents="$headings\n"
	
}

#Pull details out of user list and assign to relevant variables accordingly
:set userno [/ip hotspot active print count-only]


#Insert the values to the text file
:set filetext ("$date,$time,$userno\n")
/file set usersperhourreport.txt contents=([/file get [/file find name=usersperhourreport.txt] contents] . "\n$filetext");
:delay 5

/tool e-mail send to="me@some.server" subject=($system . " Users per Hour Usage Report") body="Attached is the hourly usage report for $system  made at $time on $date \nNote:  The attached file is a comma seperated value file, simply import into a spreadsheet" from=libraryreports@some.server file=usersperhourreport.txt server=999.999.999.999
:log info "Report Emailed"

Here is the output of the file
# dec/11/2008  8:15: 4 by RouterOS 3.13
# software id = EQZ1-ZXT
#
 # NAME              TYPE             SIZE                 CREATION-TIME       
 0 libhs             directory        1024                 dec/10/2008 13:41:21
 1 libhs/img         directory        1024                 dec/10/2008 11:15:06
 2 libhs/lv          directory        1024                 dec/10/2008 11:15:06
 3 libhs/alogin.html .html file       1342                 dec/10/2008 11:15:06
 4 libhs/aplus.png   .png file        18448                dec/10/2008 11:15:06
 5 libhs/error.html  .html file       938                  dec/10/2008 11:15:06
 6 libhs/errors.txt  .txt file        3719                 dec/10/2008 11:15:06
 7 libhs/img/isla... .png file        27875                dec/10/2008 11:15:06
 8 libhs/img/logo... .png file        4317                 dec/10/2008 11:15:06
 9 libhs/img/Thum... .db file         6144                 dec/10/2008 11:15:06
10 libhs/login.html  .html file       6594                 dec/10/2008 13:41:21
11 libhs/logout.html .html file       1886                 dec/10/2008 11:15:06
12 libhs/lv/alogi... .html file       1352                 dec/10/2008 11:15:06
13 libhs/lv/error... .txt file        3853                 dec/10/2008 11:15:06
14 libhs/lv/login... .html file       3504                 dec/10/2008 11:15:06
15 libhs/lv/logou... .html file       1916                 dec/10/2008 11:15:06
16 libhs/lv/radve... .html file       1537                 dec/10/2008 11:15:06
17 libhs/lv/statu... .html file       2860                 dec/10/2008 11:15:06
18 libhs/md5.js      .js file         7218                 dec/10/2008 11:15:06
19 libhs/radvert.... .html file       1543                 dec/10/2008 11:15:06
20 libhs/redirect... .html file       223                  dec/10/2008 11:15:06
21 libhs/scrc_lib... .jpg file        26691                dec/10/2008 11:15:06
22 libhs/status.html .html file       3202                 dec/10/2008 11:15:06

dec/11/2008,08:17:43,0

dec/11/2008,08:20:47,0

dec/11/2008,08:20:53,0

why is it outputting all the files on the router with their details, along with router info? All I want is the last few lines. Also, why arent the headings output?

Thanks
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: Problems writing to file multiple times

Thu Dec 11, 2008 4:46 am

#Create file
/file print file=usersperhourreport

this command outputs the file listing to your file... thats why you are getting that content.
 
OsirisS13
just joined
Topic Author
Posts: 14
Joined: Wed Oct 08, 2008 3:15 am

Re: Problems writing to file multiple times

Thu Dec 11, 2008 5:39 am

Oh ok, but if I get rid of that line, then I have no command to create the file. What could I use to do this?
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: Problems writing to file multiple times

Thu Dec 11, 2008 6:01 am

there is a "/file add", but i think it's broken ... lemme ponder that one for a bit. someone else might be able to chime in.

Sam
 
OsirisS13
just joined
Topic Author
Posts: 14
Joined: Wed Oct 08, 2008 3:15 am

Re: Problems writing to file multiple times

Thu Dec 11, 2008 6:09 am

Yeah I tried that and kept getting an error....thought I might have been doing it wrong :S
[admin@Maroochy Library Test] > /file
[admin@Maroochy Library Test] /file> add
error - contact MikroTik support and send a supout file (3
[admin@Maroochy Library Test] /file> /file add
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents= header 
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents="header"
error - contact MikroTik support and send a supout file (3)
[admin@Maroochy Library Test] /file> /file add contents=header  
error - contact MikroTik support and send a supout file (3)
If you have any ideas that'd be great. :D I can get around it for now as I can make the file manually, but when this goes to production I will not be able to do it for multiple sites and would rather the script did it.
 
OsirisS13
just joined
Topic Author
Posts: 14
Joined: Wed Oct 08, 2008 3:15 am

Re: Problems writing to file multiple times

Fri Dec 12, 2008 1:30 am

I also tried
/file print file=usersperhourreport
/file set usersperhourreport.txt contents=""
however this still puts the file and router information at the top :(
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: Problems writing to file multiple times

Fri Dec 12, 2008 8:58 am

This works. I used the errors.txt file from the hotspot directory.
:put [/file get hotspot/errors.txt contents]

Just so you know...when your file gets to 4095 bytes, that is it. Add any more and the "contents" will not be editable/readable.

ADD: I posted a request for reading larger files in the next release. omega-00 posted a request for writing larger files also. We are still waiting...

And I will presume the reason the "/file add" doesn't work is: there is no input available for the filename. A blank filename would/should generate an error, don't you think? OK, MT team, get on it!! :wink:
 
OsirisS13
just joined
Topic Author
Posts: 14
Joined: Wed Oct 08, 2008 3:15 am

Re: Problems writing to file multiple times

Wed Jan 14, 2009 3:08 am

I am still having problems, anyone know why
/file print file=usersperhourreport
/file set usersperhourreport.txt contents=" "
does not create an empty file? :?
 
SurferTim
Forum Guru
Forum Guru
Posts: 4636
Joined: Mon Jan 07, 2008 10:31 pm
Location: Miramar Beach, Florida

Re: Problems writing to file multiple times

Thu Jan 15, 2009 1:19 pm

Unless that is a typo, you are trying to print a file that may not exist.

/file print file=usersperhourreport
/file set usersperhourreport.txt contents=" "

These are NOT accessing the same file. If the file usersperhourreport (no extension) doesn't exist, the script may abort at that command. It will not execute the "/file set" line.

EDIT: The command above does create the file usersperhourreport.txt. And it works ok here run from a command line. It sets even large files to a length of 1. Don't know about running as a schedule tho...

Not certain what the purpose of this script is...it writes the file with a list of filenames, then immediately erases it. ???

My usersperhourreport.txt file is right at the limit for size. Almost 4000 bytes. Another 100 bytes of filenames, and it will fail the print command.

ADD: I SEE NOW!! If "/file add" doesn't work, there is always another way of creating an empty file. VERY GOOD! :D

Try
/file print file=usersperhourreport.txt count-only

This should create a file you can edit! :wink:
 
User avatar
vmiro
Frequent Visitor
Frequent Visitor
Posts: 80
Joined: Sun Jan 29, 2006 6:53 pm

Re: Problems writing to file multiple times

Fri May 08, 2009 10:42 am

Try
/file print file=usersperhourreport.txt count-only

This should create a file you can edit! :wink:
It doesn't work, it just prints all the files on RB!

Obviously there is a problem with "/file set" command when running it inside scripts. When I create script file with something like this:
/file print file=test;
/file set test.txt content=miro;

it creates file 'test.txt', ie. executes first command, but changing the content of the file with second command isn't successful. Soon after executing sript I tried to change the content of the file via console and it works. Creating another script file with just "/file set test.txt content=miro;", and running it, doesn't change the content of the file I created with first script...
MT versions I'm working with are 3.20 and 3.22...
I use this for autobackup...but it doesn't work :(
Any solution?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Problems writing to file multiple times

Fri May 08, 2009 11:35 am

Works for me:
/file print file=test
/file set test.txt contents="lala"

:log info "$[/file get test.txt contents]";

Output:

[admin@MikroTik] /log> print where buffer=memory
...
23:48:51 script,info lala
..


So obviously you are doing something wrong, check if filename in script is typed correctly and also check scripts or schedulers policy
 
User avatar
vmiro
Frequent Visitor
Frequent Visitor
Posts: 80
Joined: Sun Jan 29, 2006 6:53 pm

Re: Problems writing to file multiple times

Fri May 08, 2009 2:09 pm

Copy-Pasted your example...there is file created but the content is not 'lala' !!!
ROS 3.22
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Problems writing to file multiple times

Fri May 08, 2009 2:16 pm

I checked on v3.23
 
User avatar
vmiro
Frequent Visitor
Frequent Visitor
Posts: 80
Joined: Sun Jan 29, 2006 6:53 pm

Re: Problems writing to file multiple times

Fri May 08, 2009 3:15 pm

Hmm, when I start script again without erasing the file (test.txt), it writes the content 'lala' in the text !
The problem exist only when creating the file first time.
I'll try with newest ver.

One more question, why "/file add" command don't work ?

Thanks ;)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Problems writing to file multiple times

Fri May 08, 2009 3:19 pm

Simply file add is not implemented and never will be.
v4.0 will have better support to work with files. In v3.x it is just a workaround with too many limitations.
 
User avatar
nest
Forum Veteran
Forum Veteran
Posts: 822
Joined: Tue Feb 27, 2007 1:52 am
Location: UK
Contact:

Re: Problems writing to file multiple times

Sat May 30, 2009 9:57 pm

/file print file=test
:delay 1
/file set test.txt contents="lala"
This works on v3.24. Seems this could be a 'feature' with timing. Without the delay, the contents 'lala' are not entered into the file. Presumably as the system hasn't completed the task of closing the file after the initial write when creating it.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Problems writing to file multiple times

Thu Dec 03, 2009 9:06 pm

Simply file add is not implemented and never will be.
v4.0 will have better support to work with files. In v3.x it is just a workaround with too many limitations.
I'm on v4.1, and am still having an issue creating files larger than 4096 bytes.

Is this still a limitation in v4.x? BTW, don't mention Lua, not sure when (or if) it's going to be implemented.

Also, I am able to split a files into pieces, (ex. file1.txt, file2.txt, file3.txt) Each file is 4096 bytes. Can I make file.txt with contents of all three, and size would be 3 x 4096 bytes?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Problems writing to file multiple times

Fri Dec 04, 2009 8:24 am

Yes, it still has size limitations because LUA was not added yet.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26376
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: Problems writing to file multiple times

Fri Dec 04, 2009 1:10 pm

Lua was canceled for v4, and will probably added in v5.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Problems writing to file multiple times

Fri Dec 04, 2009 1:19 pm

Lua was canceled for v4, and will probably added in v5.
Lol, dang that's going to be a long time! What can I do in the meantime? There has to be a way to get a file larger than 4096 bytes :o . Surely there must be a way...

Just curious, what script language is v3 and v4 currently? Is it custom?

Who is online

Users browsing this forum: No registered users and 26 guests