- 论坛徽章:
- 0
|
由于WINDOWS下面没有类似unix下的find命令,在WIN下开发的朋友有时候需要按内容查找替换文件,特写这个小程序方便操作。
声明:本程序仅为兴趣参考,如有不当之处欢迎指正。对于可能存在的风险,本人不承担任何责任。
功能:按文件内容查找或替换文件,可指定按扩展名。
注意:不具有典型意义的字符替换可能会把不该替换的都替换掉,最好先查找确认。
<style>
.f1 {
width:50px;
}
</style>
<form action="" method=get>
<input type=hidden name="action" value="">
<span class="f1">查找:</span><input type="text" name="pattern" value="<? echo $_REQUEST['pattern']?stripslashes($_REQUEST['pattern']):'' ?>" size=50>
<br>
<span class="f1">替换为:</span><input type="text" name="replacement" value="<? echo $_REQUEST['replacement']?stripslashes($_REQUEST['replacement']):'' ?>" size=50>
<br>
<span class="f1">目录:</span><input type="text" name="directory" value="<? echo $_REQUEST['directory']?stripslashes($_REQUEST['directory']):'.' ?>" size=50>
<br>
<span class="f1">仅检查扩展名</span><input type="text" name="ext" value="<?=$_REQUEST['ext']?>"> (不要加句点,多个请用分号分隔)
<br>
<span class="f1">显示目录结构?</span><input type="checkbox" name="disp" value="1" <?=$_REQUEST['disp']?"checked":""?>>
<br>
<input type="button" value="查 找" onclick="this.form.action.value='find';this.form.submit();">
<input type="button" name="替 换" value="替 换" onclick="this.form.action.value='replace';this.form.submit();">
</form>
<hr>
<?php
/**
* function: Find by contents
* author: darkmoon
* site: http://www.phptop.net
* date: 2007-11-01
*/
function find( $path, $ext, $pattern = "", $disp = true )
{
static $result = array();
if( $disp ) {
echo "<br>\n$path";flush();
}
$handle = opendir( $path );
while( $item = readdir( $handle )) {
if( $item =='.' || $item == '..' ) {
continue;
}
//echo "<br>时间::".filectime($this->_path.'/'.$item);
if( is_dir( $path.'/'.$item )) {
find( $path.'/'.$item, $ext, $pattern, $disp );
}
else {
if( is_array( $ext ) && !empty( $ext )) {
$parts = explode( ".", basename( $item ));
if( !in_array( array_pop($parts), $ext )) {
continue;
}
}
$filename = $path.'/'.$item;
$contents = file_get_contents( $filename );
if( preg_match( $pattern, $contents )) {
//var_dump($pattern);die();
array_push( $result, $filename );
}
}
}
return $result;
}
function replace( $path, $ext, $pattern = "", $replacement = "", $disp = true )
{
static $result = array();
if( $disp ) {
echo "<br>\n$path";flush();
}
$handle = opendir( $path );
while( $item = readdir( $handle )) {
if( $item =='.' || $item == '..' ) {
continue;
}
if( is_dir( $path.'/'.$item )) {
replace( $path.'/'.$item, $ext, $pattern, $replacement, $disp );
}
else {
if( is_array( $ext ) && !empty( $ext )) {
$parts = explode( ".", basename( $item ));
if( !in_array( array_pop($parts), $ext )) {
continue;
}
}
$filename = $path.'/'.$item;
if( is_writable( $filename )) {
$contents = file_get_contents( $filename );
if( preg_match( $pattern, $contents )) {
file_put_contents( $filename, preg_replace( $pattern, $replacement, $contents ));
array_push( $result, $filename );
}
}
else {
echo "error: `$filename` can't be written";
}
}
}
return $result;
}
function getmicrotime() {
list($msec, $sec) = explode(" ",microtime());
$temp = getdate($sec);
$temp = bcadd((float)$msec, (float)$sec, 5 );
$temp = round($temp,2);
return $temp;
}
$count = 0;
$arr = array();
$t1 = getmicrotime();
echo "<style>\nbody,table,input { font-size:9pt;font-family:Verdana; }\nbody { background-color:buttonface;#c8c8c8;}\n</style>";
//var_dump( $_REQUEST);
if( !$directory = stripslashes( $_REQUEST['directory'] )) {
die( "invalid directory `{$_REQUEST['directory']}`" );
}
if( !$pattern = trim( stripslashes( $_REQUEST['pattern'] ))) {
die( "invalid pattern `{$_REQUEST['pattern']}`" );
}
if( $ext = trim($_REQUEST['ext'] )) {
$ext = explode( ";", $ext ); // array
}
$pattern = preg_replace( "`([()[\]{}.+*?^$])`", "\\\\\\1", $pattern );
$replacement = $_REQUEST['replacement'];
$disp = intval( $_REQUEST['disp'] );
switch( $_REQUEST['action'] ) {
case "find":
$result = find( $directory, $ext, "`".$pattern."`Uims", $disp );
break;
case "replace":
$result = replace( $directory, $ext, "`".$pattern."`Uims", $replacement, $disp );
break;
default:
break;
}
$t2 = getmicrotime();
echo "<h2>Result</h2>";
echo "<pre>";print_r($result);echo "</pre>";
echo "<br>process total <b>".count($result)."</b> files,this takes ".($t2-$t1)." s";
?>
[ 本帖最后由 powerpolly 于 2007-11-1 19:10 编辑 ] |
|