开发者

Retrieve all folder in a directories and change all folders' name that are in it

开发者 https://www.devze.com 2023-04-09 22:10 出处:网络
I want to retrieve all folders in a directory folder and change all the subfolder that in it. For example,

I want to retrieve all folders in a directory folder and change all the subfolder that in it. For example, in root folder Root, I want to change all subfolders A, B, C, D, .. to 1,2,34,... May I 开发者_如何转开发know how can I do it with php? Thanks.


<?php

$basedir = "/tmp"; //or whatever your "to change" home directory is

$contents = scandir($basedir);
$count = 1;

foreach ($contents as $check) {
    if (is_dir($basedir . "/" . $check) && $check != "." && $check != "..") {
        rename($basedir . "/" . $check, $basedir . "/" . $count);
        $count++;
    }
}

?>

Of course, you will need to have proper CHMOD depending on where you run the script from.


Something like this:

$count = 0;
foreach(new DirectoryIterator('Root') as $fileInfo) {
    if ($fileInfo->isDir() && !$fileInfo->isDot()) {
        $count++;
        rename($fileInfo->getPathName(), $fileInfo->getPath() . "/$count");
    }
}


Links:

  • DirectoryIterator
  • rename
  • RecursiveDirectoryIterator (if you with to do the same for sub-directories)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号