Heim  >  Artikel  >  Backend-Entwicklung  >  批处理及PHP编写目录上的统计文件行数及个数

批处理及PHP编写目录上的统计文件行数及个数

WBOY
WBOYOriginal
2016-06-13 11:00:30873Durchsuche

批处理及PHP编写目录下的统计文件行数及个数

?? ??今天要统计下写的代码的行数(去除空行及注释)及目录下的文件总数。首先想到的是一个批处理命令,因为简单而且高效,于是去网上找了下批命令的教程,写了个简单的批命令处理:?

??????

@Rem: 本批命令用来计算某个路径下的总文件个数,及总行数。行数的计算不算空行@Rem: author: 肖肖 [email protected]@echo ****************************************************************@echo off@echo 使用说明:@echo 1、更改要计算的路径:将filePath的路径更改为自己的需要的路径。@echo 2、更改统计的文件后缀,如果需要统计所有文件则将fileExt设置为*,如果只需要统计php文件则将该变量指定为*.php@echo ****************************************************************@set fileExt=*.php@set filePath=D:\PHPAPP\phpwindframework\wind@setlocal enabledelayedexpansion@set filenum=0@set totalnum=0@for /r %filePath% %%i in (%fileExt%) do (	@set linenum=0 & @set /a filenum+=1 & @echo %%i & (@for /f "usebackq" %%b in (%%i) do @set /a linenum+=1) & @echo 行数:!linenum! & @set /a totalnum+=linenum)@echo 总行数: %totalnum%行@echo 总文件数: %filenum%@pause

?

?? ? ?具体的命令意思我就不解释了,特别说明下:

?? 我在计算每个文件的行数的时候,就是输出不了linenum,后来经过询问查找,原来是延迟的问题,于是在开头加了命令setlocal enabledelayedexpansion来开启延迟,同时输出采用@echo !linenum!的 这样!!的方式输出。

该批命令可以指定统计处我指定目录下的文件个数(可以指定统计的文件的类型),及总行数(去除了空行)。

但是对于代码统计来说,去除空行及注释的统计才是理想的状态。相对于批命令还是PHP来的给力,所以写了一个PHP版本的统计:

???

/** * @author xiaoxia xu <[email&#160;protected]> 2011-1-12 * @link http://www.phpwind.com * @copyright Copyright &copy; 2003-2110 phpwind.com * @license * 统计目录下的文件行数及总文件数··去除注释 */$fileExt = ".php";$filePath = "D:\PHPAPP\phpwindframework\wind";if (!is_dir($filePath)) exit('Path error!');list($totalnum, $linenum, $filenum) = readGiveDir($filePath, $fileExt);echo "*************************************************************\r\n";echo "总行数: " . $totalnum . "\r\n";echo "总有效行数: " . $linenum . "\r\n";echo '总文件个数:' . $filenum;function readGiveDir($dir, $fileExt) {	$totalnum = 0;	$linenum = 0;	$filenum = 0;	if ($dh = opendir($dir)) {        while (($file = readdir($dh)) !== false) {			if (in_array($file, array(".", "..", '.svn'))) continue;			if (is_dir($dir . '/' . $file)) {				list($num1, $num2, $num3) = readGiveDir($dir . '/' . $file, $fileExt);				$totalnum += $num1;				$linenum += $num2;				$filenum += $num3;			} else {				if (strrchr($file, '.') == $fileExt) {					list($num1, $num2) = readfiles($dir . '/' . $file);					$totalnum += $num1;					$linenum += $num2;					$filenum ++;					continue;				}			}        }        closedir($dh);    } else {		echo 'open dir <' . $dir . '> error!' . "\r";	}	return array($totalnum, $linenum, $filenum);}function readfiles($file) {	echo $file . "\r\n";    //$p = php_strip_whitespace($file);	$str = file($file);	$linenum = 0;	foreach ($str as $value) {		if (skip(trim($value))) continue;		$linenum ++;	}	$totalnum = count(file($file));	echo '行数:' . $totalnum . "\r\n";	echo '有效行数:' . $linenum . "\r\n";	return array($totalnum, $linenum);}function skip($string) {	if ($string == '') return true;	$array = array("*", "/*", "//", "#");	foreach ($array as $tag) {		if (strpos($string, $tag) === 0) return true;	}	return false;}

?

?

?? 代码也挺简单,这边就不做解释了,其中我处理跳过空行和注释行的是在skip这个函数中,这里声明了一个数组,用来声明我可以过滤的标签(可以看到这里都是PHP中的注释标签),我在查找的时候只要查找是否这些标签是在最前面的位置即可。但是这样会存在一个问题,当我的代码出现不规范的格式化的时候,可能出现代码中的*乘号进行了换行到了新的一行的开始位置,那也会被过滤掉。但总的来说··这两种统计都满足了我的需求。

Tips: PHP的该方法,放在命令行下运行效果更佳。~

?

?? 第一次觉得,当我遇到问题的时候,真的用自己的所学做了一些小便利的工具提供给自己使用。哈哈哈···纪念下~~~

?

?? 希望你有个好心情~~~

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php缓存种Nächster Artikel:php读取资料