问题描述
我有下面的脚本,它将计算目录和子目录中所有文件的所有行。 工作正常并可以很好地创建输出文件。 我现在遇到的问题是,所有文件中都有注释和可执行行,我需要将两者分开。 我必须计算位置 7 处有星号的所有行。这些是注释。总行数减去注释行的简单计算将提供我需要的最后一个工件,即可执行行。 有人可以帮忙修改下面的代码,只计算位置 7 的 Asterisk。
先谢谢你, -罗恩
$path='C:\'
$outputFile='C:\Output.csv'
$include='*.cbl'
$exclude=''
param([string]$path,[string]$outputFile,[string]$include,[string]$exclude)
Clear-Host
Get-ChildItem -re -in $include -ex $exclude $path |
Foreach-Object { Write-Host "Counting '$($_.Name)'"
$fileStats = Get-Content $_.FullName | Measure-Object -line
$linesInFile = $fileStats.Lines
"$_,$linesInFile" } | Out-File $outputFile -encoding "UTF8"
Write-Host "Complete"
解决方法
我会做这样的事情
$linesInFile = 0
switch -Regex -File $_.FullName {
'^.{6}\*' { <# don't count this line #> }
default { $linesInFile++ }
}
这也应该比使用 Get-Content 更快。
附言此外,将 -File
添加到 Get-ChildItem
有助于消除处理目录。