Simple script to count NLOC?

2019-03-28 13:15发布

Do you know a simple script to count NLOCs (netto lines of code). The script should count lines of C Code. It should not count empty lines or lines with just braces. But it doesn't need to be overly exact either.

14条回答
Emotional °昔
2楼-- · 2019-03-28 13:50

The following script will get count of all file matching a pattern in a given directory.

# START OF SCRIPT

var str files
var str dir

set $files = "*.cpp" # <===================== Set your file name pattern here.
set $dir = "C:/myproject" # <===================== Set your project directory here.

# Get the list of files in variable fileList.
var str fileList
find -rn files($files) dir($dir) > $fileList

# Declare variables where we will save counts of individual files.
var int c # all lines
var int nb # non-blank lines

# Declare variables where we will save total counts for all files.
var int totalc # sum-total of all lines
var int totalnb # sum-total of all non-blank lines

# Declare variable where we will store file count.
var int fileCount

# We will store the name of the file we are working on currently, in the following.
var str file

# Go thru the $fileList one by one file.
while ($fileList<>"")
do
# Extract the next file.
lex "1" $fileList >$file

# Check if this is a flat file. We are not interested in directories.
af $file >null # We don't want to see the output.
# We only want to set the $ftype variable.
if ($ftype=="f")
do
# Yes, this is a flat file.

 # Increment file count.<br>
set $fileCount = $fileCount+1<br>

# Collect the content of $file in $content<br>
var str content # Content of one file at a time<br>
repro $file >$content<br>

# Get count and non-blank count.<br>
set $c={len -e $content}<br>
set $nb={len $content}<br>

echo -e "File: " $file ", Total Count: " $c ", Non-blank Count: " $nb<br>

# Update total counts.<br>
set $totalc = $totalc + $c<br>
set $totalnb = $totalnb + $nb<br>

done
endif

done

Show sum-totals

echo "**********************************************************************************************************************************"
echo "Total Count of all lines:\t" $totalc ",\tTotal Count of non-blank lines:\t" $totalnb ", Total files: " $fileCount
echo "**********************************************************************************************************************************"

# END OF SCRIPT

If you want line counts in files modified in year 2008 only, add ($fmtime >= "2008"), etc.

If you don't have biterscripting, get it from .com .

查看更多
Summer. ? 凉城
3楼-- · 2019-03-28 13:50

Not a simple script, but CCCC (C and C++ Code Counter) has been around for a while and it works great for me.

查看更多
登录 后发表回答