I have a directory of subfolders. Each one contains text files within it. I am trying to combine the files found in each subfolder.
Example:
SubFolder 1 → a.txt + b.txt + c.txt → SubFolder1Merged.txt
SubFolder 2 → x.txt + y.txt + z.txt → SubFolder2Merged.txt
I have referenced this thread.
This is what I have so far:
$startingDir = "C:\Users\WP\Desktop\TextFiles"
function CombineLogs {
param([string]$startingDir)
dir $startingDir -Filter *.txt | Get-Content |
Out-File (Join-Path $startingDir COMBINED.txt)
dir $startingDir | ?{ $_.PsIsContainer } | %{ CombineLogs $_.FullName }
}
CombineLogs 'C:\Users\WP\Desktop\CombinedTextFiles' #output the combined text files here
I get a combined.txt
generated in CombinedTextFiles - but not individual files merged.
Also the file is empty.
I simply want to loop through each subfolder, merge the text files within each folder, then output to my CombinedTextfiles Folder.
Recursion can be tricky if you don't know how to handle it. And in this case you don't need to implement recursion yourself anyway. Just let PowerShell do the heavy lifting for you:
Above code snippet would solve
TextFilesCOMBINED.txt
andNewCOMBINED.txt
however does not solveABCCOMBINED.txt
norxyzCOMBINED.txt
in next scenario: