SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

May 26, 2007 – 2:59am Count your SLOC: Source Lines of Code

Although this number may mean nothing at all, counting the total number of Source Lines of Code (SLOC) can be fun. Here is how you can tally up all the lines of PHP you have in a project from the command line:

find . -regex '.*\.\(php\)' -print0 | xargs -0 cat | wc -l

You can read more about this on wikipedia.

Posted by in  Web Development   |  

One Response to Count your SLOC: Source Lines of Code

  1. P Mc says:

    The following script does a little better job. Try it. If you don’t have biterScripting, download it from http://biterscripting.com/

    #####################################################################
    # SCRIPT: SS_CountLines
    #
    # This script counts the number of lines in files whose names match a given
    # criterion in a given directory.
    #
    # The file name criterion is assigned using FVA (Forward Variable Assignment)
    # for str variable $files. The value of $files is of form
    #	"*.html", "*.js", "*.cpp", "*.h", etc.
    #
    # The directory name is assigned using FVA
    # for str variable $dir. The value of $dir is of form
    #	"C:/ABC/DEF", ".", etc.
    #
    # This script can be stored and edited as necessary, in a text file
    # called SS_CounteLines.txt in a directory in your $path . The script can then be called as
    #
    #	script SS_CountLines.txt dir("C:/myproject") files("*.cpp")
    # 
    #####################################################################
     
    # Declare FVA variables.
    var str files
    var str dir
     
    # Get the list of files in variable fileList.
    var str fileList
    script SS_ListFiles.txt dir($dir) files($files) > $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 directries.
      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.
        set $fileCount = $fileCount+1
     
        # Collect the content of $file in $content
        var str content	# Content of one file at a time
        repro $file >$content
     
        # Get count and non-blank count.
        set $c={len -e $content}
        set $nb={len $content}
     
        # Update total counts.
        set $totalc = $totalc + $c
        set $totalnb = $totalnb + $nb
     
        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 "**********************************************************************************************************************************"