Skip to content

Instantly share code, notes, and snippets.

@ThioJoe
Created September 9, 2024 17:48
Show Gist options
  • Save ThioJoe/c7ad49020dac2337e173f9f8349deb01 to your computer and use it in GitHub Desktop.
Save ThioJoe/c7ad49020dac2337e173f9f8349deb01 to your computer and use it in GitHub Desktop.
PowerShell Script to combine files in a directory into a single text file
param(
[string]$FolderPath,
[string]$Extension = "txt",
[ValidateSet("name", "date", "name-reverse", "date-reverse")]
[string]$Sort = "name",
[switch]$Recursive
)
Write-Host "------------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host "NOTE - You can customize the script behavior with arguments, for example: "
Write-Host " .\ConcatenateTextFiles.ps1 [-FolderPath <path>] [-Extension <file extension to combine>] [-Sort <option>] [-Recursive]"
Write-Host ""
Write-Host " Sorting Options:"
Write-Host " -Sort name Sort files by name in ascending order (default)"
Write-Host " -Sort date Sort files by modification date in descending order (newest first)"
Write-Host " -Sort name-reverse Sort files by name in descending order"
Write-Host " -Sort date-reverse Sort files by modification date in ascending order (oldest first)"
Write-Host " Additional Options:"
Write-Host " -Recursive Search for files in subfolders as well"
Write-Host ""
Write-Host " Note: You can specify the extension with or without a leading period (e.g., 'txt' or '.txt')"
Write-Host ""
Write-Host ""
if (-not $FolderPath) {
$FolderPath = Read-Host "Please enter the folder path containing the text files"
}
# Remove quotes from the folder path if present
$FolderPath = $FolderPath.Trim('"')
# Ensure the extension starts with a period
$Extension = $Extension.TrimStart('.')
$Extension = ".$Extension"
if (Test-Path -Path $FolderPath -PathType Container) {
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$outputFilePrefix = "CombinedTextFile_"
$outputFileName = "$outputFilePrefix$timestamp.txt"
$outputFilePath = Join-Path -Path $FolderPath -ChildPath $outputFileName
$separator = "-" * 40 # Separator line
$combinedContent = @()
# Update the Get-ChildItem command to use the Recursive parameter if specified
$getChildItemParams = @{
Path = $FolderPath
Filter = "*$Extension"
File = $true
}
if ($Recursive) {
$getChildItemParams['Recurse'] = $true
}
$files = Get-ChildItem @getChildItemParams | Where-Object { !$_.Name.StartsWith($outputFilePrefix) }
switch ($Sort) {
"name" {
$files = $files | Sort-Object -Property Name
}
"date" {
$files = $files | Sort-Object -Property LastWriteTime -Descending
}
"name-reverse" {
$files = $files | Sort-Object -Property Name -Descending
}
"date-reverse" {
$files = $files | Sort-Object -Property LastWriteTime
}
}
$files | ForEach-Object {
$fileName = $_.FullName.Substring($FolderPath.Length + 1)
$fileContent = Get-Content $_.FullName
$combinedContent += "$separator $fileName $separator"
$combinedContent += $fileContent
$combinedContent += ""
}
$combinedContent | Out-File -FilePath $outputFilePath -Encoding UTF8
Write-Host "Text files concatenated successfully. Output file: $outputFilePath"
}
else {
Write-Host "Invalid folder path or folder does not exist."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment