At Bay
At Bay

Reputation: 1

How to search for all txt files in a directory (including subfolders)

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

Answers (3)

lit
lit

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

K J
K J

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

YouryDW
YouryDW

Reputation: 393

dir /s /b /a:-d *.txt

might do the trick for ya :)

Upvotes: 1

Related Questions