A PowerShell Script for Combining Files
Combine-Files.ps1 is a flexible PowerShell script for combining multiple text based files into a single file. Files can be filtered by name or by modified date and files can also be appended to an existing file. The script can be used on its own or as part of a larger build script for combining *.sql, *.js or *.csv files.
The script takes several parameters and can be used in a number of ways.
- The path (relative or absolute) of the output file.
- The path (relative or absolute) of the source file or files
- The filter to be applied to the source when retrieving files
- Only retrieve files which have been modified after this datetime
- Only retrieve files which have been modified before this datetime
- Switch to append files to an existing output file
- Switch to retrieve files recursively from child folders
- Switch to put a separator between each file which includes the file path
(Parameters are shown on multiple lines for legibility.)
Example 1 - Basic example
.\Combine-Files.ps1 -output "..\CombinedFiles.sql" -source "D:\Database\Scripts\" -filter "*.sql"
Example 2 - Filter by date and append recursively selected files
.\Combine-Files.ps1 -output "..\CombinedFiles.sql" -source "D:\Database\Scripts\" -filter "*.sql" -modifiedAfter "2011-11-11 11:11:11" -modifiedBefore "2012-12-12 12:12:12" -append -recurse -separator
Example 3 - Combine files from multiple locations
The script can be called multiple times, either from the console or from another script to combine files from multiple locations. The "-append" switch ensures that the output file is appended to and not overwritten.
.\Combine-Files.ps1 -output "CombinedFiles.txt" -source "folder1\" -filter "*.txt" .\Combine-Files.ps1 -output "CombinedFiles.txt" -source "folder1\folder2\folder3\" -filter "*.txt" -append