如何从 .csproj 文件中获取匹配行并使用 Powershell 将其写入不同的输出文件

How to get matching lines from .csproj files and writing it to different output files using Powershell

我在 c:\test\**\**\*.csproj 的子目录中有 .csproj 文件

首先我想读取 .csproj 文件,如果 pattren 匹配 '' 打印所有具有匹配模式的行到名称为 c:\output\new.(.csproj 文件名它正在阅读)_QA_ABC

注意:我需要多个名称为 "new.(.csproj file name)_QA_ABC"

的输出文件

我正在尝试这个

     $configFiles = Get-ChildItem c:\test\**\**\*.csproj

        foreach ($file in $configFiles)
       {
         (Get-Content $file) | 
           Foreach-Object {
           if ( $_ -match '<HintPath>' ) | 
             % { Select -ExpandProperty line } | Out-File "c:\output\`new.$_`_QA`_ABC"
           } 
       }

谁能帮帮我

谢谢

你很接近。

Get-ChildItem c:\test\*\*\*.csproj | 
ForEach-Object {
    $_ | 
    Get-Content | 
    Where-Object { $_ -match '<HintPath>' } | 
    Out-File "c:\output\new.$($_.Name)_QA_ABC" 
}

Get-ChildItem c:\test\*\*\*.csprojc:\test 中获取所有 .csproj 文件正好 2 个文件夹。 (您可以考虑使用 Get-ChildItem c:\test -filter *.csproj -recurse 而不是递归地获取 c:\test 下的所有 .csproj 文件)

然后将输出通过管道传输到 ForEach-Object cmdlet,它基本上实现与您拥有的 foreach 循环相同的功能。将输入管道输入到 cmdlet 更像 PowerShell。 $_ForEach-Object cmdlet 中的循环变量。它作为 Get-Content cmdlet 的输入通过管道传输,然后过滤以匹配 '<HintPath>',过滤后的行使用 Out-File cmdlet 输出到文件。