Powershell - 计算文件中的行数 excluding/including 某些行
Powershell - Count lines in files excluding/including certain lines
我有下面的脚本,可以计算目录和子目录中所有文件的所有行数。
工作正常并可以正常创建输出文件。
我现在遇到的问题是,所有文件中都有注释和可执行行,我需要将两者分开。
我必须计算位置 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 更快。
P.S。此外,将 -File
添加到 Get-ChildItem
有助于消除处理目录。
我有下面的脚本,可以计算目录和子目录中所有文件的所有行数。 工作正常并可以正常创建输出文件。 我现在遇到的问题是,所有文件中都有注释和可执行行,我需要将两者分开。 我必须计算位置 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 更快。
P.S。此外,将 -File
添加到 Get-ChildItem
有助于消除处理目录。