Page 1 of 1

copying files

Posted: Thu Feb 13, 2025 6:11 pm
by Liiina
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
}

Re: copying files

Posted: Thu Feb 13, 2025 6:37 pm
by rextended
Image

Re: copying files

Posted: Thu Feb 13, 2025 7:22 pm
by optio
@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.

Re: copying files

Posted: Thu Feb 13, 2025 7:32 pm
by Liiina
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={
..
}

Re: copying files

Posted: Sat Feb 15, 2025 5:05 am
by Liiina
: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?

Re: copying files

Posted: Sat Feb 15, 2025 10:07 am
by BartoszP
...
it seems to work, I had to do it myself :(
..
I do not know if I should laugh or cry?

Re: copying files

Posted: Sat Feb 15, 2025 12:25 pm
by optio
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.

Re: copying files

Posted: Sat Feb 15, 2025 12:32 pm
by Liiina
I do not know if I should laugh or cry?
I'm sad that I had to do everything myself
Image

Re: copying files

Posted: Sat Feb 15, 2025 12:39 pm
by Liiina
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!

Re: copying files

Posted: Sat Feb 15, 2025 12:57 pm
by BartoszP
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

Re: copying files

Posted: Sat Feb 15, 2025 1:31 pm
by rextended
And then some users who feel like they are the second choice after FakeGPT, they are f–g about.