Community discussions

MikroTik App
 
User avatar
phistrom
just joined
Topic Author
Posts: 12
Joined: Sat Mar 11, 2017 8:45 pm
Location: Texas

mkdir function for easy folder creation

Thu Aug 29, 2019 7:31 pm

I have created a script that creates a $mkdir global function to create directories. Once you have run the script, you can write this on the command line:
$mkdir some/new/folder
.


It builds on this code found on the official Wiki, which uses FTP via /tool fetch to create the directory tree. This function handles cases like the FTP service being disabled, cleans up after itself even if an error occurs, and is easy to install.

The code and instructions are available on GitHub.

Here's the script itself: https://github.com/phistrom/routeros-mk ... nction.rsc

or here's a snippet you can copy and paste in the terminal to easily create the function and a scheduler entry that runs on startup to make sure that $mkdir is always available.
# if you trust strangers on the internet
# for extra safety, the commit in the below URL is pinned to da45ff1987aa6723559aee8bf344b341efd31393
{
    :local result [/tool fetch \
    url="https://raw.githubusercontent.com/phistrom/routeros-mkdir/da45ff1987aa6723559aee8bf344b341efd31393/persist_create_mkdir_function.rsc" \
    as-value output=user];
    :local script [:parse ($result->"data")]
    $script;
}

If using $mkdir in a script, remember you have to include it with
:global mkdir;
at the top for your references to
$mkdir
work.

Script Example:
:global mkdir;
# ensure backups directory exists
$mkdir "disk1/backups";
/system backup save name=disk1/backups/backup;
 
Dude2048
Member Candidate
Member Candidate
Posts: 212
Joined: Thu Sep 01, 2016 4:04 pm

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 8:57 am

Great work
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 10:43 am

I am some shocked.
A script on 200+ lines is needed just to create a folder in RouterOS.
This is some MT should add a built in function.
 
krisjanisj
Member Candidate
Member Candidate
Posts: 101
Joined: Wed Feb 20, 2019 2:53 pm
Contact:

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 10:46 am

I am some shocked.
A script on 200+ lines is needed just to create a folder in RouterOS.
This is some MT should add a built in function.
You can always log in via FTP to create a folder and/or copy/move files.
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1782
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 11:07 am

I am some shocked.
A script on 200+ lines is needed just to create a folder in RouterOS.
This is some MT should add a built in function.
You can always log in via FTP to create a folder and/or copy/move files.
which is exactly what the script does...

NOT a acceptable "solution"
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26287
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 11:16 am

What do you need folders for?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 3:47 pm

For test I do use many version of backup config and it could be nice to have folder to store them in.
One for wifi, one for hotspot etc.

Problem is that on my hAP lite I needed to delete all file to be able to upgrade due to the small space on the box.
 
Dude2048
Member Candidate
Member Candidate
Posts: 212
Joined: Thu Sep 01, 2016 4:04 pm

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 3:50 pm

I use it the same way and keep things organized. This script is a big help.
 
User avatar
phistrom
just joined
Topic Author
Posts: 12
Joined: Sat Mar 11, 2017 8:45 pm
Location: Texas

Re: mkdir function for easy folder creation

Fri Aug 30, 2019 10:38 pm

I agree that the script is long and scary looking, but the idea was to give you a very robust function that should run no matter what your settings are. It has to set up and tear down a temporary group, user, firewall rules, and check to see if it needs to briefly toggle on your FTP service, and then toggle it back off (or restore whatever your "available from" IP ranges were). It also has to chop the input string, i.e. if you said $mkdir /some/path/, it has to convert that to $mkdir some/path. Plus I left in a lot of commenting to give as much transparency as possible into what is going on.

One could theoretically cut this down to basically the same size as the original script, but it wouldn't be nearly as portable.
 
User avatar
Joni
Member Candidate
Member Candidate
Posts: 156
Joined: Fri Mar 20, 2015 2:46 pm
Contact:

Re: mkdir function for easy folder creation

Sun Oct 25, 2020 9:13 pm

What do you need folders for?

For RouterOS not trying to upgrade itself on every boot when .npk for Capsman Cap's (APs) exists in filesystem... for one...

"What does Capsman need a package path setting for?"
 
HaStry
just joined
Posts: 2
Joined: Fri Aug 27, 2021 6:09 pm

Re: mkdir function for easy folder creation

Fri Aug 27, 2021 6:13 pm

What do you need folders for?
I'm sorry, but I just made an account specifically to reply to this.
This must be the most condescending disgustingly customer-unfriendly response I've seen in a long long while.
Are you actually for real??

Besides the fact that folders are an EXTREMELY common and useful way to store files, and it's in no freaking way an anomaly for someone to want to store their files separated in directories, what's it up to you? Even if it wasn't necessary to store files in separate directories, it's in no way any business of yours to second guess something this common.
This is....
wow, this is just disgusting.
 
HaStry
just joined
Posts: 2
Joined: Fri Aug 27, 2021 6:09 pm

Re: mkdir function for easy folder creation

Fri Aug 27, 2021 6:15 pm

I am some shocked.
A script on 200+ lines is needed just to create a folder in RouterOS.
This is some MT should add a built in function.
You can always log in via FTP to create a folder and/or copy/move files.
That's not a solution. Not even close.
 
luca1234567
newbie
Posts: 31
Joined: Tue May 15, 2018 1:27 am
Contact:

Re: mkdir function for easy folder creation

Wed Sep 08, 2021 4:42 pm

What do you need folders for?
...
wow, this is just disgusting.
For your disgust.

Super-Easy script to create dir viewtopic.php?t=169738
Create directory on remote FTP server viewtopic.php?t=170708

I need to re-try/test with latest Long-term release

Hi
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: mkdir function for easy folder creation

Wed Sep 08, 2021 5:43 pm

I am some shocked.
A script on 200+ lines is needed just to create a folder in RouterOS.

This is really a shitty script, full of "on-error={}", do not count "flash", and other nonsense
like creating firewall rules on filter, nat, mangle, raw, temporary user group, temporary user, enabling ftp service, etc. etc. etc. ....
And the script must have all the control of device because "policy" is required fro create and manage users
And if something go wrong delete all firewall filter / nat / mangle / raw rules


Simply one command (ignoring SMIPS),
simply create a recurrency & test foreach / on path

see my next post for the script.
Last edited by rextended on Wed Sep 08, 2021 9:16 pm, edited 7 times in total.
 
luca1234567
newbie
Posts: 31
Joined: Tue May 15, 2018 1:27 am
Contact:

Re: mkdir function for easy folder creation

Wed Sep 08, 2021 7:39 pm

I have made some test with "/ip smb shares", but the problem is that this function can not create more then one subfolder of a subfolder.
For example, if not exist subfolder and not exist sub-subfolder, it can not be created in one istrution the folder /flash/A/B.
But must be created /flash/A and next /flash/A/B

In the metod I use it can be created in one iteration.

Hi
Last edited by luca1234567 on Wed Sep 08, 2021 9:30 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: mkdir function for easy folder creation

Wed Sep 08, 2021 7:44 pm

I'm writing the function on this moment...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: mkdir function for easy folder creation  [SOLVED]

Wed Sep 08, 2021 8:50 pm

search tag # rextended create directory and subdirectory

the function return an array for check on calling script if the path are created or not (and why not)
if the result ($results->0) containing OK, on ($results->1) are present the name of folder/path created

35 lines and also with sanitizing input and "flash" folder on consideration
:global createpath do={
    :global createpath
    :if ([/system resource get architecture-name] = "smips") do={:return [:toarray ("ERROR,SMIPS")]}
    :if ([:typeof $1] = "nothing") do={:return [:toarray "ERROR,Directory not specified"]}
    :local invalidchars "[\01-\1F\7F-\FF]"
    :local invalidonwin "[\22\2A\3A\3C\3E\3F\5C\7C]"
    :local fullpath $1
    :if ($fullpath ~ $invalidchars) do={:return [:toarray "ERROR,Invalid character on Linux"  ]}
    :if ($fullpath ~ $invalidonwin) do={:return [:toarray "ERROR,Invalid character on Windows"]}
    :if ($fullpath ~ "//"         ) do={:return [:toarray "ERROR,Invalid // path specified"   ]}
    :if ($fullpath ~ "^/"         ) do={:set fullpath [:pick $fullpath 1  [:len $fullpath]     ]}
    :if ($fullpath ~ "^flash/"    ) do={:set fullpath [:pick $fullpath 6  [:len $fullpath]     ]}
    :if ($fullpath ~ "/\$"        ) do={:set fullpath [:pick $fullpath 0 ([:len $fullpath] - 1)]}
    /file
    :local rootdir ""
    # if the root is on ramdisk the folder must go on flash disk...
    :if ([:len [find where name=flash and type=disk]] = 1) do={:set rootdir "flash/"}
    :if ([:len [find where name="$rootdir$fullpath" and type=directory]] = 1) do={:return [:toarray "OK EXIST,$rootdir$fullpath"]}
    :if (($fullpath ~ "/") and ($2 != "NOREC")) do={
        :local workpath $fullpath
        :local whereare $rootdir
        :local thisdir  ""
        :while ($workpath ~ "/") do={
            :set thisdir  [:pick $workpath 0 [:find $workpath "/" 0]]
            :set whereare "$whereare$thisdir"
            :if ([:len [find where name="$rootdir$whereare" and type=directory]] = 0) do={$createpath $whereare "NOREC"}
            :set whereare "$whereare/"
            :set workpath [:pick $workpath ([:find $workpath "/" -1] + 1) [:len $workpath]]
        }
    }
    /ip smb shares
    :local defaultentry [find where default=yes]
    :if ([:len $defaultentry] = 1) do={
        :local previousdir [get $defaultentry directory]
        set $defaultentry directory="$rootdir$fullpath"
        set $defaultentry directory=$previousdir
    }
    :return [:toarray "OK CREATED,$rootdir$fullpath"]
}

example code

# global for test on terminal
:global results [$createpath ("backup/2021/09/08")]
:put ($results->0)
:put ($results->1)
Last edited by rextended on Wed Mar 15, 2023 11:20 am, edited 2 times in total.
 
luca1234567
newbie
Posts: 31
Joined: Tue May 15, 2018 1:27 am
Contact:

Re: mkdir function for easy folder creation

Wed Sep 08, 2021 9:38 pm

Okey good.

But the origin of problem is that the RouterOS NOT HAVE A SPECIFIC command to:
- create folder
- rename folder
- delete folder

And we need to do any sort of trick as WORKAROUND !!!

THIS IS VERY DISGUTING !!!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: mkdir function for easy folder creation

Thu Sep 09, 2021 1:08 am

You can use another vendor router can have full scripting support inside, what is the problem?
 
User avatar
berisz
newbie
Posts: 43
Joined: Tue Dec 12, 2017 12:50 am

Re: mkdir function for easy folder creation

Wed May 31, 2023 12:28 pm

Who is online

Users browsing this forum: No registered users and 18 guests