Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find all files in directory and sub-directories

Status
Not open for further replies.

georgeocrawford

Technical User
Aug 12, 2002
111
GB
Hello,

I going round in loops here!

How can I write a script to find *all* the files within a directory, i.e. all the files contained in all the subdirectories of a specified directory?

I've tried looking at other scripts, creating multidimensional arrays, etc but I find myself in impossible loop situations!

I want to look for all the text files in a directory and it's subdirectories, and store their paths relative to the main directory in an array, like this:

0 => /folder1/folder6/text_file1.txt
1 => /folder4/folder1/text_file2.txt
2 => ...

and so on.

Thanks for any help



______________________

George
 
heh, I was just sitting down to write an app that starts this way, unfortunately my days almost over. If no one else answers, I should have something working around 12 eastern tomorrow that I can share.

-Rob
 
great minds think alike (ish!)

is it a simple proposition, or more complex that I thought?

______________________

George
 
I'd probably do it using recursion.

Code:
<?php

$file_list = array();

function get_files ($dirname)
{
   global $file_list;

   if (is_dir($dirname))
   {
      $dh = opendir ($dirname);

      while ($sub = readdir($dh))
      {
         if ($sub != '.' && $sub != '..')
         (
            $newname = $dirname . '/' . $sub;
            if (is_dir($newname))
            {
               get_files ($newname);
            )
            else
            {
               $file_list[] = $newname;
            }
         }
      }
   }
}

get_files ('directoryname');

print_r ($file_list);
?>


This will put all filenames in $file_list with relative paths. Directory names will not appear in $file_list.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks slepnir, as ever!

I found an answer myself - it's a very different approach.

What might be the pros and cons of this:

Code:
$output = `find /Library/WebServer/Documents/Files -name &quot;*.txt&quot; -print`;
echo &quot;<pre>$output</pre>&quot;;

Are there any security issues?

______________________

George
 
Got to work incredibly late, just hacked it out, and it looks eeriely similar to sleipnir's with the exception that I pass in an array by reference rather than using a global and I set a flag for the recursiveness... in other words.

Code:
function getAllFiles($path, &$files, $recursive=true) {
  echo &quot;New directory starting: &quot;.$path.&quot;\n&quot;;
  $dir_handle=opendir($path);
  while ($entry = readdir($dir_handle)) {
    if (is_file($path.&quot;/&quot;.$entry)) {
      $files[]=$path.&quot;/&quot;.$entry;
    } else {
      if ($entry != &quot;.&quot; && $entry != &quot;..&quot; && $recursive) getAllFiles($path.&quot;/&quot;.$entry, $files);
    }
  }
}

The only downside I see to your solution is the OS specificness of it.

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top