2

I am trying to list files in subdirectories and write these lists into separate text files.

I managed to get the directory and subdirectory listings and even to write all the files into a text file.

I just don't seem to manage to burst out of loops I am creating. I either end up with a single text file or the second+ files include all preceeding subdirectories content as well.

What I need to achieve is:

  • dir A/AA/a1.txt,a2.txt >> AA.log
  • dir A/BB/b1.txt,b2.txt >> BB.log
  • etc.

Hope this makes sense.

I've found the recursiveDirectoryIterator method as described in PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree being great help. I then use a for and a foreach loop to iterate through the directories, to write the text files, but I cannot break them into multiple files.

Community
  • 1
  • 1
datafunk
  • 636
  • 9
  • 18
  • I don't understand exactly what your problem is. I'd generally say problems like these are solveable, however the description you give leaves too much rooms for variables so that I think you should improve your question. – hakre Jun 16 '12 at 13:29
  • Please provide the source code you are having problems with. – Emil Vikström Jun 17 '12 at 10:45

2 Answers2

2

Most likely you are not filtering out the directories . and .. .

$maindir=opendir('A');
if (!$maindir) die('Cant open directory A');
while (true) {
  $dir=readdir($maindir);
  if (!$dir) break;
  if ($dir=='.') continue;
  if ($dir=='..') continue;
  if (!is_dir("A/$dir")) continue;
  $subdir=opendir("A/$dir");
  if (!$subdir) continue;
  $fd=fopen("$dir.log",'wb');
  if (!$fd) continue;
  while (true) {
    $file=readdir($subdir);
    if (!$file) break;
    if (!is_file($file)) continue;
    fwrite($fd,file_get_contents("A/$dir/$file");
  }
  fclose($fd);
}
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • Thanks for your replies so far! I'll work through the above and weather solved or not I will bring back source code. Only reason I haven't before because I tried mutliple approaches and always failed similarly. The first thing I see from the above is that my thinking was probably incorrect and I may have been using the wrong loop types. back soon and thanks again!! – datafunk Jun 17 '12 at 21:50
1

I thought I'd demonstrate a different way, as this seems like a nice place to use glob.

// Where to start recursing, no trailing slash
$start_folder = './test';
// Where to output files
$output_folder = $start_folder;

chdir($start_folder);

function glob_each_dir ($start_folder, $callback) {

    $search_pattern = $start_folder . DIRECTORY_SEPARATOR . '*';

    // Get just the folders in an array
    $folders = glob($search_pattern, GLOB_ONLYDIR);

    // Get just the files: there isn't an ONLYFILES option yet so just diff the
    // entire folder contents against the previous array of folders
    $files = array_diff(glob($search_pattern), $folders);

    // Apply the callback function to the array of files
    $callback($start_folder, $files);

    if (!empty($folders)) {
        // Call this function for every folder found
        foreach ($folders as $folder) {
            glob_each_dir($folder, $callback);
        }
    }
}

glob_each_dir('.', function ($folder_name, Array $filelist) {
        // Generate a filename from the folder, changing / or \ into _
        $output_filename = $_GLOBALS['output_folder']
            . trim(strtr(str_replace(__DIR__, '', realpath($folder_name)), DIRECTORY_SEPARATOR, '_'), '_')
            . '.txt';
        file_put_contents($output_filename, implode(PHP_EOL, $filelist));
    });
Alan Pearce
  • 1,320
  • 10
  • 17
  • Hi guys back again to say thanks - I will ought to report back (I think) but this is a home project and work completly overtook life for the past week and I haven't nailed it yet, but back on it asap! Thanks for your help again! – datafunk Jun 23 '12 at 11:20