Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:54:51+00:00 2026-06-05T23:54:51+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:54:52+00:00Added an answer on June 5, 2026 at 11:54 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a list of all files in a directory and
I was trying to get a list of files under a directory using: val
I am trying to get a list of subdirectories in a given directory using
i am trying to traverse a given directory and create a list of files
I am trying to get Update queries from a list of files using this
I'm trying to write a bash script that will process a list of files
I'm trying to make a script to list all directories, subdirectories, and files in
I'm trying to list files in one particular directory and with one particular ip.
I'm trying to use a batch file to list files in a directory such
I'm trying to find a list of files in a directory tree. In essence

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.