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

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

发表评论