批处理及PHP编写目录上的统计文件行数及个数
批处理及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版本的统计:
??? ? ? ?? 代码也挺简单,这边就不做解释了,其中我处理跳过空行和注释行的是在skip这个函数中,这里声明了一个数组,用来声明我可以过滤的标签(可以看到这里都是PHP中的注释标签),我在查找的时候只要查找是否这些标签是在最前面的位置即可。但是这样会存在一个问题,当我的代码出现不规范的格式化的时候,可能出现代码中的*乘号进行了换行到了新的一行的开始位置,那也会被过滤掉。但总的来说··这两种统计都满足了我的需求。 Tips: PHP的该方法,放在命令行下运行效果更佳。~ ? ?? 第一次觉得,当我遇到问题的时候,真的用自己的所学做了一些小便利的工具提供给自己使用。哈哈哈···纪念下~~~ ? ?? 希望你有个好心情~~~/** * @author xiaoxia xu <[email protected]> 2011-1-12 * @link http://www.phpwind.com * @copyright Copyright © 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;}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast
