How to Copy All Text Files Including Folder Names Into One Text File in Windows 11/10
If you have multiple text files saved across different folders and want to combine their contents into one document, Windows offers a simple way to do it using Command Prompt or PowerShell.
You can use built-in commands to collect text from multiple .txt files, include their original folder names and file names, and save everything into a single text file.
Let’s learn how!
How to Copy All Text Files Including Folder Names Into One Text File

Imagine you have a main folder containing several subfolders, and each subfolder contains multiple text files. Opening every file, copying its contents, and pasting everything into a new document can take a lot of time.
Instead, you can automate this process using a simple PowerShell script. It can search through all subfolders, read every text file, add its folder path and file name, and combine the contents into one output file.
For example, suppose your folder structure looks like this:
My Documents
│
├── Projects
│ ├── Project1.txt
│ └── Project2.txt
│
├── Notes
│ ├── Meeting.txt
│ └── Ideas.txt
│
└── Reports
└── AnnualReport.txt
After combining everything, your new text file can look something like this:
================================
Folder: Projects
File: Project1.txt
================================
Project details go here.
================================
Folder: Projects
File: Project2.txt
================================
Additional project information.
================================
Folder: Notes
File: Meeting.txt
================================
Meeting notes go here.
This approach keeps the folder and file names visible, making it much easier to identify where each piece of information came from.
Let’s go through the process step by step.
Step 1: Create a Main Folder for Your Text Files
First, identify the main folder containing all the text files you want to combine.
For example, if your text files are stored inside Documents, open File Explorer by pressing Windows + E on your keyboard.
Navigate to the location where your folders are saved. You might have a folder named Projects, Work Notes, Research, or something similar.
Make sure all the text files you want to combine are inside this main folder or its subfolders.
You don’t need to move every text file into one location. PowerShell can search through multiple folder levels automatically.
For this example, let’s assume your main folder is located at:
C:\Users\YourName\Documents\MyTextFiles
You will need its actual location in a later step.
Step 2: Copy the Main Folder Path
Now that you’ve identified the main folder, you need to copy its location.
Open the folder in File Explorer and click the address bar at the top of the window.
The folder location should become visible as a complete path. Press Ctrl + C to copy it.
For example:
D:\Work\MyTextFiles
Keep this path somewhere accessible because you’ll use it in your PowerShell command.
If your folder is stored on an external hard drive, make sure the drive is connected before continuing.
Step 3: Open Windows PowerShell
The next step is to open PowerShell, which is a built-in Windows command-line tool.
Click the Start button on your taskbar and type PowerShell in the search box.
Select Windows PowerShell or PowerShell from the search results.
You don’t need administrator permissions for this task if you have permission to read the folders and write to the output location.
Once PowerShell opens, you’ll see a command window where you can enter commands.
Don’t worry if you’ve never used PowerShell before. You only need to copy and paste the command provided in the next step.
Step 4: Enter the PowerShell Script to Combine All Text Files
This is the most important step because the script will collect all the text files and combine their contents into one document.
Copy the following PowerShell script:
$source = “D:\Work\MyTextFiles”
$output = “D:\Work\CombinedText.txt”
$files = Get-ChildItem -LiteralPath $source -Filter “*.txt” -File -Recurse |
Where-Object { $_.FullName -ne $output }
foreach ($file in $files) {
$relativeFolder = $file.DirectoryName.Substring($source.Length).TrimStart(‘\’)
if ([string]::IsNullOrWhiteSpace($relativeFolder)) {
$relativeFolder = “(Main Folder)”
}
Add-Content -LiteralPath $output -Value “========================================”
Add-Content -LiteralPath $output -Value “Folder: $relativeFolder”
Add-Content -LiteralPath $output -Value “File: $($file.Name)”
Add-Content -LiteralPath $output -Value “========================================”
Get-Content -LiteralPath $file.FullName -Raw |
Add-Content -LiteralPath $output
Add-Content -LiteralPath $output -Value “`r`n”
}
Before running it, replace the example folder locations with your actual paths.
For example, if your source folder is C:\Users\Liam\Documents\TextFiles, change the first line to:
$source = “C:\Users\Liam\Documents\TextFiles”
You can save the combined file somewhere outside the source folder, such as:
$output = “C:\Users\Liam\Documents\CombinedText.txt”
What does this script do?
The $source variable tells PowerShell where your original text files are stored. The $output variable specifies where the combined document should be saved.
The Get-ChildItem command searches for all .txt files, including those inside nested subfolders. The foreach loop processes each file individually.
The script then adds the relative folder name and original file name before copying the file’s contents into the output document.
This means you can identify the source of each section without opening the original files individually.
Step 5: Run the Script and Create the Combined Text File
Once you’ve updated the folder paths, paste the complete script into the PowerShell window.
Press Enter to execute it.
PowerShell will begin searching through the main folder and its subfolders. It will read each text file and append its contents to the output document.
Depending on the number and size of your text files, this process may take a few seconds or several minutes.
If everything works correctly, PowerShell will finish without displaying an error.
Now open File Explorer and navigate to the output location you specified in the script.
You should find a new file named CombinedText.txt.
Double-click it to open it in Notepad. You should see the folder names, file names, and text contents arranged in separate sections.
Important: This script appends content to the output file. If you run it again using the same output file, it may duplicate the existing content. Delete or rename the previous output file before running the script again.
Step 6: Verify the Folder Names and Text File Contents
After creating the combined document, take a moment to check whether everything was copied correctly.
Open CombinedText.txt in Notepad and scroll through the document.
Check whether each section includes the correct folder name and original text file name. Also, compare a few sections with their original files to make sure the contents are present.
If you notice that a particular text file is missing, check whether it uses the .txt extension. The script searches for .txt files, so documents with extensions such as .log, .csv, or .md won’t be included.
Also, check whether you have permission to access the original folders.
If you want to include other text-based file extensions, you can modify the search command accordingly.
For example, you can use a filter that searches for multiple extensions:
Get-ChildItem -LiteralPath $source -File -Recurse |
Where-Object { $_.Extension -in “.txt”, “.log”, “.md” }
You would replace the original $files search portion with this command in the script.
Step 7: Save and Organize Your Combined Text File
Once you’ve verified the contents, you can organize the final document according to your needs.
Open CombinedText.txt in Notepad and press Ctrl + S to save any additional changes.
You can also rename the file to something more meaningful, such as AllProjectNotes.txt, CombinedReports.txt, or CompleteTextBackup.txt.
If the document contains a lot of information, consider using Notepad’s Find feature by pressing Ctrl + F. You can search for a folder name, file name, keyword, or specific sentence without manually scrolling through the entire document.
One useful tip is to keep the original text files unchanged. The script reads their contents and creates a separate combined document, so your source files remain available.
FAQs
1. Can I combine multiple text files into one file without installing software?
Yes. You can use Windows PowerShell or Command Prompt to combine multiple text files. Both tools are included with Windows 11 and Windows 10.
2. Can I include folder names and file names in the combined text file?
Yes. PowerShell can retrieve the folder path and file name of each text document. You can add this information before the contents of each file, making it easier to identify the original location.
3. Does this method include text files inside subfolders?
Yes. The -Recurse parameter tells PowerShell to search through the main folder and all its subfolders, including deeper folder levels.
4. Will combining text files delete or modify the original files?
No. The script only reads the original files and writes their contents into a separate output document. Your original files remain unchanged.
5. Can I combine TXT files from multiple drives?
Yes. You can use a source folder located on another drive, such as D:\Documents or E:\Backup. Just make sure the drive is connected and the folder path is correct.
6. Why is my combined text file empty?
This usually happens when the source folder path is incorrect, the folder doesn’t contain any .txt files, or PowerShell cannot access the files. Double-check your source path and confirm that the files have the correct extension.
7. Can I combine PDF, Word, and Excel files using the same method?
Not directly. This script is designed for plain text files. PDF, DOCX, and XLSX files use different formats and require additional tools or scripts to extract their contents properly.
