Advertisement
EmilySamantha80

Count lines of code

Nov 30th, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $CodeFiles = Get-ChildItem -include *.cs,*.aspx,*.js,*.css,*.master,*.html -exclude *.designer.cs,jquery* -recurse |
  2.     Where-Object { $_.FullName -notmatch '\\(obj|packages|release|debug|plugin-.*)\\' }
  3.  
  4. $TotalFiles = $CodeFiles.Count
  5.  
  6. $IndividualResults = @()
  7.  
  8. $CommentLines = ($CodeFiles | ForEach-Object{
  9.     #Get the comments via regex
  10.     $Comments = ([regex]::matches(
  11.         [IO.File]::ReadAllText($_.FullName),
  12.         '(?sm)^[ \t]*(//[^\n]*|/[*].*?[*]/)'
  13.     ).Value -split '\r?\n') | Where-Object { $_.length -gt 0 }
  14.    
  15.     #Get the total lines
  16.     $Total = ($_ | select-string .).Count
  17.     #Add to the results table
  18.     $IndividualResults += @{
  19.         File = $_.FullName | Resolve-Path -Relative;
  20.         Comments = $Comments.Count;
  21.         Code = ($Total - $Comments.Count)
  22.         Total = $Total
  23.     }
  24.     Write-Output $Comments
  25. }).Count
  26.  
  27. $TotalLines = ($CodeFiles | select-string .).Count
  28.  
  29. $TotalResults = New-Object PSObject -Property @{
  30.     Files = $TotalFiles
  31.     Code = $TotalLines - $CommentLines
  32.     Comments = $CommentLines
  33.     Total = $TotalLines
  34. }
  35.  
  36. Write-Output (Get-Location)
  37. Write-Output $IndividualResults | % { new-object PSObject -Property $_} | Format-Table File,Code,Comments,Total
  38. Write-Output $TotalResults | Format-Table Files,Code,Comments,Total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement