Community discussions

MikroTik App
 
tabate47
Long time Member
Long time Member
Topic Author
Posts: 510
Joined: Wed Mar 13, 2013 5:23 am
Location: Los Angeles

If Then script just won't work

Sun May 14, 2023 1:45 am

The script seems to be working fine except for the else command. When the file does not exist, it never gets logged. What am I doing wrong?
:local ftpserver "xxx.xxx.com"
:local username "username"
:local password "password"
:local remotepath "router_updates/router_updates.rsc"
:local localpath "router_updates.rsc"
/tool fetch mode=ftp address=$ftpserver src-path=$remotepath user=$username password=$password dst-path=$localpath keep-result=yes
# Check if the file exists
:local file [/file find name="router_updates.rsc"]
:if ([:len $file] > 0) do={
    # File exists, do something
    :log info "Update file found."
    :delay 5s
    /import router_updates.rsc
    :delay 5s
    /file remove "router_updates.rsc"
} else={
    :log info "No updates available."
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: If Then script just won't work

Tue May 16, 2023 3:04 pm

What am I doing wrong?
1) Wrong forum section

2) You wrote the code assuming that the file always exists,
so it is obvious that if the file not exists, the script "gives" an error and crashes,
and therefore the else is useless because written like this, it will never get there...

3) Do not use the NAND (the Flash do not have this problem) more time than the necessary for store the file, simply run it from the memory.

Who is online

Users browsing this forum: No registered users and 51 guests