declan bright

A PowerShell Script for Combining Files

PowerShell

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.

Download

Parameters

The script takes several parameters and can be used in a number of ways.

-output
The path (relative or absolute) of the output file.
-source
The path (relative or absolute) of the source file or files
-filter
The filter to be applied to the source when retrieving files
-modifiedAfter
Only retrieve files which have been modified after this datetime
-modifiedBefore
Only retrieve files which have been modified before this datetime
-append
Switch to append files to an existing output file
-recurse
Switch to retrieve files recursively from child folders
-separator
Switch to put a separator between each file which includes the file path

Examples

(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
View Comments