scripting with for each loop

i’ve some files each starting with “mikrotik” prefix, like mikrotik-1234, mikrotik-5678 and so on.need to do a loop scripting like "for each $filename do /tool fetch $filename " so that i can send all files through ftp with same name with help of /tool fetch command

thanks in advnace

:local filename;
:foreach file in=[/file find where name~“mikrotik”] do={
:set $filename [/file get $file name];
/tool fetch src-path=$filename …
}

thanks a lot . its working..

Hello,
Could you please provide me your script, as Im working on my own with no success, below my script:

:local filename;
:foreach filename in=[/file find where name~“routeros-mipsbe”] do={
:set $filename [/file get $file name];
/tool fetch mode=ftp upload=no address=x.x.x.x port=x user=“x” password=“x” src-path=$filename dst-path=“routeros-mipsbe-6.47.2.npk” keep-result=yes
}

Hey topic hopper!

http://forum.mikrotik.com/t/syntax-to-wildcard-the-rest-of-path/142735/1

:set $filename [/file get $file name];
/tool fetch ...................dst-path="routeros-mipsbe-6.47.2.npk" keep-result=yes



:set $filename [/file get $file name];
/tool fetch .............dst-path="$filename keep-result=yes

You don’t know in advance what the source filename is so you also don’t know the destination name.

also tried with no success,

I only brought forward a logical error.

The next error you make is using foreach and it’s counter is changed in the loop to an other value.

Thirdly, you can’t use variable names that are already used by RouterOS.

You should check you code better and make you variable names unique.

{
:local fileName;
:foreach fileCounter in=[/file find where name~"routeros-mipsbe"] do={
:set $fileName [/file get $fileCounter name];
/tool fetch mode=ftp upload=no address=x.x.x.x port=x user="x" password="x" src-path=$fileName dst-path=$fileName keep-result=yes
}

I can’t test it and it is way over my head how it works. :wink:

thank you for your help!

My goal is to write a script to get packages from remote RouterOS(x.x.x.x) using /tool fetch mode=ftp…, in src-path I need to wildcard the rest of name of package, example if package with name “routeros-mipsbe-6.47.2.npk” i need only to put in src-path “routeros-mipsbe-WHATEVER”, what shall i use as expression/character in Mikrotik script to express the WHATEVER?

Please note that this script (/tool fetch mode=ftp upload=no address=x.x.x.x port=x user=“x” password=“x” src-path=“/routeros-mipsbe-6.47.2.npk” dst-path=“routeros-mipsbe-6.47.2.npk” keep-result=yes;) working well!!

My adaptation needs a extra “}” in the last line.

You have to check what value is in $fileName:

:set $fileName [/file get $fileCounter name]; :put $fileName;

So you can check if yuo have to also a “/” in front of the src-path

src-path=“/$fileName”

{
:local fileName
:foreach fileCounter in=[/file find name~"routeros-mipsbe"] do={
:set $fileName [/file get $fileCounter name]
:do {
/tool fetch mode=ftp upload=no address=x.x.x.x port=x user="x" password="x" src-path="/$fileName" dst-path="/$fileName" keep-result=yes
:log info "Copied file: $fileName"
} on-error={ :log warning "Matched file $fileName has not been copied."} 
}}