3

How to create a new file by script on Mikrotik Routerboard (Router OS 6.30)?

lukyer
  • 7,595
  • 3
  • 37
  • 31
  • A good question. To the people who closed this question as too broad: Why exactly is the question too broad? Was this question closed based on assumptions without knowledge of the subject matter? – jkj Feb 10 '17 at 18:26
  • I didn't have enough reputation to do anything about it, sorry. Probably i can't even now. – lukyer Feb 11 '17 at 10:41

1 Answers1

3

UPDATE: This solutions also does not work well. It seems to be impossible to create a file and edit (/file set) it during one script call. I have no idea why, but only working solution is to run special create script two times (first creates a file and second edits it).

This easy task took me long time to solve. There is no official way, only some ugly workarounds. The main idea is create a file using some export operation (or fetch from network operation) and then flush created file.

Another problem is that multiple commands in one script file somehow cannot use created file's identifier. So instead of:

/file print file=newFile; /file set newFile contents=""

you must use:

/file print file=newFile; /file set [find name="newFile"] contents=""

Notice that first command just exports list of all files and second one flushes its content.

lukyer
  • 7,595
  • 3
  • 37
  • 31
  • My actual solution is server side script, which takes get parameter with content it should return as a file. Then on mikrotik i just call: /tool fetch url="https://likewifi.lukyer.cz/generateFile.php?content=0" dst-path="createdFile.txt" – lukyer Jul 24 '15 at 13:21
  • Or if you don't mind extra newline, you can use: :execute script=":put yourText" file="newFile" – lukyer Jul 24 '15 at 13:27
  • update for new routeros: use {} instead of "" for executing script with variables ``` :local $data [some sort of scipts to get data] :execute script={:put $data} file="newFile" ``` – quickbrown Jul 11 '21 at 22:20