如何使用php扫描所有.m文件?

时间:2011-06-20 12:57:30

标签: php file

  

可能重复:
  php scandir --> search for files/directories

我有一个文件夹,在这个文件夹里面有很多子文件夹,但我想扫描所有的子文件夹,并扫描所有的.m文件......我怎么能这样做?

这是文件:

/MyFilePath/
           /myPath.m
           /myPath2.m
           /myPath3.m
          /MyClasses/
                    /my.m
                    /my1.m
                    /my2.m
                    /my3.m
                    /Utilities/
                              /u1.m
                              /u2.m
                              /External/
                                       /a.m
                                       /b.m
                                       /c.m
                              /Internal/
                                       /d.m
                                       /e.m
                                       /f.m
                    /Views/
                           /a_v.m
                           /b_v.m
                           /c_v.m
                    /Controllers/
                                /a_vc.m
                                /b_vc.m
                                /c_vc.m
          /AnotherClasses/
                         /anmy.m
                         /anmy1.m
                         /anmy2.m
                         /anmy3.m
                    /Networking/
                               /net1.m
                               /net2.m
                               /net3.m
                              /External/
                              /Internal/
                    /Views/
                    /Controllers/

4 个答案:

答案 0 :(得分:7)

你也可以使用一些SPL的迭代器。一个快速而基本的例子如下:

$directories = new RecursiveDirectoryIterator('path/to/search');
$flattened   = new RecursiveIteratorIterator($directories);
$filter      = new RegexIterator($flattened, '/\.in$/');

foreach ($filter as $file) {
    echo $file, PHP_EOL;
}

更多信息(大部分是不完整的):

答案 1 :(得分:1)

function ScanForMFiles($dir){ 

    $return = array();
    if ($handle = opendir($dir)) { 
        while (false !== ($file = readdir($handle))) { 

            if ($file != "." && $file != "..") { 
                if(is_dir($dir.$file)){
                    $return =  array_merge($return, ScanForMFiles($dir.$file."/")); 
                }
                else {
                    if(substr($file, -2) == '.m')
                        $return[] = $file;
                }
            } 
        } 

        closedir($handle); 
    }

    return $return;

}

var_dump(ScanForMFiles('./'));

答案 2 :(得分:1)

您可以使用这样的递归函数:

function searchFiles($dir,$pattern,$recursive=false)
{
    $matches = array();

    $d = dir($dir);
    while (false !== ($entry = $d->read()))
    {
        if (is_dir($d->path.$entry) && $recursive)
        {
            $subdir = $d->path.$entry;
            $matches = array_merge($matches,searchFiles($dir,$pattern,$recursive));
        }
        elseif (is_file($d->path.$entry) && preg_match($pattern,$entry))
        {
            $matches[] = $d->path.$entry;
        }
    }
    $d->close();
    return $matches;
}

用法:

$matches = searchFiles("/mypath/","'[.]m$'i",true);

答案 3 :(得分:0)

您需要查看PHP文档以获取有关此内容的详细信息:http://php.net/manual/en/function.readdir.php

这是一个应该做你想做的事的例子。它将返回子目录中所有.m文件的数组。然后,您可以遍历每个文件并阅读内容,如果这是您感兴趣的内容。

<?php
function get_m_files($root = '.'){ 
  $files  = array(); 
  $directories  = array(); 
  $last_letter  = $root[strlen($root)-1]; 
  $root  = ($last_letter == '\\' || $last_letter == '/') ? $root : $root.DIRECTORY_SEPARATOR; 

  $directories[]  = $root; 

  while (sizeof($directories)) { 
    $dir  = array_pop($directories); 
    if ($handle = opendir($dir)) { 
      while (false !== ($file = readdir($handle))) { 
        if ($file == '.' || $file == '..') { 
          continue; 
        } 
        $file  = $dir.$file; 
        if (is_dir($file)) { 
          $directory_path = $file.DIRECTORY_SEPARATOR; 
          array_push($directories, $directory_path); 
        } elseif (is_file($file)) { 
          if (substr( $file, -strlen( ".m" ) ) == ".m") { 
              $files[]  = $file; 
          }               
        } 
      } 
      closedir($handle); 
    } 
  } 

  return $files; 
}
?>