首页 > 解决方案 > 如何搜索目录中的所有txt文件(包括子文件夹)

问题描述

我想要一个可以查看目录的所有子文件夹并将txt文件的所有内容编译成一个的命令。

我有一个为文件夹执行此操作的代码。

@echo on
erase Compiled.txt
type *.txt>> Compiled.txt

我如何使它能够查看所有子文件夹?

谢谢

标签: cmd

解决方案


如果您可以使用 PowerShell,则可以这样做。

$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
    }

如果你必须在 1987 年的老式 cmd.exe 中运行,它可以被扭曲成可用的形式。

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" ^
    "    }"

推荐阅读