PHP+SHELL笨方法实现遍历复制新目录结构(简单有效哈)

首先,使用PHP把一个多层级的文件目录及其文件的完整路径读出来:

<?php
function scanDirectories($rootDir, $allData=array()) {
    // set filenames invisible if you want
    $invisibleFileNames = array(".", "..", ".htaccess", ".htpasswd");
    // run through content of root directory
    $dirContent = scandir($rootDir);
    foreach($dirContent as $key => $content) {
        // filter all files not accessible
        $path = $rootDir.'/'.$content;
        if(!in_array($content, $invisibleFileNames)) {
            // if content is file & readable, add to array
            if(is_file($path) && is_readable($path)) {
                // save file name with path
                $allData[] = $path;
            // if content is a directory and readable, add path and name
            }elseif(is_dir($path) && is_readable($path)) {
                // recursive callback to open new directory
                $allData = scanDirectories($path, $allData);
            }
        }
    }
    return $allData;
}
 
echo "<pre>";
$res = scanDirectories("E:/hello");
foreach ($res as $url) {
	$arr[] = dirname($url);
}
print_r(array_unique($arr));

echo "</pre>";

然后使用把上面生成的数组数据复制出来,整体替换成一行行目录的完整路径,为空格加上反斜杠\,换行变成空格。然后使用如下SHELL命令:

mkdir -p hello/new\ cute1 hello/new\ cute2 hello/new\ cute3

朋友你如果有更好的代码,还请赐教。

郑重声明:

1 本资源来源于互联网,资源的版权归资源原作者所持有,受《中华人民共和国著作权法》等相关法律保护。

2 由于无法和原作者取得联系,所以上传的部分资源无法先通过原作者的同意就分享给大家了,如本资源侵犯了您(原作者)的权益,请联系我们(微信号 wenyan90s),我们会立马删除您的资源,并向您表达诚挚的歉意!

3 本站是一个公益型网站,分享资源的目的在于传播知识,分享知识,收取一点点打赏的辛苦费是用于网站的日常运营开支,并非用于商业用途。

4 本站资源只提供学习和参考研究使用,使用过后请在第一时间内删除。本站不承担资源被单位或个人商用带来的法律责任。

发表评论