Reputation: 1
I want a command that can look through all the subfolders of a directory and compile all the contents of the txt files into one.
I have a code that does this for a folder.
@echo on
erase Compiled.txt
type *.txt>> Compiled.txt
How do I make it so that it looks through all subfolders as well?
Thank you
Upvotes: 0
Views: 4244
Reputation: 16236
If you can use PowerShell, this might do it.
$CompiledFile = '.\Compiled.txt'
if (Test-Path -Path $CompiledFile) { Remove-Item -Path $CompiledFile }
Get-ChildItem -File -Recurse -Filter '*.txt' |
ForEach-Object {
Get-Content $_.FullName | Out-File -FilePath $CompiledFile -Encoding ascii -Append
}
If you must run in the 1987 vintage cmd.exe, it can be twisted into a usable form.
powershell -NoLogo -NoProfile -Command ^
"$CompiledFile = '.\Compiled.txt'" ^
"if (Test-Path -Path $CompiledFile) { Remove-Item -Path $CompiledFile }" ^
"Get-ChildItem -File -Recurse -Filter '*.txt' |" ^
" ForEach-Object {" ^
" Get-Content $_.FullName | Out-File -FilePath $CompiledFile -Encoding ascii -Append" ^
" }"
Upvotes: 0
Reputation: 11730
Since the other one line answer does not cut it, start from reading
> For /?
then try
@echo on
erase Compiled.txt
For /R %%f in (*.txt) do echo type "%%f"
to see behaviour then if happy remove echo and add your redirection >> Compiled.txt
Just beware that very simplistic approach means that compiled.txt may also be embedded thus use either >> Compiled.TEXT (and rename as last step) OR output to a non included folder like >>"..\Compiled.txt"
Upvotes: 0