N
N
Nik2019-05-06 05:11:44
Mikrotik
Nik, 2019-05-06 05:11:44

How to reformat the time in the config file name on Mikrotik from 00:00:00 to 00-00-00?

I am writing a Mikrotik script to automatically save a backup to an FTP server.
I found a script on the Internet. It looks like this:

log info "Starting Automatic Backup Script"
:global thisdate [/system clock get date]
:global time [/system clock get time]
:global datetimestring ([:pick $thisdate 0 3] ."-" . [:pick $thisdate 4 6] ."-" . [:pick $thisdate 7 11])
:global backupfilename ([/system identity get name]."_".$datetimestring."_$time")
:local ftpusername "mikrotik"
:local ftpuserpassword "Aa1234567"
:local ftphostname "10.7.8.120"
/system backup save name="$backupfilename"
:delay 5s
/export compact file="$backupfilename"
:log info "Please wait…!!!"
:delay 5s
:log info "Sending Backup Mikrotik to FTP Server…………."
/tool fetch address="$ftphostname" src-path="$backupfilename.backup" user="$ftpusername" password="$ftpuserpassword" port=21 upload=yes mode=ftp dst-path="$backupfilename.backup"
:delay 1
/tool fetch address="$ftphostname" src-path="$backupfilename.rsc" user="$ftpusername" password="$ftpuserpassword" port=21 upload=yes mode=ftp dst-path="$backupfilename.rsc"
:delay 1
/file remove "$backupfilename.backup"
/file remove "$backupfilename.rsc"
:log info "Finished Backup Script…!!!!"

There is a line with a variable_$time
:global backupfilename ([/system identity get name]."_".$datetimestring."_$time")

The bottom line is that this variable writes it as 00:00:00, and Windows does not understand this file format, stupidly does not allow characters in the file name :, how can I convert these characters to -so that Windows will allow me to save the config to FTP?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav Valsinats, 2019-05-06
@jesterOK

Can I try.
I myself did not write scripts for Mikrotik, but I wrote for other systems. Let's look at the strings:
1) :global thisdate [/system clock get date]
2) :global time [/system clock get time]
3) :global datetimestring ([:pick $thisdate 0 3] ."-" . [: pick $thisdate 4 6] ."-" . [:pick $thisdate 7 11])
1) We get the date and push it into a variable in some form
2) In the variable time we just push the time in some form
3) In we FORMAT the datetimestring variable and push the date in the form we need.
Therefore, we need to do the same with time. What would I do =)
1) Create a variable of type:
:global timestring
2) would process the rule, as indicated by the friend above - ([:pick $thisdate 0 3] ."-" . [:pick $thisdate 4 6] ."-" . [:pick $thisdate 7 11])
If pick - this is a cut of a substring of a given range from a variable (thisdate ), then you can understand that from the beginning, a character is cut from 0 to 3, combined with the character "-", then from 4 to 6 a character from the variable, then we combine it with "-", and then take the last part from 7th to 11th character and concatenate it with "-".
It seems to be clear??
THEN, ACCORDING TO YOUR words, if the time variable gives 00:00:00, where let's say HH.MM.SS does not matter, then for modification you need to add a line after time
:global timestring ([:pick $thisdate 0 1] ." -" . [:pick $thisdate 3 4] ."-" . [:pick $thisdate 6 7])
And then correct the output file name formation line:
It was like this -
:global backupfilename ([/system identity get name]."_".$datetimestring."_$time")
It will become like this -
:global backupfilename ([/system identity get name ]."_".$datetimestring."_$timestring ")
Try

M
Moskus, 2019-05-06
@Moskus

Will you be able to rewrite and apply this function for your needs?

:global replaceChar do={
  :for i from=0 to=([:len $1] - 1) do={
    :local char [:pick $1 $i]
    :if ($char = $2) do={
      :set $char $3
    }
    :set $output ($output . $char)
  }
  :return $output
}

D
Dmitry Shitskov, 2019-05-06
@Zarom

:global datetimestring ([:pick $thisdate 0 3] ."-" . [:pick $thisdate 4 6] ."-" . [:pick $thisdate 7 11])

Do the same with the time variable. Like here, split the date(time) into smaller parts and substitute "-" where needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question