Click here to Skip to main content
15,879,095 members
Articles / Operating Systems / Windows
Tip/Trick

Powershell Line Count Utility Tip

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
2 Apr 2017CPOL1 min read 8K   3  
A Powershell command to count lines of code (loc), so I can look it up quicker in the future
Counting lines of code using Powershell - a quick introduction.

Introduction

Ever tried to get a quick LOC (lines of code) count on your project, but Visual Studio only tallies them for managed solutions? And you are grinding away in C++, or another language that isn't supported? Well, here you go, assuming Powershell access!

Background

There is no background to this tip, except I found it on technet: link. I'm only putting it on CodeProject so I can find it here, which is typically quicker than digging through Google again. (Yes, I often search my own articles! And saved a lot of time as a result!)

Using the Code

First, in Explorer, browse to the subdirectory where your code resides. Then type 'powershell' in the Explorer bar, which will open a copy of Powershell there. Then type:

PowerShell
(dir -include *.cpp, *.h -recurse | select-string "^").Count

To exclude blank lines:

PowerShell
(dir -include *.cpp, *.h -recurse | select-string "^(\s*)$" -notMatch).Count

Or, to also exclude '//' comments:

PowerShell
(dir -include *.cpp, *.h -recurse | select-string "^(\s*)//" -notMatch | 
 select-string "^(\s*)$" -notMatch).Count

Change the '*.cpp' and '.h' to whatever file extensions you are interested in. Remove '*.cpp' if you are only interested in headers.

Points of Interest

While typing this up, I found that the original solution counted every character, and my project really didn't have 1.5 million LOC! The corrected attribution link is this one. There is even a version in the comments that strips '/* ... */' multi-liners.

History

Initial submission: Ground my teeth in frustration upon finding that solution wasn't the solution! Googled and prevailed! (Google-fu search phrase: "powershell count lines code".)

One edit followed, to get the strikethrough working correctly, thanks to a pointer by Sean.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer www.randommonkeyworks.com
United States United States
I am the author of Laughing at the Devil: One Man’s Religious Discoveries. If you want to understand the astronomic investigations of our priests 3,000 years ago, LATD is the book to turn to. It opens up the thoughts that pushed them away from their earlier polytheism and towards our current definition of God.

Trained as a mechanical engineer, I have been involved with design, supervision, and project management. I taught myself C++ programming in order to play around with binaural beats more easily. I've also created various databases to help with project management and personal tasks.

Databases are cool and extremely useful! Happy coding, everybody!

Comments and Discussions

 
-- There are no messages in this forum --