MikroTik Script: Writing Strings to a File in a Loop
To write strings to a file in a loop on MikroTik RouterOS, you need to consider a few key points:
- RouterOS scripting does not have a direct "append to file" command; you must read the file, add your new content, and write it back.
- There is a 4096-byte limit for file contents when using variables, so for large data, you may need to split the output into chunks[6][10].
- The
:execute command can be used to write output directly to a file, bypassing some limitations[10].
Below are two common approaches: accumulating data in a variable and writing once, and writing each loop iteration using :execute.
1. Accumulate Data in a Variable, Then Write to File
This is the most reliable method for small to moderate data sizes (under 4KB).
:local filename "output.txt"
:local data ""
:for i from=1 to=10 do={
:set data ($data . "Line number $i\r\n")
}
# Create the file (if not exists)
/file print file=$filename
# Wait for file creation
:delay 1
# Write the accumulated data to the file
/file set $filename contents=$data
- This script loops from 1 to 10, builds a string with each line, and writes the result to
output.txt[3][6][8].
2. Write Each Loop Iteration Using :execute
For larger outputs or to avoid the 4KB variable limit, use :execute to write each line directly to the file. This method outputs as if to the terminal, so each :put adds a newline.
:for i from=1 to=10 do={
:execute ":put \"Line number $i\"" file="output.txt"
}
- Each iteration writes a line to
output.txt[1][10].
- This method is efficient for logging or when you want to avoid accumulating data in memory.
3. Appending Data to an Existing File
MikroTik does not have a native "append" operation, but you can simulate it:
:local filename "output.txt"
:local newLine "New data\r\n"
# Read current contents
:local currentContents [/file get $filename contents]
# Add new line
:local updatedContents ($currentContents . $newLine)
# Write back to file
/file set $filename contents=$updatedContents
- This reads the file, adds the new line, and writes it back[6].
Important Notes
- File Size Limit: If your data exceeds 4096 bytes, you must split it into multiple files or use
:execute to write output in chunks[10].
- Newlines: Use
\r\n for Windows-style newlines, or \n for Unix-style. Each :put in :execute adds a newline automatically[1][5].
- File Creation: Always ensure the file exists before writing to it. Use
/file print file=$filename to create it if needed[8].
Summary Table
| Method | Pros | Cons | Example Use Case |
|---|
| Accumulate & Write | Simple, readable, efficient | 4KB limit on data size | Small logs, configs |
:execute per line | No 4KB limit, direct writing | Less control over format | Large logs, real-time |
| Simulated Append | Appends to existing file | Inefficient for many writes | Occasional updates |
References
- Accumulating and writing after loop: [3][6][8]
- Using
:execute for direct file output: [1][10]
- File size and newline considerations: [1][5][10]
Example: Write 100 lines to a file using :execute
:for i from=1 to=100 do={
:execute ":put \"Entry $i\"" file="mylog.txt"
}
- This will create (or overwrite)
mylog.txt with 100 lines, one per loop iteration[10].
For most use cases, these patterns will cover writing strings to files in a loop on MikroTik RouterOS. Adjust the approach based on your data size and performance needs.