1.匹配指定的关键字查找文件 2.匹配到文件后再执行相应的系统命令或生成批处理文件 3.反馈执行结果 4.示例:$phpfind.phpd:/photo.jpg,.gif,.pnggmconvert-resize200X200$filename$filename 5.示例解释:查找d:/photo下的所有文件夹及子文件夹jpg,gif,png格
1.匹配指定的关键字查找文件
<?php
/***
Name: find file
Example:
php find.php d:/photo .jpg,.gif,.png "gm convert -resize 100X100 $filename $filename"
php find.php .txt,.doc "copy $filename e:/document"
***/
// create bat file? 1:yes, 0:no
define('CREATE_BAT_FILE', 0);
switch(count($argv)) {
case 4:
$command = $argv[3];
$match = $argv[2];
$dir = $argv[1];
break;
case 3:
$command = $argv[2];
$match = $argv[1];
$dir = __DIR__;
break;
default:
exit('Error: Missing parameters!' . PHP_EOL .
'Example: path match command, d:/dir .jpg,.gif "echo $name"' . PHP_EOL);
}
if(!is_dir($dir)) {
exit($dir . ' not a directory.' . PHP_EOL);
}
$directory = array(str_replace('\', '/', $dir));
$files = array();
$index = 0;
$count = 0;
$result = '';
while($currentPath = current($directory)) {
$dirHandle = dir($currentPath);
while(false !== ($name = $dirHandle->read())) {
if($currentPath[strlen($currentPath) - 1] == '/') {
$filename = $currentPath . $name;
} else {
$filename = $currentPath . '/' . $name;
}
if($name == '..' || $name == '.') {
continue;
}
if(is_dir($filename)) {
$directory[] = $filename;
} else {
str_replace(explode(',', $match), '', $name, $count);
if($match != '*' && $count == 0) {
continue;
}
$template = array('$name', '$filename', '$path', '$index');
$variable = array($name, $filename, $currentPath, $index);
$cmd = str_replace($template, $variable, $command);
if(CREATE_BAT_FILE) {
$result .= $cmd . PHP_EOL;
} else {
echo shell_exec($cmd);
}
$index++;
}
}
next($directory);
}
// create bat file
if(CREATE_BAT_FILE) {
$batFile = fopen('temp.bat', 'w');
fwrite($batFile, $result);
fclose($batFile);
echo 'output file to: ' . str_replace('\', '/', __DIR__) . '/temp.bat' . PHP_EOL;
}
// echo result:
echo $index . ' file find.' . PHP_EOL;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号