我在下面编写了以下脚本 - 我对脚本不太满意,因为我认为它可以用更优雅和动态的方式编写...
我正在使用ftp.exe
将我的应用程序中的文件ftp到另一台服务器。
如果网站上不存在文件夹结构。我需要创建文件夹结构......
这样做
mkdir folder1
mkdir folder1/folder2
mkdir folder1/folder2/folder3
使用下面的脚本我可以提供一些folder
长度,但不能无限制...所以解决方案只能使用一定数量的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, '');
如何让它更具动感?以便脚本看到输入包含多少个文件夹...
我可以看到folders full length
并获取此/folder1/folder2/folder3
这样的文件夹,但需要将其删除到
folder1
folder1/folder2/
folder1/folder2/folder3/
然后创建它......
答案 0 :(得分:1)
我脑子里有一些不同的想法可以帮助你。
一个想法是考虑您也可以在创建每个目录后更改目录,这样您就不会最终构建这样的长路径。这是一个FTP命令序列的例子,它可以实现同样的目的:
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)
祝你好运!
答案 1 :(得分:1)
您可以使用循环,检查'/'
的索引并使用它们来获取完整路径的一部分。
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";
}