在多个目录中搜索文件并删除(取消链接)


Search for files in multiple directories and delete (unlink)

我需要一个安全的方式来扫描位于"users"下的约1000个目录。

主用户目录有另一个目录(人名)。那个人的名字目录存储了几个我想删除的坏文件。所以,与其手动浏览并删除它们,有没有一种方法可以运行脚本来删除这些文件?

我需要删除那些人名字目录中的多个文件!


users/john/badfile.xml
users/tim/badfile.xml
users/bob/badfile.xml
users/scott/badfile.xml
users/jess/badfile.xml
....
users/tom/badfile.xml






这是最终的工作代码!

  $users = scandir('test'); // First we get the users
  unset($users[0]); // We unset the first two elements, which are       useless
  unset($users[1]);
  foreach ( $users as $i ) // We loop through the folders
  {
   $contents = scandir('test/'.$i); // We repeat the same process
   unset($contents[0]);
   unset($contents[1]);
    unlink('test/'.$i.'/'.'badfile.php'); // File deletion
    unlink('test/'.$i.'/'.'badfile.xml'); // File deletion
    unlink('test/'.$i.'/'.'badfile.html'); // File deletion
    unlink('test/'.$i.'/'.'badfile.txt'); // File deletion
}
?>

这个脚本应该能够删除每个目录中的文件,但不会删除目录。

请记住我的工作路径是C:'server'root'michael'folder

$users = scandir('users'); // First we get the users
unset($users[0]); // We unset the first two elements, which are useless
unset($users[1]);
foreach ( $users as $i ) // We loop through the folders
{
        $contents = scandir('users/'.$i); // We repeat the same process
        unset($contents[0]);
        unset($contents[1]);
        foreach ( $contents as $i2 ) // Loop through files of each folder
        {
            unlink('users/'.$i.'/'.$i2); // File deletion
        }
}