Community discussions

MikroTik App
 
Liiina
newbie
Topic Author
Posts: 26
Joined: Mon Apr 15, 2024 10:21 pm

copying files

Thu Feb 13, 2025 6:11 pm

good afternoon,

I have this script for copy one file
:local buffer [/file get [/file find name="flash/tmp1/ubuntu_rsa"] contents]
/file print file=("tmp1/ubuntu_rsa"."\00")
/delay 1s
/file set tmp1/ubuntu_rsa contents="$buffer"
and it works great,

how to modify it so that it also copies all files from folder A to folder B?

I asked the chatbot Mikrotik and GPT, they gave something similar, but it still doesn’t work
# Define source and destination directories
:local sourceDir "/source-directory/"
:local destDir "/destination-directory/"

# Get a list of all files in the source directory
:local fileList [/file find where name~"$sourceDir"]

# Loop through each file in the source directory
:foreach file in=$fileList do={
    # Get the file name
    :local fileName [/file get $file name]
    
    # Read the contents of the file
    :local fileContents [/file get $file contents]
    
    # Define the destination file path
    :local destFile ($destDir . [:pick $fileName [:len $sourceDir] [:len $fileName]])
    
    # Write the contents to the destination file
    /file print file=$destFile contents=$fileContents
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12861
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: copying files

Thu Feb 13, 2025 6:37 pm

Image
 
optio
Forum Guru
Forum Guru
Posts: 1063
Joined: Mon Dec 26, 2022 2:57 pm

Re: copying files

Thu Feb 13, 2025 7:22 pm

@Liiina Did you try how your current copy script works with large files (eg. 1GB file size)? See here for possible workaround if you gonna go into this endeavour.
 
Liiina
newbie
Topic Author
Posts: 26
Joined: Mon Apr 15, 2024 10:21 pm

Re: copying files

Thu Feb 13, 2025 7:32 pm

thanks for the answers, but i don't need to copy large files, just three dozen files witch configurations, macros, rsa keys, etc. the largest file does not exceed 1.5 KB. enabling external services is not allowed.

i would be very grateful if someone rewrote my single file script into a proper workflow with
:foreach file in=$fileList do={
..
}
 
Liiina
newbie
Topic Author
Posts: 26
Joined: Mon Apr 15, 2024 10:21 pm

Re: copying files

Sat Feb 15, 2025 5:05 am

:local sourceDir "source-directory"
:local destDir "destination-directory"

:local fileList [/file find where name~("$sourceDir" . "/")]

:foreach file in=$fileList do={
    :local fileName [/file get $file name]
    :local fileContents [/file get $file contents]
    :local destFile ($destDir . [:pick $fileName [:len $sourceDir] [:len $fileName]])

    /file print file=("$destFile"."\00")
    /delay 3s
    /file set $destFile contents=$fileContents
}
it seems to work, I had to do it myself :(

Is there another way to create a file and write the contents in a way that is not as stupid as "print", "set" in a file?
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3261
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: copying files

Sat Feb 15, 2025 10:07 am

...
it seems to work, I had to do it myself :(
..
I do not know if I should laugh or cry?
 
optio
Forum Guru
Forum Guru
Posts: 1063
Joined: Mon Dec 26, 2022 2:57 pm

Re: copying files

Sat Feb 15, 2025 12:25 pm

it seems to work, I had to do it myself :(
Best way to learn new things.
Is there another way to create a file and write the contents in a way that is not as stupid as "print", "set" in a file?
On which ROS version? There is file add command introduced in some 7.x ver., see doc: https://help.mikrotik.com/docs/spaces/R ... rdirectory where with single command call file with contents can be created.
 
Liiina
newbie
Topic Author
Posts: 26
Joined: Mon Apr 15, 2024 10:21 pm

Re: copying files

Sat Feb 15, 2025 12:32 pm

I do not know if I should laugh or cry?
I'm sad that I had to do everything myself
Image
 
Liiina
newbie
Topic Author
Posts: 26
Joined: Mon Apr 15, 2024 10:21 pm

Re: copying files

Sat Feb 15, 2025 12:39 pm

On which ROS version? There is file add command introduced in some 7.x ver., see doc: https://help.mikrotik.com/docs/spaces/R ... rdirectory where with single command call file with contents can be created.
thanks for the help, changed it to

/file add name=("$destFile"."\00") type=file contents=$fileContents
# /file print file=("$destFile"."\00")
# /delay 3s
# /file set $destFile contents=$fileContents


and everything became faster and better!
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3261
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: copying files

Sat Feb 15, 2025 12:57 pm

I do not know if I should laugh or cry?
I'm sad that I had to do everything myself
Are you convinced that a forum is a place where dozens of users just wait on the couch, tapping their feet impatiently, waiting to solve others' problems?
Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12861
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: copying files

Sat Feb 15, 2025 1:31 pm

And then some users who feel like they are the second choice after FakeGPT, they are f–g about.