i don't know what probelm in my interface script

i want to save tx-bytes from interface to text file and update it every 1hour

can any body help my :smiley: :smiley:

/interface {
:local tx                           
:local name                     
:local fname                    
:local fcontent 0
:fore s in=[/interface find] do={  
:set tx [get $s tx-byt]           
:set tx ($tx / 1048576)
:set name [get $s name]                               
:set name ("Mostafa_".$name.".txt")                   
#:put $name                        
:fore i in=[/file find] do={
:set fname [/file get $i name]
:if ($name != $fname) do={     
/file print file=$name;
:delay 2s
/file set $name contents="$tx"; 
} else={
:put "*****************************************"
:put "found !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
:put $name                                      
:put $fname
:put "*****************************************"
:set fcontent [/file get $name contents];       
:set fcontent ($fcontent/1048576)        
:put $fcontent                   
:if ($fcontent < $tx) do={
:put "$fcontent < $tx"   
:set fcontent $tx     
/file set $name contents="$fcontent";
} else={                             
:if ($fcontent > $tx) do={
:put "$fcontent > $tx"   
:set fcontent [$tx - $fcontent]
:set fcontent [$fcontent + $tx]
/file set $name contents="$fcontent";
}                                    
}
}                                                
}  
} 
}

I have no idea what you try to do when $tx < $fconetent, but here goes:

/interface {
    :local tx
    :local fname
    :foreach s in=[find] do={
        :set tx [get $s tx-byte]
        :set tx ($tx / 1048576)
        :set fname [get $s name]
        :set fname ("Mostafa_".$fname.".txt")
        /file {
            :local f [find name=$fname]
            :if ([:len $f] = 0) do={
                print file=$fname where size=-1;
                :delay 2s
                set [find name=$fname] contents="$tx";
            } else={
                :local fcontent [get $f contents];
                :if ($fcontent < $tx) do={
                    :put "$fcontent < $tx"
                    :set fcontent $tx
                    set $f contents="$fcontent";
                } else={                             
                    :if ($fcontent > $tx) do={
                        :put "$fcontent > $tx"
                        :set fcontent [$tx - $fcontent]
                        :set fcontent [$fcontent + $tx]
                        set $f contents="$fcontent";
                    }
                }
            }
        }
    }
}

thanks itโ€™s work fine but i have problem

in tx < fcontent

when router board restart itโ€™s reset tx-bytes i want tell mikrotik take tx-bytes and sum it with file content
example
like if tx-bytes now 1Gb
and
file content 5Gb

the sum will be 6Gb after 1 hour the script will work and get tx-bytes it will be 2Gb when sum the result will be 8Gb but this not correct

the Idea of script itโ€™s save tx-byte in file and update it every 1 hour and when restart router board Continue get new tx-bytes and sum with file to get the pppoe usage in month

sorry for language

Static or dynamic interfaces?

All if you can
or
dynamic interfaces only