File does not get created in script (Mikrotik)

756 views Asked by At

Hello I've got a following script:

:local filename "test.txt"
:local content "This is a test."
   
/file print file=$filename where name="";
:delay 5
/file set $filename contents=$content;

Policies: read, write, test

But for some reason, requested file does not get created. Why is that? My hardware is Mikrotik hAP ac3, with RouterOS 7.2.3.

1

There are 1 answers

0
braggPeaks On

:local variables are not cached in Terminal, but only in scripts.

:local filename "test.txt"
:put $filename # variable is empty

You can circumvent this by either using :global instead or using { } brackets:

{
  :local filename "test.txt"
  :local content "This is a test."
   
  /file print file=$filename where name="";
  :delay 5
  /file set $filename contents=$content;
}

Tested with RouterOS v7.4.1.

Side note: File names (unfortunately) always end with .txt. Maximum file content size is 4096 characters.