[Combine CSV Files](https://happycsv.com/blog/merge-multiple-csv-files) from Multiple Folders
How to merge CSV files scattered across different subfolders. Recursive merging using Command Line and Python.
Combine CSV Files from Multiple Folders
You have a data dump organized like this:
/2023
/Jan/data.csv
/Feb/data.csv
/2024
/Jan/data.csv
You want ONE master CSV file containing everything.
Standard merge tools often only look in one folder. Here is how to do a "Recursive Merge".
Method 1: Command Line (Windows)
- Open Command Prompt (
cmd). - Navigate to the top folder:
cd C:\MyData - Run this command:
for /r %i in (*.csv) do @type "%i" >> all_data.csv
The Problem: This includes headers from every file repeatedly in the output. The Fix: You'll need to use a tool to remove duplicate header rows later.
Method 2: Command Line (Mac/Linux)
- Open Terminal.
- Navigate to folder.
- Run:
find . -name "*.csv" -exec cat {} + > all_data.csv
Same Problem: Repeated headers.
Better Version (Skip headers for subsequent files): This is complex in bash. It's easier to use Python.
Method 3: Python (The Clean Way)
This script walks through all folders, finds CSVs, and merges them smartly (keeping only one header).
import os
import pandas as pd
master_df = pd.DataFrame()
# Walk through all folders
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".csv"):
path = os.path.join(root, file)
# Read CSV
df = pd.read_csv(path)
# Add a 'Source' column so you know where it came from
df['Source_File'] = path
# Append to master
master_df = pd.concat([master_df, df])
master_df.to_csv("combined_all.csv", index=False)
Method 4: Power Query (Excel)
- Data > Get Data > From File > From Folder.
- Select the top-level folder.
- Excel will show a list of all files in all subfolders.
- Click Combine & Load.
- Excel automatically handles the headers and merges them.
Pros: No code, handles headers correctly. Cons: Can be slow with thousands of files.
Summary
- Quick & Dirty: Command line (but watch out for repeated headers).
- Clean & Automated: Python script.
- User Friendly: Excel Power Query.
Files everywhere? HappyCSV can accept drag-and-drop of multiple files and combine them instantly.
Related Articles
Anonymize CSV Data (GDPR/Testing)
How to mask sensitive data in CSV files. Anonymize names, emails, and phones for testing or GDPR compliance.
Batch Convert Multiple Excel Files to CSV
How to convert 100 Excel files to CSV at once. Use VBA macros, Python scripts, or batch converters to save hours of manual work.
Best Free CSV Viewers for Mac & Windows
Excel isn't the only way to open CSVs. Check out the best free CSV viewers like Tad, Miller, and online tools for large files.
Need to handle CSV files?
HappyCSV is the free, secure way to merge, split, and clean your data — all in your browser.