This is a simple method of appending lines to a file in RouterOS:
Bear in mind there is 4KB size limitation on read/write of files with this method
:local content [file get YOUR_FILE contents]
:set content ("$content\r\n$YOUR_VARIABLE")
/file set YOUR_FILE contents=$content
. If you want to overcome 4KB limitation you will have to write more complex routine for manipulating files and variables.