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 7777097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:03:08+00:00 2026-06-01T18:03:08+00:00

I have zip file that I get that, when unzipped, has several subfolders each

  • 0

I have zip file that I get that, when unzipped, has several subfolders each containing a csv called report.csv:

the_folder/
    1234/report.csv
    abcd/report.csv
    jklm/report.csv
    5678/report.csv

Each CSV’s has columns & content like:

almonds, biscuits, cookies, dog_biscuits 
123, 321, 333, 444
555, 666, 777, 888
444, 551, 555, 999 (and so on for 75 lines or so)

I want to put their into a combined CSV file. I’d been using exec in a PHP file to do this:

exec("cat /path/the_folder/*/report.csv > /path/combined.csv");

And then using sed to remove the duplicate “almonds, biscuits, cookies, dog_biscuits” header rows.

Now I need to take the names of the subfolders and put them into the lines of combined.csv.

So, there will be a column added to the CSV (“subfolder_name”) and then in that column the name of the folder that line had come from. Something like

almonds, biscuits, cookies, dog_biscuits, subfolder_name
123, 321, 333, 444, 1234
555, 666, 777, 888, 1234
444, 551, 555, 999, abcd
333, 333, 111, 222, abcd
111, 222, 444, 333, abcd (etc and so on for 300 lines)

What is the smartest, simplest, most efficient way to go about doing this?

  • 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-01T18:03:09+00:00Added an answer on June 1, 2026 at 6:03 pm

    You can try

    $folderList = array (
            "1234/report.csv",
            "abcd/report.csv",
            "jklm/report.csv",
            "5678/report.csv" 
    );
    
    $combinedCSV = "result_combine.csv";
    $headers = array (
            "almonds",
            "biscuits",
            "cookies",
            "dog_biscuits" 
    );
    touch ( $combinedCSV );
    
    $fp = fopen ( $combinedCSV, 'w' );
    fputcsv ( $fp, $headers ); // Add headers
    foreach ( $folderList as $file ) {
        if (($handle = fopen ( $file, "r" )) !== FALSE) {
            while ( ($data = fgetcsv ( $handle, 1000, "," )) !== FALSE ) {
                if ($data [0] == "almonds") // Remove Headers
                    continue;
                fputcsv ( $fp, $data );
            }
            fclose ( $handle );
        }
    }
    
    fclose($fp);
    

    I hope it helps

    Thanks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that can be any thing like ZIP, RAR, txt, CSV,
We have a source XML file that has an address node, and each node
I have a function that get a ZIP file and extract it to a
I have found that creating a zip file using the Zip task provided by
I have a URL that, when requested, start the download of a .zip file.
I have a ZIP archive that's embedded inside a larger file. I know the
I have a small job that takes a text file of email/zip codes and
I have .zip file which contain csv data. I am reading .zip file using
I have a zip file containing a folder and inside the folder I have
I have programmed a class that get file with creating some parallel downloads. I

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.