Reputation: 1337
I have written the following script below - I am not quite happy with the script as I think it could be written in a more elegant and dynamic way...
I am using ftp.exe
to ftp files from my application to another server.
If the folder structure does not exists on the web site. I need to create the folder stucture...
and doing it this way
mkdir folder1
mkdir folder1/folder2
mkdir folder1/folder2/folder3
with the script below I cater for a few folder
lengths but not unlimited... so the solution will only work up to a certain amount of sub folders
.
ftpmkdirlength = ftpmkdir.split('/').length-1
var i=0;
for (i=0;i<=ftpmkdirlength;i++)
{
ftpmkdir0 = " mkdir " + ftpmkdir.match(/\/.*?\//) + "\n";
ftpmkdir1 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\//) + "\n";
ftpmkdir2 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir3 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir4 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir5 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir6 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir7 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir8 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir9 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir10 = " mkdir " + ftpmkdir.match(/\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\/.*?\//) + "\n";
ftpmkdir1 = " mkdir " + ftpmkdir + ftpmkdir0 + ftpmkdir1 + ftpmkdir2 + ftpmkdir3 + ftpmkdir4 + ftpmkdir5 + ftpmkdir6 + ftpmkdir7 + ftpmkdir8 + ftpmkdir9+ ftpmkdir10;
ftpmkdir1 = ftpmkdir1.replace(/mkdir null/ig, '');
How can I make it more dynamic? so that the script see how many folders the input contains...
I can see the folders full length
and get the folder like this /folder1/folder2/folder3
but need to strip it to
folder1
folder1/folder2/
folder1/folder2/folder3/
and then create it...
Upvotes: 0
Views: 342
Reputation: 11238
You can use a loop, checking for indexes of '/'
and using them to get parts of the full path.
var cmd = '';
var j, k;
var subpath;
if (ftpmkdir.length < 1 || ftpmkdir === '/') {
throw 'no path';
}
j = ( ftpmkdir.indexOf('/', 0) === 0 ) ? 1 : 0; // provides non-slash starting point
k = ftpmkdir.indexOf('/', j);
while (k != -1) {
subpath = ftpmkdir.substr(j, k - j); // get the path up to the current slash
cmd += " mkdir " + subpath + "\n";
k = ftpmkdir.indexOf('/', k + 1); ; // move the cursor
}
// check for additional path after last slash mark
if ((subpath + '/').length !== ftpmkdir.length - j) {
cmd += " mkdir " + ftpmkdir.substr(j, ftpmkdir.length - j) + "\n";
}
Upvotes: 1
Reputation: 4278
A few different ideas pop in my head for helping here.
One idea is to consider that you can also just change directories after creating each one, so that you don't end up building such long paths. Here's an example of an FTP command sequence that would achieve the same thing:
mkdir folder1
cd folder1
(working directory is now /folder1/)
mkdir folder2
cd folder2
(working directory is now /folder1/folder2/)
mkdir folder3
cd folder3
(working directory is now /folder1/folder2/folder3/)
(etc...)
cd / (to return "home" when finished)
Good luck!
Upvotes: 1