PowerShell-从多个特定嵌套文件复制特定行

PowerShell-从多个特定嵌套文件复制特定行,powershell,Powershell,因此,文件夹结构如下所示: 根文件夹 根文件夹 子文件夹1 子文件夹1 totals.txt 子文件夹2 totals.txt 子文件夹2 子文件夹1 totals.txt 子文件夹2 totals.txt 我要做的是递归地遍历totals.txt文件的这些子文件夹。读取内容,并将第22、26、30、34、38和42行(第一行为0而不是1)复制到单个组合文件中 我从以下代码开始: Get-ChildItem -Recurse | Where-Object {$_

因此,文件夹结构如下所示:

根文件夹

  • 根文件夹
    • 子文件夹1
      • 子文件夹1
        • totals.txt
      • 子文件夹2
        • totals.txt
    • 子文件夹2
      • 子文件夹1
        • totals.txt
      • 子文件夹2
        • totals.txt
我要做的是递归地遍历totals.txt文件的这些子文件夹。读取内容,并将第22、26、30、34、38和42行(第一行为0而不是1)复制到单个组合文件中

我从以下代码开始:

Get-ChildItem -Recurse | Where-Object {$_ -like "totals.txt" } | Get-Content | Select-Object -Index 22,26,30,34,38,42  | Add-Content "DataInportFile.txt"
但是,这只会找到RootFolder\SubFolder\SubSubFolder\totals.txt,然后退出脚本。不是我要找的

我需要的是上面的脚本继续递归地搜索下一个文件,直到在结构中搜索完所有目录。所以我用了这个:

Get-ChildItem -Recurse | ForEach-Object {$_ -like "totals.txt" } | Get-Content | Select-Object -Index 22,26,30,34,38,42  | Add-Content "DataInportFile.txt"
但是,此脚本会出现错误

C:\Users\user1\scripts\Untitled1.ps1:1 char:69
+ ... urse | ForEach-Object {$_ -like "totals.txt" } | $_.filename #| Get- ...
+                                                       ~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

我需要一些帮助来理解这个powershell脚本的其余部分。如此接近,却没有欢乐。谢谢你的帮助。

意识到我需要遍历阵列

供他人参考,本规范按要求执行:

Get-ChildItem -Recurse | Where-Object {$_ -like "totals.txt" } | 
    %{
        (Get-Content $_.FullName) | Select-Object -Index 22,26,30,34,38,42  | Add-Content "DataInportFile.txt"
        }

Get ChildItem-Recurse-Filter“totals.txt”|
不使用where更有效。