Fetch multiple files at same connection - is possible?

On several occasions I have to push multiple files that are inside RouterOS files and I always end up having to use foreach to do this. Or even worst... Do it manually.

It would be faster, simpler, and more economical in time and resources if you could do this using a Linux regular expression to define the files.

  • Instead of:
    • /tool fetch upload=yes src-path=file1.txt
    • /tool fetch upload=yes src-path=file2.txt
    • /tool fetch upload=yes src-path=file3.txt
    • /tool fetch upload=yes src-path=file4.txt
    • /tool fetch upload=yes src-path=file5.txt
  • Use something similar to:
    • /tool fetch upload=yes src-path=file*.txt

Is this already possible today?
If yes, how?

If not, why not?
P.S.: To me it seems like it was something like: "This was the way to prevent the user from doing stupid things like bringing into an RB750 thousand files due to a wrong command."

What about getting a list of files and iterating over it with a script?

/file print where name~"file[0-9]*\\.txt"

or maybe just?

/tool fetch upload=yes src-path~"file[0-9]*\\.txt"

he already does it on 1st case

simply src-path must accept array of file .id instead of a simple string, like:
src-path=[/file find where name~"\\.txt$"]

Thank you @BartoszP and @rextended !
Your tip solved part of my dummy doubt.

My problem now is with the target filenames.

I tried:

[someone@thisRouterOS] > /tool fetch upload=yes user=myUser password="myPass" src-path=[/file find where name~"tmpfs/folder/" type!="directory"] url="sftp://10.20.30.40/path/to/destination/directory/";
    status: failed
  uploaded: 0KiB  
failure: cannot open file: No such file or directory

I also tried with an asterisk(*):

[someone@thisRouterOS] > /tool fetch upload=yes user=myUser password="myPass" src-path=[/file find where name~"tmpfs/folder/" type!="directory"] url="sftp://10.20.30.40/path/to/destination/directory/*";
    status: failed
  uploaded: 0KiB  
failure: cannot open file: No such file or directory

And I also tried without using "url=" and put it into muti parameters, without an with the asterisk(*):

[someone@thisRouterOS] > /tool/fetch upload=yes user=myUser password="myPass" mode=sftp address=10.20.30.40 dst-path=/path/to/destination/directory/ src-path=[/file find where name~"tmpfs/folder/" type!="directory"]
    status: failed
  uploaded: 0KiB  
failure: cannot open file: No such file or directory

[someone@thisRouterOS] > /tool/fetch upload=yes user=myUser password="myPass" mode=sftp address=10.20.30.40 dst-path="/path/to/destination/directory/*" src-path=[/file find where name~"tmpfs/folder/" type!="directory"]
    status: failed
  uploaded: 0KiB  
failure: cannot open file: No such file or directory

Probably is a silly thing... But I'm stucked on it.

Any Ideas?

These weren't existing commands, but we agree with you that they should be implemented, ours were just examples...

Just for reference, here's a jerryrig I pulled out of my sleeve at some point, and I've been using it ever since.
P.S.: Is it ugly? YES!
Does it work? Poorly, with one connection for each file, but YES! IT WORKS!

:foreach myFile in=[/file print as-value where name~"tmpfs/folder/" type!="directory"] do={
    :local filePathLength [:len "$myFilesFolder/"];
    :local fileNameLength [:len ($myFile->"name")];
    /tool fetch upload=yes user=myUser \
        password="myPass" \
        src-path=($myFile->"name") \
        url="sftp://10.20.30.40/path/to/destination/directory/$[:pick ($myFile->"name") $filePathLength $fileNameLength]";
}

Makeshift is usually the most durable solution. :slight_smile:

... and then you protest when AI hallucinates, I wouldn't be surprised if most of the (non-existing) syntax often output from AI comes from these hypothetical syntaxes posted here and there as if they were real things ...

And who told the Artificial Deficence ​​to copy/use the MikroTik forum and use it as a base?

Surely one with so much Deficiency, since they users are used to post the WRONG configurations of the devices... NO???

:wink:

:rofl:

The LSP/MCP MikroTik Syntax created by our forum colleague should be input as the major reference for all IAs/LLMs.
There is something close to "robots.txt" that a companies can publish "official" information's like syntax reference?

It exists!
I didn't know about it...

image

so statement in 1st post was not entirely true because usually there is no “Eureka” moment when doing something in regular manner :slight_smile:

PS.

If your case is to transfer multiple files over sftp and avoid using fetch in iteration, you may try with sshfs disk mount, it should use single connection.

Yeah, both macOS Finder and Windows Explorer supports it natively with extensions.

As far as I know, /tool fetch still doesn’t support wildcard expansion like file*.txt directly, so the usual approach is still looping through files with foreach.

Honestly, it would be pretty useful though, especially for automation-heavy setups. I can also understand why MikroTik may have avoided it originally — one bad pattern match on a small router could suddenly trigger way more file operations than intended.

A lot of scripting limitations like this end up being less about syntax and more about keeping resource usage predictable on lower-end hardware.

Still, an added switch like mode=YESIREALLYWANTTODOTHISIKNOWWHATIAMDOING would be nice to override this (or other) artificial limitations when needed.

Or features can have runtime HW available resources check and adapt depending on it...

This looks promising.
As soon as things calm down or another need arises, I'll invest some time in it.

Haha honestly I’d probably use that mode too. Sometimes it feels like RouterOS is trying a little too hard to protect us from ourselves :smiley:

An optional “I know what I’m doing” override for advanced users would actually be pretty useful for automation-heavy setups where the current workarounds end up being more annoying than risky.