Last active
February 6, 2020 21:17
-
-
Save scottmwyant/6cdb5c331d74f0f7e7ea239164ec377b to your computer and use it in GitHub Desktop.
Combine multiple text files into one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Title{ | |
param([string]$Title) | |
Clear-Host | |
Write-Host | |
Write-Host " -- $Title --" | |
Write-Host | |
} | |
function Get-Path{ | |
param([string]$Prompt, [string]$DefaultValue) | |
Write-Host $Prompt | |
Write-Host " Default [$DefaultValue]" | |
Do{ | |
$target = Read-Host -Prompt ' ' | |
if ($target -eq ''){ | |
$target = $DefaultValue | |
Write-Host | |
$target | |
Break | |
} | |
elseif ((Test-Path $target -PathType Container) -eq $false){ | |
Write-Host " Error: Invalid path" | |
Write-Host | |
} | |
else{ | |
Write-Host | |
$target | |
Break | |
} | |
} | |
Until($false) | |
} | |
function Get-FileName{ | |
param([string]$Prompt, [string]$DefaultValue) | |
Write-Host $Prompt | |
Write-Host " Default [$DefaultValue]" | |
Do{ | |
$target = Read-Host -Prompt ' ' | |
if ($target -eq ''){ | |
$target = $DefaultValue | |
} | |
if(Test-ValidFileName $target){ | |
Write-Host | |
$target | |
Break | |
} | |
else{ | |
Write-Host " Error: Invalid file name" | |
Write-Host | |
} | |
} | |
Until($false) | |
} | |
function Test-ValidFileName | |
{ | |
param([string]$FileName) | |
$IndexOfInvalidChar = $FileName.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) | |
# IndexOfAny() returns the value -1 to indicate no such character was found | |
if($IndexOfInvalidChar -eq -1) | |
{ | |
return $true | |
} | |
else | |
{ | |
return $false | |
} | |
} | |
function Get-Flag{ | |
param([string]$Prompt, [string]$DefaultValue) | |
Write-Host $Prompt | |
Write-Host " Default [$DefaultValue]" | |
Do{ | |
$target = Read-Host -Prompt ' ' | |
if(($target.ToLower() -eq 'n') -or ($target.ToLower() -eq 'no') -or ($target.ToLower() -eq 'false')){ | |
Write-Host | |
return $false | |
} | |
elseif(($target.ToLower() -eq '') -or ($target.ToLower() -eq 'y') -or ($target.ToLower() -eq 'yes') -or ($target.ToLower() -eq 'true')) { | |
Write-Host | |
return $true | |
} | |
}Until($false) | |
} | |
# Get Started | |
Write-Title "Combine-TextFiles" | |
# Get the folder to look in | |
$target = Get-Path 'Specify a target directory containing text files' $PSScriptRoot | |
# Get the path to use for output | |
#$outPath = Get-Path 'Specify a directory for output' ($env:HOMEDRIVE+$env:HOMEPATH+'\Documents') | |
$outPath = Get-Path 'Specify a directory for output' $PSScriptRoot | |
# Get the file name to output | |
$outFile = Get-FileName 'Specify a name for the output file' 'combined.txt' | |
# See if we want to output file names in the summary | |
$flag = Get-Flag 'Include file names in output? y/n' 'y' | |
# Combine the output path and filename | |
$outputFile = $outPath+'\'+$outFile | |
# When the file already exists, delete it | |
if(Test-Path $outFile -PathType Leaf){ | |
Remove-Item -Force $outFile | |
} | |
# Loop over each item, add the item name to the file then the content | |
Get-ChildItem $target -Filter *.txt | | |
Foreach-Object { | |
if($flag -eq $true){ | |
Add-Content -Path $outputFile -Value ('File: ' + $_.Name) | |
} | |
Get-Content $_.FullName | Add-Content -Path $outputFile | |
} | |
Write-Host " - Done -" | |
Write-Host " Combined [$target]" | |
Write-Host " Into [$outputFile]" | |
Write-Host | |
Read-Host -Prompt 'Press any key to exit . . . ' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment