Community discussions

MikroTik App
 
bobwalker
just joined
Topic Author
Posts: 23
Joined: Sun Apr 03, 2011 6:07 am

How to write string to a file using script ?

Wed Jul 06, 2016 2:38 pm

How to write string to a file using script ? Please provide a command or function, THX
 
IntrusDave
Forum Guru
Forum Guru
Posts: 1286
Joined: Fri May 09, 2014 4:36 am
Location: Rancho Cucamonga, CA

Re: How to write string to a file using script ?

Wed Jul 06, 2016 7:06 pm

 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: How to write string to a file using script ?

Tue Sep 06, 2016 4:02 pm

# Create a file with specific content.
{
# Variables.
    :local filename "test.txt"
    :local content "This is a test."
    
# Create file.
    /file print file=$filename
# Set file's content.
    /file set $filename contents=$content
}
Be aware that there is a maximum file size that RouterOS will allow you to access using scripts.
I forget the size and method though, but at some point [/file get $filename contents] just starts either giving blank results or throw errors.
 
dana1975
Frequent Visitor
Frequent Visitor
Posts: 94
Joined: Sat Jul 04, 2009 8:32 pm

Re: How to write string to a file using script ?

Fri Mar 02, 2018 11:00 am

your script is not working
 
User avatar
Deantwo
Member
Member
Posts: 331
Joined: Tue Sep 30, 2014 4:07 pm

Re: How to write string to a file using script ?

Fri Mar 02, 2018 11:50 am

your script is not working
Your comment is not helping.
Jokes aside. Helps if you tell me how it isn't working or what is going wrong.

You might need a delay between creating the file and changing the content.
# Create a file with specific content.
{
# Variables.
    :local filename "test.txt"
    :local content "This is a test."
    
# Create file.
    /file print file=$filename where name=""
# Wait for the file to be created.
    :delay 1
# Set file's content.
    /file set $filename contents=$content
}
Works for me, but there are times when this will stop working and if it takes too long to create the file it won't be able to write.
 
User avatar
doneware
Trainer
Trainer
Posts: 647
Joined: Mon Oct 08, 2012 8:39 pm
Location: Hungary

Re: How to write string to a file using script ?

Thu Apr 19, 2018 12:02 am

also note, due to mikrotik's implementation, variable value sizes can be up to 4096 bytes.
this ultimately limits the file size you create this way.

Who is online

Users browsing this forum: No registered users and 27 guests