powershell - 合并不同目录中的同名 .txt 文件?

标签 powershell text merge command-prompt

我有一个文件夹,里面装满了过去日期的子文件夹(例如20120601),在每个日期文件夹中都有一个名为test.txt的文件,以及使用另一个名为 example.txt 的文件。如何将所有 test.txt 文件合并为一个文件?

我正在尝试在 Windows 中执行此操作,并且可以访问 Windows PowerShell 和 Windows 命令处理器 (cmd.exe)。最好的方法是什么?

我的层次结构看起来像这样:

\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

我想是这样的

copy *\test.txt alltestfiles.txt

这可能吗?可以为目录指定通配符吗?

最佳答案

其实很简单:

Get-ChildItem \Data -Recurse -Include test.txt |
  Get-Content |
  Out-File -Encoding UTF8 alltestfiles.txt

或更短:

ls \Data -r -i test.txt | gc | sc -enc UTF8 alltestfile.txt

这将首先收集所有 test.txt 文件,然后读取它们的内容,最后将组合的内容写到新文件中。

关于powershell - 合并不同目录中的同名 .txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11294862/

相关文章:

c# - 在 Cmdlet 中,如何检测是否设置了调试标志?

windows - Docker 在 Windows 容器上的健康检查和退出代码 ("||"linux 等效)

unix - 如何在没有最后一行的情况下输出文件的所有行?

HTML & CSS : Adjust background rectangle on text so it does not span entire webpage

fonts - 合并ttf文件

powershell - 我能做什么! (感叹号)什么时候使用 Read-Host?

xml - PowerShell 解析 xml 并保存更改

c# - 如何获得 Fry Graph 可读性公式的级别?

python - 合并可变数量的嵌套字典的通用函数

python - 合并没有匹配列的数据框