免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3921 | 回复: 5
打印 上一主题 下一主题

[小工具] WIN下面按内容查找替换文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-01 19:05 |只看该作者 |倒序浏览
由于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 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-11-01 19:06 |只看该作者
以上程序已调试通过,欢迎大家测试

论坛徽章:
3
金牛座
日期:2013-10-12 15:42:452015年辞旧岁徽章
日期:2015-03-03 16:54:15IT运维版块每日发帖之星
日期:2016-06-01 06:20:00
3 [报告]
发表于 2007-11-01 19:55 |只看该作者
支持一下!

论坛徽章:
0
4 [报告]
发表于 2007-11-02 10:35 |只看该作者
做成这样的HTML嵌入PHP代码,还不如直接写一个PHP代码文件从console执行,效率和输出都更近人意

论坛徽章:
0
5 [报告]
发表于 2007-11-02 11:37 |只看该作者
原帖由 一颗小白菜 于 2007-11-2 10:35 发表
做成这样的HTML嵌入PHP代码,还不如直接写一个PHP代码文件从console执行,效率和输出都更近人意


谢谢,这个建议不错~

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
6 [报告]
发表于 2007-11-04 21:59 |只看该作者
友情提示,不如用find的portable for win32版本。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP