Community discussions

MikroTik App
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Script function for converting and shifting the standard date to OS Mikrotik

Sun Aug 22, 2021 5:16 pm

Script function for converting and shifting the standard date to OS MikroTik
Start variables:
* First mandatory variable in "aug/22/2021" or "08/22/2021" format.
* Second optional variable in -5 or 0 or 5 format. Specifies how many days forward or backward you want to move the total date as a result.
* Third optional variable in true or false format. The value of the "true" variable prevents you from changing the format of the month from text to number and from number to text.
:global FuncDateConvert do={
:local LocalDate $1
:local ChangeDay $2
:local NoConvertMonth $3
:local Day [:pick $LocalDate ([:find $LocalDate "/" -1] + 1) [:find $LocalDate "/" [:find $LocalDate "/" -1]]]
:local Month [:pick $LocalDate -1 [:find $LocalDate "/" -1]]
:local Year [:pick $LocalDate ([:find $LocalDate "/" [:find $LocalDate "/" -1]] + 1) [:len $LocalDate]]
:local ArrayMonth {"jan";"feb";"mar";"apr";"may";"jun";"jul";"aug";"sep";"oct";"nov";"dec"}
:local ArrayMonthDay {31;28;31;30;31;30;31;31;30;31;30;31}
:local MonthType "str"
# Check the input format and correct if necessary.
:if ( [:typeof $NoConvertMonth] = "nothing") do={:set  $NoConvertMonth "false"}
:if ( [:typeof $ChangeDay] = "nothing") do={:set  $ChangeDay 0}
:if ( [:typeof $ChangeDay] = "str") do={:set  $ChangeDay [:tonum $ChangeDay]}
:if ( [:typeof $Day] = "str") do={:set  $Day [:tonum $Day]}
:if ( [:typeof $Year] = "str") do={:set  $Year [:tonum $Year]}
# Check the data format of the Month variable and correct if necessary.
:if ( $Month~"^[0-9]{1,2}\$") do={
	:set  $Month [:tonum $Month]
	:set  $MonthType "num"
}
# Calculate the numerical equivalent for the Month variable
:if ($MonthType = "str") do={
	:foreach MonthNum,MonthStr in=$ArrayMonth do={
		:if ($MonthStr = $Month) do={:set $Month ($MonthNum + 1)}
	}
}
# Calculate date shift by specified number of days
:if ($ChangeDay > 0) do={
	:set $Day ($Day + $ChangeDay)
		:while ((($Day > ($ArrayMonthDay->($Month - 1))) and ((($Year % 4) != 0) or ((($Year % 4) = 0) and ($Month != 2)))) or \
		(($Day > (($ArrayMonthDay->($Month - 1)) + 1)) and ($Month = 2) and (($Year % 4) = 0))) do={
		:if (($Month = 2) and (($Year % 4) = 0)) do={:set $Day ($Day - (($ArrayMonthDay->($Month - 1)) + 1))
		} else={:set $Day ($Day - ($ArrayMonthDay->($Month - 1)))}
		:set $Month ($Month + 1)
		:if ($Month > 12) do={:set $Year ($Year + 1); :set $Month 1}
	}
}
:if ($ChangeDay < 0) do={
	:set $Day ($Day + $ChangeDay )
	:while ($Day < 1) do={
		:set $Month ($Month - 1)
		:if ($Month < 1) do={:set $Year ($Year - 1); :set $Month 12}
		:if (($Month = 2) and (($Year % 4) = 0)) do={:set $Day ($Day + (($ArrayMonthDay->($Month - 1)) + 1))
		} else={:set $Day ($Day + ($ArrayMonthDay->($Month - 1)))}
	}
}
# Add the character 0 to the beginning of the Day variable
:if (($Day < 10) and ([:len $Day] < 2))  do={:set $Day ("0".$Day)}
# Calculate the text equivalent for the Month variable
:if (($MonthType = "num") and ($NoConvertMonth = "false")) do={:set $Month ($ArrayMonth->($Month - 1))}
# Add the character 0 to the beginning of the Month variable
:if (($MonthType = "num") and ($NoConvertMonth = "true") and ($Month < 10)) do={:set $Month ("0".$Month)}
# Calculate the text equivalent for the Month variable
:if (($MonthType = "str") and ($NoConvertMonth = "true")) do={:set $Month ($ArrayMonth->($Month - 1))}
# Add the character 0 to the beginning of the Month variable
:if (($MonthType = "str") and ($NoConvertMonth = "false") and ($Month < 10)) do={:set $Month ("0".$Month)}
# Return data array in MM/DD/YYYY, DD, MM, YYYY format
:return {($Month."/".$Day."/".$Year);$Day;$Month;$Year}
}

Example of execution:
:put "Start date 01/01/2021"
:put ([$FuncDateConvert "01/01/2021" -1 true]->0)
:put ([$FuncDateConvert "01/01/2021" -30 true]->0)
:put ([$FuncDateConvert "01/01/2021" -31 true]->0)
:put ([$FuncDateConvert "01/01/2021" -32 true]->0)
:put ([$FuncDateConvert "01/01/2021" 1 true]->0)  
:put ([$FuncDateConvert "01/01/2021" 20 true]->0)
:put ([$FuncDateConvert "01/01/2021" 31 true]->0)
:put ([$FuncDateConvert "01/01/2021" 32 true]->0)
:put "Start date 11/01/2021"                     
:put ([$FuncDateConvert "11/01/2021" 1 true]->0)
:put ([$FuncDateConvert "11/01/2021" 20 true]->0)
:put ([$FuncDateConvert "11/01/2021" 31 true]->0)
:put ([$FuncDateConvert "11/01/2021" 32 true]->0)
:put ([$FuncDateConvert "11/01/2021" 61 true]->0)
:put ([$FuncDateConvert "11/01/2021" 62 true]->0)
:put ([$FuncDateConvert "11/01/2021" 91 true]->0)
:put ([$FuncDateConvert "11/01/2021" 92 true]->0)

Start date 01/01/2021
12/31/2020
12/02/2020
12/01/2020
11/30/2020
01/02/2021
01/21/2021
02/01/2021
02/02/2021
Start date 11/01/2021
11/02/2021
11/21/2021
12/02/2021
12/03/2021
01/01/2022
01/02/2022
01/31/2022
02/01/2022

:put "Start date 01/01/2021"
:put ([$FuncDateConvert "01/01/2021" -1 ]->0)
:put ([$FuncDateConvert "01/01/2021" -30 ]->0)
:put ([$FuncDateConvert "01/01/2021" -31 ]->0)
:put ([$FuncDateConvert "01/01/2021" -32 ]->0)
:put ([$FuncDateConvert "01/01/2021" 1 ]->0)  
:put ([$FuncDateConvert "01/01/2021" 20 ]->0)
:put ([$FuncDateConvert "01/01/2021" 31 ]->0)
:put ([$FuncDateConvert "01/01/2021" 32 ]->0)
:put "Start date 11/01/2021"                 
:put ([$FuncDateConvert "11/01/2021" 1 ]->0)
:put ([$FuncDateConvert "11/01/2021" 20 ]->0)
:put ([$FuncDateConvert "11/01/2021" 31 ]->0)
:put ([$FuncDateConvert "11/01/2021" 32 ]->0)
:put ([$FuncDateConvert "11/01/2021" 61 ]->0)
:put ([$FuncDateConvert "11/01/2021" 62 ]->0)
:put ([$FuncDateConvert "11/01/2021" 91 ]->0)
:put ([$FuncDateConvert "11/01/2021" 92 ]->0)

Start date 01/01/2021
dec/31/2020
dec/02/2020
dec/01/2020
nov/30/2020
jan/02/2021
jan/21/2021
feb/01/2021
feb/02/2021
Start date 11/01/2021
nov/02/2021
nov/21/2021
dec/02/2021
dec/03/2021
jan/01/2022
jan/02/2022
jan/31/2022
feb/01/2022

:put "Start date 01/01/2021"
:put ([$FuncDateConvert "jan/01/2021" -1 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -30 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -31 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -32 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 1 ]->0)  
:put ([$FuncDateConvert "jan/01/2021" 20 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 31 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 32 ]->0)
:put "Start date 11/01/2021"                  
:put ([$FuncDateConvert "nov/01/2021" 1 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 20 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 31 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 32 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 61 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 62 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 91 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 92 ]->0)

Start date 01/01/2021
12/31/2020
12/02/2020
12/01/2020
11/30/2020
01/02/2021
01/21/2021
02/01/2021
02/02/2021
Start date 11/01/2021
11/02/2021
11/21/2021
12/02/2021
12/03/2021
01/01/2022
01/02/2022
01/31/2022
02/01/2022
Last edited by Prometej on Tue Aug 24, 2021 11:45 am, edited 13 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 8:05 am

All occurencies of this type are wrong (for example):
:set $VarName
must be writed:
:set VarName


The script do not consider the 29 feb and everytime you add or remove days, if the operation walk over a 29 feb, obviously do wrong results.
[rextended@AP Matrix] > :put ([$FuncDateConvert "feb/29/2024" 1]->0)
03/2/2024
[rextended@AP Matrix] > :put ([$FuncDateConvert "feb/29/2024" -1]->0) 
01/2/2024


Also do that strange....
[rextended@AP Matrix] > :put ([$FuncDateConvert "jan/01/2024" -10]->0) 
01/11/2024

I suggest you to do more tests, indenting the scripts also help...
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 8:58 am

Calculation of a high year, when in February + 1 day did not realize due to the lack of technical work with fractions. This is an error in operation.
If you have a technical solution, please describe it.

Fixed error with date reduction:
:put ([$FuncDateConvert "jan/01/2024" -10]->0)
12/22/2023
:put ([$FuncDateConvert "jan/01/2024" -25]->0)
12/07/2023

Fixed the return value of the Day variable if it is less than 10. Appends 0 before the number.

Thank you for the feedback. Waiting for feedback.
Old:
:if ($Day > ($ArrayMonthDay->($Month-1))) do={
:set $Day ($Day + $ChangeDay )
:while ($Day < 1) do={
:set $Day ($Day - ($ArrayMonthDay->($Month-1)))
:set $Month ($Month - 1)
:if ($Month < 1) do={:set $Year ($Year - 1); :set $Month 12}
}}

New:
:if ($ChangeDay < 0) do={
:set $Day ($Day + $ChangeDay )
:while ($Day < 1) do={
:set $Month ($Month - 1)
:if ($Month < 1) do={:set $Year ($Year - 1); :set $Month 12}
:set $Day ($Day + ($ArrayMonthDay->($Month-1)))
}}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 9:47 am

Why use fraction?
ignoring century and other complex rules for feb 29 (if we are alive on 2100 we fix that),
simpy 2021 % 4 = 1 ... only if reminder is 0 the year have feb 29 day
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 11:16 am

Thank you for your thought.

Corrections have been scripted.
Fixed:
# Calculate date shift by specified number of days
:if ($ChangeDay > 0) do={
:set $Day ($Day + $ChangeDay)
:while ((($Day > ($ArrayMonthDay->($Month-1))) and ((($Year%4)!=0) or ((($Year%4)=0) and ($Month!=2)))) or (($Day > (($ArrayMonthDay->($Month-1))+1)) and ($Month=2) and (($Year%4)=0))) do={
:if (($Month=2) and (($Year%4)=0)) do={:set $Day ($Day - (($ArrayMonthDay->($Month-1))+1))} else={:set $Day ($Day - ($ArrayMonthDay->($Month-1)))}
:set $Month ($Month + 1)
:if ($Month > 12) do={:set $Year ($Year + 1); :set $Month 1}
}}
:if ($ChangeDay < 0) do={
:set $Day ($Day + $ChangeDay )
:while ($Day < 1) do={
:set $Month ($Month - 1)
:if ($Month < 1) do={:set $Year ($Year - 1); :set $Month 12}
:if (($Month=2) and (($Year%4)=0)) do={:set $Day ($Day + (($ArrayMonthDay->($Month-1))+1))} else={:set $Day ($Day + ($ArrayMonthDay->($Month-1)))}
}}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 2:22 pm

Some hint:

International / ISO8601 date: 2021-08-23
accept "today" parameter, instead of date, for use current date obtained from routeros
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 6:57 pm

Some hint:

International / ISO8601 date: 2021-08-23
accept "today" parameter, instead of date, for use current date obtained from routeros
Good afternoon.
Do you propose to change the date output and execute it in ISO format?
You can generate a date in any format from the output array of the function.

My goal with this feature is to convert an array that contains the dates when the files were created. In the future, sort by ascending based on the Date field and delete obsolete files.

There is a script that generates backup files in the "backup" + "rsc" formats and sends to mail.
Last edited by Prometej on Mon Aug 23, 2021 7:00 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 6:58 pm

inside routeros filesystem?
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:02 pm

inside routeros filesystem?
It is. There is a need to clean the MikroTik OS file system according to certain criteria.
Developers of MikroTik OS did not take care of the functionality of converting and comparing this in the format "Date."
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:10 pm

You accept some hint?
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:14 pm

I don't understand your thought. Try explaining more openly.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:16 pm

If you check the .id, the file on same folder are indexed on creation time, but on creation-time appear LAST MODIFIED time...
/file> :put [print as-value]
.id=*1f0001a6;creation-time=jan/02/1970 02:00:38;name=flash/pub;type=directory;
.id=*1f0001a7;creation-time=jan/02/1970 02:00:38;name=flash/firmware;type=directory;
.id=*1f0001a8;creation-time=jan/02/1970 02:00:38;name=flash/hotspot;type=directory;
.id=*1f0001a9;creation-time=jan/02/1970 02:00:38;name=flash/log;type=directory;
.id=*1f0001aa;creation-time=jan/02/1970 02:00:38;name=flash/package;type=directory;
.id=*1f0001ab;creation-time=jan/02/1970 02:00:38;name=flash/pcap;type=directory;
.id=*1f0001ac;creation-time=jan/02/1970 02:00:38;name=flash/skins;type=directory;
.id=*1f0001ad;creation-time=jan/02/1970 02:00:38;name=flash/web-proxy;type=directory;
.id=*1f0001d7;creation-time=jan/02/1970 02:00:39;name=flash/webproxy;type=directory;
.id=*1f0001d8;creation-time=jan/02/1970 02:00:39;name=flash/webproxy/error.html;size=529;type=.html file;
.id=*1f000205;creation-time=jan/01/1970 02:00:06;name=flash;type=disk;
.id=*1f000213;creation-time=aug/20/2021 12:21:20;name=flash/log/log.0.txt;size=1496;type=.txt file;
.id=*1f000295;creation-time=jan/08/2008 17:15:17;name=flash/Script_CBM.txt;size=39;type=.txt file;
.id=*1f0002b3;creation-time=jan/08/2008 17:15:17;name=flash/Script_CBM.backup;size=21979;type=backup;
.id=*1f0002f1;creation-time=jun/24/2021 16:45:36;name=flash/skins/default.json;size=4;type=.json file
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:20 pm

If you check the .id, the file on same folder are indexed on creation time, but on creation-time appear LAST MODIFIED time...
/file> :put [print as-value]
.id=*1f0001a6;creation-time=jan/02/1970 02:00:38;name=flash/pub;type=directory;
.id=*1f0001a7;creation-time=jan/02/1970 02:00:38;name=flash/firmware;type=directory;
.id=*1f0001a8;creation-time=jan/02/1970 02:00:38;name=flash/hotspot;type=directory;
.id=*1f0001a9;creation-time=jan/02/1970 02:00:38;name=flash/log;type=directory;
.id=*1f0001aa;creation-time=jan/02/1970 02:00:38;name=flash/package;type=directory;
.id=*1f0001ab;creation-time=jan/02/1970 02:00:38;name=flash/pcap;type=directory;
.id=*1f0001ac;creation-time=jan/02/1970 02:00:38;name=flash/skins;type=directory;
.id=*1f0001ad;creation-time=jan/02/1970 02:00:38;name=flash/web-proxy;type=directory;
.id=*1f0001d7;creation-time=jan/02/1970 02:00:39;name=flash/webproxy;type=directory;
.id=*1f0001d8;creation-time=jan/02/1970 02:00:39;name=flash/webproxy/error.html;size=529;type=.html file;
.id=*1f000205;creation-time=jan/01/1970 02:00:06;name=flash;type=disk;
.id=*1f000213;creation-time=aug/20/2021 12:21:20;name=flash/log/log.0.txt;size=1496;type=.txt file;
.id=*1f000295;creation-time=jan/08/2008 17:15:17;name=flash/Script_CBM.txt;size=39;type=.txt file;
.id=*1f0002b3;creation-time=jan/08/2008 17:15:17;name=flash/Script_CBM.backup;size=21979;type=backup;
.id=*1f0002f1;creation-time=jun/24/2021 16:45:36;name=flash/skins/default.json;size=4;type=.json file
On the previous question:
Do you suggest accepting a date in a different format for processing?
Do you suggest displaying in a different format?
Do you want to add an incoming parameter to correctly read the date in the ISO8601 format?

As for the date the files were created, thank you for specifying. In my case, it's not a problem. Files are created on a schedule and are not modified. So for a particular set of files, I can rotate files.
file print where name~"^$[/system identity get name]-[0-9]{8}-[0-9]{4}\\.(backup|rsc)\$"    
 # NAME                                                           TYPE                                                                SIZE CREATION-TIME       
 0 MikroTik DC_Cheap Border-20200720-1655.backup                  backup                                                          214.2KiB jul/20/2020 16:55:12
 1 MikroTik DC_Cheap Border-20200721-0607.backup                  backup                                                          209.0KiB jul/21/2020 06:07:29
 2 MikroTik DC_Cheap Border-20200721-0710.backup                  backup                                                          198.0KiB jul/21/2020 07:10:57
 3 MikroTik DC_Cheap Border-20201111-1305.backup                  backup                                                          209.9KiB nov/11/2020 13:05:50
 4 MikroTik DC_Cheap Border-20201118-1345.backup                  backup                                                          212.7KiB nov/18/2020 13:45:30
 5 MikroTik DC_Cheap Border-20201121-1637.backup                  backup                                                          215.4KiB nov/21/2020 16:37:57
 6 MikroTik DC_Cheap Border-20201122-1011.backup                  backup                                                          215.4KiB nov/22/2020 10:11:52
 7 MikroTik DC_Cheap Border-20201207-1707.backup                  backup                                                          212.3KiB dec/07/2020 17:07:45
 8 MikroTik DC_Cheap Border-20201224-0545.backup                  backup                                                          212.7KiB dec/24/2020 05:45:03
 9 MikroTik DC_Cheap Border-20201224-0643.backup                  backup                                                          212.7KiB dec/24/2020 06:43:06
10 MikroTik DC_Cheap Border-20201228-0752.backup                  backup                                                          212.7KiB dec/28/2020 07:52:36
11 MikroTik DC_Cheap Border-20201228-1102.backup                  backup                                                          212.7KiB dec/28/2020 11:02:47
12 MikroTik DC_Cheap Border-20210115-1119.backup                  backup                                                          214.6KiB jan/15/2021 11:19:28
13 MikroTik DC_Cheap Border-20210118-1358.backup                  backup                                                          212.5KiB jan/18/2021 13:58:55
14 MikroTik DC_Cheap Border-20210119-0723.backup                  backup                                                          213.3KiB jan/19/2021 07:23:19
15 MikroTik DC_Cheap Border-20210122-0629.backup                  backup                                                          213.5KiB jan/22/2021 06:29:12
16 MikroTik DC_Cheap Border-20210301-1506.backup                  backup                                                          348.0KiB mar/01/2021 15:06:15
17 MikroTik DC_Cheap Border-20210314-0840.backup                  backup                                                          366.8KiB mar/14/2021 08:40:34
18 MikroTik DC_Cheap Border-20210527-0926.backup                  backup                                                          698.2KiB may/27/2021 09:26:37
19 MikroTik DC_Cheap Border-20210607-1701.backup                  backup                                                          704.8KiB jun/07/2021 17:01:52
20 MikroTik DC_Cheap Border-20210710-1315.backup                  backup                                                          721.3KiB jul/10/2021 13:15:00
21 MikroTik DC_Cheap Border-20210710-1315.rsc                     script                                                          324.2KiB jul/10/2021 13:15:04
22 MikroTik DC_Cheap Border-20210717-0100.backup                  backup                                                          723.5KiB jul/17/2021 01:00:00
23 MikroTik DC_Cheap Border-20210717-0100.rsc                     script                                                          325.4KiB jul/17/2021 01:00:03
24 MikroTik DC_Cheap Border-20210724-0100.backup                  backup                                                          725.8KiB jul/24/2021 01:00:00
25 MikroTik DC_Cheap Border-20210724-0100.rsc                     script                                                          326.6KiB jul/24/2021 01:00:03
26 MikroTik DC_Cheap Border-20210731-0100.backup                  backup                                                          740.2KiB jul/31/2021 01:00:00
27 MikroTik DC_Cheap Border-20210731-0100.rsc                     script                                                          334.4KiB jul/31/2021 01:00:04
28 MikroTik DC_Cheap Border-20210814-0100.backup                  backup                                                          255.2KiB aug/14/2021 01:00:00
29 MikroTik DC_Cheap Border-20210814-0100.rsc                     script                                                           70.3KiB aug/14/2021 01:00:02
30 MikroTik DC_Cheap Border-20210821-0100.backup                  backup                                                          262.4KiB aug/21/2021 01:00:00
31 MikroTik DC_Cheap Border-20210821-0100.rsc                     script                                                           74.4KiB aug/21/2021 01:00:02
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:32 pm

I think if you have already the date on ISO8601 like 20210823 format on filename, is useless check time.

For example, if you do backup every week (but is better to do not leave inside but do it by mail or on another device)
on the script delete the file than have inside two month ago.

One moment I write one thing....
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:37 pm

I think if you have already the date on ISO8601 like 20210823 format on filename, is useless check time.
Honestly, I'll tell you.
I had no desire to forge file names. Process the creation and modification date of the object faster than extracting the date from the file name. And I want a kind of universality.

That's what you think. Files are sent to email mail. And here there is a desire to organize file rotation.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:42 pm

For example something like:

:local month <from your function actual month - 2>
:local id [/system identity get name]
/file remove [find where name~"^$id-[0-9]{4}$month[0-9]{2}-[0-9]{4}\\.(backup|rsc)\$"]

Once you set 1st time the directory clean, automatically delete 3 monts old backup
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:48 pm

For example something like:

:local month <from your function actual month - 2>
:local id [/system identity get name]
/file remove [find where name~"^$id-[0-9]{4}$month[0-9]{2}-[0-9]{4}\\.(backup|rsc)\$"]

Once you set 1st time the directory clean, automatically delete 3 monts old backup
A good and simple solution that I discarded because all files will be deleted for the specified month. You can stay with one single backup file in the new month. Of course there are backup files in the mail.
In my opinion, the best solution will be to compare the entire date without time.

Your example of deleting files simplified what I wanted to do. Thank you.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 7:55 pm

A good and simple solution that I discarded because all files will be deleted for the specified month. You can stay with one single backup file in the new month. Of course there are backup files in the mail.
Really... not... Delete the backup of 2 months ago, previous month and actual month are untouched...


Always happy to help and give hints, thanks!
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 8:03 pm

A good and simple solution that I discarded because all files will be deleted for the specified month. You can stay with one single backup file in the new month. Of course there are backup files in the mail.
Really... not... Delete the backup of 2 months ago, previous month and actual month are untouched...


Always happy to help and give hints, thanks!
You're welcome.
I hope my script came to your liking and will be useful to others.
I'll post another script as soon as I'm ready.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Mon Aug 23, 2021 10:01 pm

I am very happy every time someone shares something new.

I saw some interesting potential from your script,
otherwise I would not have tried to suggest things to you,
and certainly in the future you will come up with a solution to something else,
thanks to what you made me think today.

Really thanks for that.

Another hint, regexp matching 1 to 12 and 01 to 09:
^([0]?[1-9]|1[0-2])\$
matches
if there is present 0 or not followed by a number from 1 to 9
OR
by a 1 followed by a number from 0 to 2

Remember to add \ before ? on terminal, on script do not put the \ before ?


The only things I have to say now, is: please format the script or is hard to understand :)
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Tue Aug 24, 2021 4:53 am

Good afternoon.
It can be formatted for understanding, and not in the machine version.
I would post both options, but for some reason the "code" tags are not perceived twice in the same message by the forum.

I did not want to make an exact mask for regular expressions, since it was only necessary to distinguish the presence of a number from the string characters in the variable.
The initial variable filter clause is correct if you want to add protection against incorrect data to the script. If necessary, you can add:)

Do not tell how you can write correctly two or more times "code" what would be correctly displayed on the forum?
:global FuncDateConvert do={
}
:global FuncDateConvert do={
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Tue Aug 24, 2021 10:20 am

Like on my previous posts, simply add 3 "enter" new line ;)


no enter:
code1
code2



with 3 enter:
code3

code4
 
Prometej
just joined
Topic Author
Posts: 17
Joined: Sat Mar 10, 2012 6:58 am

Re: Script function for converting and shifting the standard date to OS Mikrotik

Tue Aug 24, 2021 11:39 am

Thank you. It turned out but with oddities and dances with a tambourine.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12445
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script function for converting and shifting the standard date to OS Mikrotik

Wed Sep 01, 2021 3:51 pm

If better not finded, this is my method for convert literal month to two characters string with "zero filler"
:global strDate [/system clock get date]
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
:global strYear [:pick $strDate 7 11]
:global strMonth ($arrMonths->[ :pick $strDate 0 3 ])
:global strDay [:pick $strDate 4 6]
:put "$strDate converted on ISO format is $strYear-$strMonth-$strDay"


# or on short way
:global strDate [/system clock get date]
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
:put "Today is $[:pick $strDate 7 11]-$($arrMonths->[ :pick $strDate 0 3 ])-$[:pick $strDate 4 6]"
 
dokacoimbra
just joined
Posts: 7
Joined: Fri Jan 13, 2023 6:11 pm

Re: Script function for converting and shifting the standard date to OS Mikrotik

Fri Jan 13, 2023 10:45 pm

Rex I'm a beginner in the area, could you take a doubt please?
In case this script changes the date presented in the Mikrotik? If that's the case, should I put this script to run on the scheduler for this to happen?
Last edited by BartoszP on Sat Jan 14, 2023 11:55 am, edited 1 time in total.
Reason: no need to quote whole previous post ... we can follow the stream of discussion

Who is online

Users browsing this forum: No registered users and 15 guests