如何遍历所有包含特殊文件的子文件夹?

发布于 2024-09-12 02:28:06 字数 321 浏览 1 评论 0原文

我想遍历特定文件夹的所有子文件夹并检查其中是否有特殊文件,否则删除子文件夹。

举个例子(file.txt 是这里的特殊文件):

  • folder_all
    • 文件夹1
      • 文件.txt
    • 文件夹2
      • 文件.txt
    • 文件夹3

因为“folder3”没有该文件,所以我想删除它。
这就是我想做的。有什么想法吗?

非常感谢!

I want to traverse all sub-folders of a particular folder and check whether they have a special file in it otherwise delete the sub-folder.

Take this example (file.txt being the special file here):

  • folder_all
    • folder1
      • file.txt
    • folder2
      • file.txt
    • folder3
      • empty

Because "folder3" doesn't have the file, I'd like to delete it.
And this is what I want to do. Any ideas?

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-09-19 02:28:06

更新的代码

您可以使用RecursiveDirectoryIterator班级:

<?php

$dir = '/path/';
$file = '/filetosearch.txt';
$paths = array();

$i = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

while($i->valid()) {

    if (!$it->isDot()) {

        $subpath = $it->getSubPath();

        if ($subpath != '') {
            // if inside a subdirectory
            // add the subpath in our array flagging it as false
            if (!array_key_exists($subpath, $paths) $paths[$subpath] = false;

            // check if it's our file
            if (substr_compare($i->getSubPathName(), $file, -strlen($file), strlen($file)) === 0)
                $paths[$subpath] = true;

    }

    $it->next();
}

// now check our paths array and delete all false (not containing the file)
foreach ($paths as $key => $value)
{
    if (!$value) rmdir($dir.$key);
}

?>

updated code

You can use the RecursiveDirectoryIterator class:

<?php

$dir = '/path/';
$file = '/filetosearch.txt';
$paths = array();

$i = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));

while($i->valid()) {

    if (!$it->isDot()) {

        $subpath = $it->getSubPath();

        if ($subpath != '') {
            // if inside a subdirectory
            // add the subpath in our array flagging it as false
            if (!array_key_exists($subpath, $paths) $paths[$subpath] = false;

            // check if it's our file
            if (substr_compare($i->getSubPathName(), $file, -strlen($file), strlen($file)) === 0)
                $paths[$subpath] = true;

    }

    $it->next();
}

// now check our paths array and delete all false (not containing the file)
foreach ($paths as $key => $value)
{
    if (!$value) rmdir($dir.$key);
}

?>
梦里兽 2024-09-19 02:28:06
function recursive_delete_if_exists($path,$file) {
   foreach (glob($path.'/*.*') as $name)
      if (is_dir($name))
         recursive_delete_if_exists($name,$file);
      elseif ($name==$file)
         unlink($path);
}

recursive_delete_if_exists('/folder_all','file.txt');
function recursive_delete_if_exists($path,$file) {
   foreach (glob($path.'/*.*') as $name)
      if (is_dir($name))
         recursive_delete_if_exists($name,$file);
      elseif ($name==$file)
         unlink($path);
}

recursive_delete_if_exists('/folder_all','file.txt');
感情旳空白 2024-09-19 02:28:06

只需迭代 folderall 的所有子文件夹并检查文件 folder_all/$subfoldername/file.txt 是否存在。如果没有,请将其删除。那应该是一个循环。

API:

Simply iterate over all subfolders of folderall and check if the file folder_all/$subfoldername/file.txt exists. If not, delete it. That should be one loop.

API:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文