Reputation: 8103
I have a folder that is filled with sub folders of past dates (20120601
for example), inside each date folder there is a file named test.txt
, along with another file named example.txt
. How can I merge all the test.txt
files into one?
I am trying to do this in Windows and have access to Windows PowerShell and Windows Command Processor (cmd.exe
). What would be the best way to do this?
My hierarchy would look something like this:
\Data
\20120601
test.txt
example.txt
\20120602
test.txt
example.txt
\20120603
test.txt
example.txt
\20120604
test.txt
example.txt
\20120605
test.txt
example.txt
I would imagine it is something like
copy *\test.txt alltestfiles.txt
Is that possible? Can you specify a wildcard for a directory?
Upvotes: 2
Views: 1912
Reputation: 1205
A simple command like cat path\**\* > target_single_file
also works.
Upvotes: 0
Reputation: 354366
Fairly easy, actually:
Get-ChildItem \Data -Recurse -Include test.txt |
Get-Content |
Out-File -Encoding UTF8 alltestfiles.txt
or shorter:
ls \Data -r -i test.txt | gc | sc -enc UTF8 alltestfile.txt
This will first gather all test.txt
files, then read their contents and finalle write out the combined contents into the new file.
Upvotes: 5
Reputation: 24071
List all the files. Read one file's content and add it into the combined file. Like so,
cd data
gci -Recurse | ? { -not $_.psIsContainer -and $_.name -like "test.txt"}
$files | % { Add-Content -Path .\AllTests.txt -Value (Get-Content $_.FullName) }
Upvotes: 1