gpt4 book ai didi

Javascript和ASP更优雅地创建动态文件夹

转载 作者:行者123 更新时间:2023-12-02 20:33:08 26 4
gpt4 key购买 nike

我在下面编写了以下脚本 - 我对该脚本不太满意,因为我认为它可以以更优雅和动态的方式编写......

我正在使用 ftp.exe 将文件从我的应用程序 ftp 到另一台服务器。

如果网站上不存在该文件夹结构。我需要创建文件夹结构...

这样做

mkdir folder1
mkdir folder1/folder2
mkdir folder1/folder2/folder3

使用下面的脚本,我可以满足一些文件夹长度的需求,但不是无限的...因此该解决方案只能处理一定数量的子文件夹

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, '');

如何使其更加动态?以便脚本查看输入包含多少个文件夹...

我可以看到文件夹完整长度并获取像这样的文件夹/folder1/folder2/folder3,但需要将其剥离到

folder1
folder1/folder2/
folder1/folder2/folder3/

然后创建它...

最佳答案

您可以使用循环,检查 '/' 的索引并使用它们来获取完整路径的部分内容。

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";
}

关于Javascript和ASP更优雅地创建动态文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3764380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com