Code Libraries
Projects & Resources
|
Calculate the total number of lines of code within a directory tree
Keeping track of the total number of lines of source code is often important to big projects.
This code allows you to view the total number of lines written within a project.
You set the $start variable to your root directory for your project and the script will look at every file within that directory and it's subdirectories (without limit). It will then list each file, the total of lines within that file and a grand total of lines at the end.
<?php
$start = $_GET['t'];
if(empty($start)) { $start = '/home/public_html/'; }
// Tmp array of found sub dirs $subdir = array(0 => $start); $total = 0;
// Loop through the files for($i = 0; $i < count($subdir); $i++) {
// Open the folder $dir_handle = @opendir($subdir[$i]) or die('Unable to open ' . $subdir[$i]); $path = $subdir[$i];
while ($file = readdir($dir_handle)) { if($file == '.' || $file == '..') { continue; } else { if(is_dir($path . $file)) { //echo 'DIR: ' . $file . '<BR>'; array_push($subdir, ($path . $file . '/')); } else { $ext = explode('.', $file); if($ext[1] == 'php') { $sloc = count(file($path . $file)); $total = $total + $sloc; echo '(' . $sloc . ') ' . $path . $file . '<BR>'; unset($sloc); } } } } }
echo '<p><b>Total lines: ' . $total . '</a>';
// Close closedir($dir_handle);
?>
|
Developer Comments
|