我正在查找位于相同级别(没有子文件夹)的同一文件夹中的大约20.000到30.000个文件的字符串。
一种方法是使用PHP的file_get_contents
函数和strpos
结合使用,但我想知道是否可以按顺序使用控制台命令as unix users can do改善表现。
现在我正在使用它:
$path = 'C:/myPath/';
$string = 'mySearchString';
$dir = new DirectoryIterator($path);
$results = array();
foreach ($dir as $file){
if (!$file->isDot()){
$content = file_get_contents($file->getPathname());
if (strpos($content, $string) !== false) {
$results[] = $file;
}
}
}
我一直在看how to look for a string by using windows command lines,但我无法使用PHP。
答案 0 :(得分:0)
我通过使用Batch file(。bat)和使用Windows控制台命令找到了一种更快捷的方法。
我的.bat文件包含以下内容:
@echo off
pushd "%1"
findstr /m /C:%2 *
popd
其中%1
和%2
是PHP调用批处理文件中的参数:
$path = 'C:/my/path/';
$string = 'toSearch';
exec('cmd /c C:/path/myFile.bat '.$path.' "'.$string.'"', $result);
print_r($result);