How many lines of code do you have?

Written on 2 December 2011, 05:41pm

Tagged with:

Do you want to know how many lines of code you have in your application? Challenge accepted 🙂
Here’s a 10-minutes, 50-lines ColdFusion script that counts the number of lines in all the files from a given folder.

<cfsetting requesttimeout="300" showdebugoutput="no" />
<cfset initialDir = ExpandPath( '../' )>
<cfset files = 0> <!-- we will store the number of files that we traversed --->
<cfset lines = 0> <!--- this is what we need  --->
<cfset localRoot = "E:\your\input\folder" />
<cfset separator = '\'>
<cfset extensions = 'cfm,cfc,htm,html,css,js'> <!-- add here the list of extensions --->

<cfset tick = GetTickCount()> <!--- start the clock --->

<!--- get list of ALL files --->
<cfdirectory action="list" directory="#initialDir#" name="localQuery" recurse="true"/>

<cfoutput query="localQuery">
	<cfif type EQ 'File'>
		<cfset extension = LCase(listLast(name,".")) /> <!--- get the extension --->
		<cfset temp = ListFindNoCase(extensions, extension)> 
		<cfif temp IS NOT 0> <!--- if the file extension is one in our initial list --->
			<cfset path = directory & separator &  name>
			<cfset files = files + 1>
			<cfif FileExists(path)>
					<cffile action="read" file="#path#" variable="content" charset="utf-8">
				<cfcatch type="all">Error reading file</cfcatch>
				<cfset content = replace(content,Chr(10),Chr(13),"ALL")/> <!--- replace all LFs with CRs --->
				<cfset content = replace(content,Chr(13)&Chr(13),Chr(13),"ALL")/> <!--- replace all double CRs with single CRs --->
				...and here is most important part: 
				we remove ALL the other characters that are NOT CR (\r)
				we end up with a string containing ONLY the CRs
				we simply count the length of this string and then add 1
				<cfset thisFileLines = Len( REReplace( content, "[^\r]+", "", "ALL" ) ) />
				<cfset thisFileLines = thisFileLines + 1> 

				<cfset lines = lines + thisFileLines>
				File #path# has <strong>#thisFileLines#</strong> lines<br /> 
		</cfif> <!--- end if extension --->
	</cfif><!--- end if type file --->

<cfset tock = GetTickCount()> <!--- stop the clock --->
<cfset time = round((tock-tick)/1000)>

<cfoutput><hr />There are <strong>#lines#</strong> lines in #files# files. Search took #time# seconds.</cfoutput>

I got:

There are 101637 lines in 527 files. Search took 3 seconds.

See also

Comments (2)

  1. Hey There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful
    information. Thanks for the post. I will certainly return.


  2. magnetic energy — December 26, 2018 at 04:28

    Benefits of Magnetic Power Generator

    All worldwide shortage of power is a common problem and that is why folks are in search of some reliable and cheap electric
    source. Many folks are using magnetic generators as well as the reason behind this is that they get several
    benefits through them.
    This generator is quite easy to install in your house inside a limited place without disturbing the first
    settings of your dwelling. All you need is a little spare space to install this.
    This will help you to cut back your normal power bill.
    You can use this just as one energy supplier for many of the times
    and that means you don’t need to switch back to your old methods
    for power consumption. As soon as you start employing this source
    of energy within your house you will notice a noticeable saving in your income.

    It doesn’t produce any harmful gases that will damage environment.
    In this generator, magnets will generate energy that’s
    usable in different condition. The cost of having one
    of these power generator is not high at all. It is the cheapest way
    to obtain power generation nowadays in this era of technology.

    The main thing on this generator is a magnet which works and also
    a small wheel.
    As you know that it’s free of cost and there is no alternative party associated with installing this generator, so
    that you won’t need any maintenance from some expert. You can maintain it by yourself by cleaning it at regular intervals.
    This is the perfect thing to place in your house as a electrical source.
    It can support the energy sufficient to take care of all the appliances with the house.

    A family of 4 in order to six is extremely perfect for such a energy generation device.

    No harmful rays no complex electric motors are involved here so
    you shouldn’t bother about the protection of your family and friends.

    Unlike other free electric generation devices, it doesn’t need any solar technology or wind.
    It will work with its own without supply of external natural energy.
    Weather will not get a new performance or generation of one’s because
    of this magnetic generator. As it doesn’t rely on any external source so this will be an uninterrupted supply of power generation.

    Check more info on site :


Leave a response