Community discussions

 
User avatar
phistrom
just joined
Topic Author
Posts: 7
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;
MTCNA, MTCRE
 
Dude2048
Frequent Visitor
Frequent Visitor
Posts: 81
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: 1296
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
krisjanisj
MikroTik Support
MikroTik Support
Posts: 51
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.
* Wager of "The Holy War" against users who don't paste their config/export/print into [code][/code] blocks
* Avid coffee consumer
* Provider of stupid solutions for simple problems
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1775
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: 24142
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?
No answer to your question? How to write posts
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1296
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

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.
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
Dude2048
Frequent Visitor
Frequent Visitor
Posts: 81
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: 7
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.
MTCNA, MTCRE

Who is online

Users browsing this forum: No registered users and 2 guests