You are not logged in.

#1 2009-03-10 12:11:14

Olnex
Member
Registered: 2008-04-10
Posts: 89

Need script or Program to count lines of source code

Hello, I need a script or a program that can count how many lines are there in a directory contains source code as well as other directories(contain code as well), there are .java files in these directories, the script or program should count number of lines with/without comments(starts with // or surrounded by /**/)

Thanks a lot!!!

Offline

#2 2009-03-10 12:32:33

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: Need script or Program to count lines of source code

cat *.java | grep -vP '^\s*[\/\*]+' | sed '/^\s*$/d' | wc -l


NOTE: I haven't really tested that.

Last edited by fumbles (2009-03-10 12:36:42)

Offline

#3 2009-03-10 12:55:06

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Need script or Program to count lines of source code

Try sloccount

Last edited by rson451 (2009-03-10 12:56:03)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#4 2009-03-10 12:57:13

mitchell
Member
Registered: 2006-10-15
Posts: 18

Re: Need script or Program to count lines of source code

Offline

#5 2009-03-10 16:19:56

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: Need script or Program to count lines of source code

I wrote a quick python program to count the lines in a file, you could just adapt it to your needs, or use the second program I have in this post that condenses all the .java files in a directory into a single text file, which you can then use a line-count on (useful if you need to print it as well - I wrote it for my IB dossier java project), if you'd like a proper script for your needs, otherwise check the other suggestions.

Run it by copying the code into a text file and saying it as script.py (change script to whatever you want) and running it with python /path/to/script.py

#!/usr/bin/env python
#Program to tally the lines in a file
#Author: lswest
import os

home=os.path.expanduser("~")
endPath=raw_input("Path to file relative to your home directory (include file name and extension): ")
count=0
ff=open(os.path.join(home, endPath))
for x in ff:
    count+=1
values={'name': os.path.join(home,endPath), 'count' : count}
print "The file %(name)s contains %(count)s lines." % values
#!/usr/bin/env python
#Script to condense the multiple files of a project into one for easy printing/copying
#Author: lswest
import os

home=os.path.expanduser("~")
endPath=raw_input("Path relative to your home directory to the project folder: ")
extension=raw_input("Extension of files you want to condense: ")
outPath=raw_input("Path to output file relative to home directory: ")
outFile=raw_input("Output file name (including extension): ")
ff=open(os.path.join(home+outPath,outFile), "wt")

for root, dirs, files in os.walk(os.path.join(home,endPath), "true", "none", "true"):
    for infile in [f for f in files if f.endswith(extension)]:
        fh=open(os.path.abspath(os.path.join(root,infile)))
        for line in fh:
            ff.write(line,)
        fh.close()

ff.close()

Last edited by lswest (2009-03-10 16:23:52)


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#6 2009-03-10 19:44:16

markp1989
Member
Registered: 2008-10-05
Posts: 431

Re: Need script or Program to count lines of source code

cat FILE | wc -l

Desktop: E8400@4ghz - DFI Lanparty JR P45-T2RS - 4gb ddr2 800 - 30gb OCZ Vertex - Geforce 8800 GTS - 2*19" LCD
Server/Media Zotac GeForce 9300-ITX I-E - E5200 - 4gb Ram - 2* ecogreen F2 1.5tb - 1* wd green 500gb - PicoPSU 150xt - rtorrent - xbmc - ipazzport remote - 42" LCD

Offline

#7 2009-03-10 20:29:45

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Need script or Program to count lines of source code

Since excessive comments are only required for unclear code, shouldn't they be counted in LOC?

Offline

#8 2009-03-10 22:11:33

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Need script or Program to count lines of source code

sloc is what you want

Offline

#9 2009-03-11 01:10:09

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: Need script or Program to count lines of source code

I just love the way it computes the number of guys supposedly needed to wirte the source code, how much time they should have spent, and concludes by estimating the cost of development.

You got pretty impressive results by running sloc on linux kernel sources wink

Offline

Board footer

Powered by FluxBB