【发布时间】:2020-07-14 16:03:44
【问题描述】:
感谢@mklement0 提供的帮助,并在Powershell search directory for code files with text matching input a txt file 中给出了答案。
下面的 Powershell 非常适合在源代码文件夹中查找出现的一长串数据库字段名称。
$inputFile = 'C:\DataColumnsNames.txt'
$outputFile = 'C:\DataColumnsUsages.txt'
Get-ChildItem C:\ProjectFolder -Filter *.cs -Recurse -Force -ea SilentlyContinue |
Select-String -Pattern (Get-Content $inputFile) |
Select-Object Path, LineNumber, line |
Export-csv $outputfile
但是,许多源代码行有多个匹配项,尤其是 ADO.NET SQL 语句,其中一行中有很多字段名称。如果匹配输出中包含字段名称参数,则结果将更直接有用,而无需额外的按摩,例如将所有内容与原始字段名称列表对齐。例如,如果有一个源行“BatchId = NewId”,它将匹配字段名称列表项“BatchId”。有没有一种简单的方法可以在输出中同时包含“BatchId”和“BatchId = NewId”?
玩了火柴对象,但它似乎没有信息。也尝试过像这里一样的管道变量,但 X 为空。
$inputFile = 'C:\DataColumnsNames.txt'
$outputFile = 'C:\DataColumnsUsages.txt'
Get-ChildItem C:\ProjectFolder -Filter *.cs -Recurse -Force -ea SilentlyContinue |
Select-String -Pattern (Get-Content $inputFile -PipelineVariable x) |
Select-Object $x, Path, LineNumber, line |
Export-csv $outputile
谢谢。
【问题讨论】: