zoukankan      html  css  js  c++  java
  • 硬盘全文检索工具

    需要解决的几个问题:

    1、硬盘文件检索

    2、多线程

    3、word打开超时或者出现异常情况

    第一个问题,用的是递归方法,深度优先。据查资料,有个api命令"FindFirstFile"、"FindNextFile",可以更高效地列出所有目录,速度很快,没有进行深入研究

    第二个问题,使用的是manage,可以很方便地管理线程的执行情况,快速暂停、恢复

    第三个问题,采用com方式打开文档,只要电脑上可以打开,就没有问题啦。效率可能不是太高,暂时还没有找到更好的办法。打开的时候,给word命名一个guid的名称,然后传递给生成的一个监控线程,看执行了多少时间,如果超过指定时间,就通过api找到句柄,强行关闭word文档。

    其他的代码,就没有什么含量了。

    主界面代码

    import win.ui;
    import fsys;
    import string;
    import thread.manage;
    import thread.command;
    /*DSG{{*/
    mainForm = ..win.form(text="FindDisk";right=536;bottom=407;border="dialog frame";max=false;mode="popup")
    mainForm.add(
    button={cls="button";text="选择目录";left=441;top=19;right=512;bottom=43;z=5};
    button2={cls="button";text="开始";left=398;top=55;right=451;bottom=79;z=6};
    button3={cls="button";text="暂停";left=458;top=55;right=511;bottom=79;z=7};
    edit={cls="edit";text="关键字|关键字";left=73;top=57;right=390;bottom=80;edge=1;z=4};
    edit2={cls="edit";left=73;top=19;right=434;bottom=42;edge=1;z=9};
    groupbox={cls="groupbox";text="配置";left=12;top=1;right=524;bottom=92;edge=1;z=2};
    listview={cls="listview";left=12;top=98;right=524;bottom=389;bgcolor=16777215;edge=1;z=1};
    static={cls="static";text="关键字";left=28;top=61;right=69;bottom=79;transparent=1;z=3};
    static2={cls="static";text="目 录";left=28;top=23;right=69;bottom=41;transparent=1;z=8};
    static4={cls="static";left=9;top=391;right=530;bottom=407;font=LOGFONT( h=-10 );nWrap=1;transparent=1;z=10}
    )
    /*}}*/
    
    var sdir;
    var keyword;
    
    //创建线程管理器
    var manage = thread.manage()
    
    //获取待检索目录
    mainForm.button.oncommand = function(id,event){
        import fsys.dlg    
        mainForm.edit2.text = fsys.dlg.opendir()
    }
    
    
    //开始检索
    mainForm.button2.oncommand = function(id,event){
        sdir = mainForm.edit2.text;
        if( ! io.exist(sdir) ) 
        {
            mainForm.msgbox("请选择有效目录!")
            return ;
        }    
        mainForm.button2.disabled = true;
        keyword = mainForm.edit.text
        
        manage.create( 
            function(arg) {
                import thread.works;
                import myfun;
                
                var sdir = arg.sdir;
                var key = arg.key;
                var tcount = arg.tcount;
                
                myfun.search(sdir,key)
            },
            {sdir=sdir;key=keyword;}
        )
    
    }
    mainForm.button3.oncommand = function(id,event){
        if(mainForm.button3.text=="暂停"){
            mainForm.button3.text = "恢复"
            manage.suspend()
        }
        else{
            mainForm.button3.text = "暂停"
            manage.resume()
        }
    }
    mainForm.listview.insertColumn("文件路径",400,,)
    
    
    import mouse;
    import win.ui
    import win.ui.menu;
    import win.util.tray;
    import process;
    
    mainForm.popmenu = win.ui.popmenu(mainForm);//创建弹出菜单
    mainForm.popmenu.add('定位',function(id){
        //在下面输入菜单响应代码
        var index = mainForm.listview.selIndex;
        var path = mainForm.listview.getItemText(index,1)
        process.explore_select(path)
    });
    
    mainForm.listview.onnotify = function(id,code,ptr){    
        select(code) {
            case 0xFFFFFFFB/*_NM_RCLICK*/  {
                var x,y = mouse.getPos()
                mainForm.popmenu.popup(x,y,true);//弹出菜单
            }
        }
        
    }
    
    //消息响应函数
    var listener = thread.command()
    listener.print = function(...){
        mainForm.static4.text = ...;
    }
    listener.add = function(...){
        mainForm.listview.addItem({...})
    }
    listener.log = function(...){
        file = io.open("log.txt","a+");
        file.write(...+'
    ')
        file.close()
    }
    
    
    
    mainForm.show() 
    win.loopMessage(); 
    
    //安全终止线程
    manage.quitMessage()
    manage.waitClose() 

    myfun工具函数代码

    namespace myfun{
    
        //遍历目录开始
        search = function(folder,keyword){
            import fsys;
            import io;
            import thread.command
            import string;
            
            //目录不存在,就返回
            if( !io.exist(folder) ){
                return ;
            }
                    
            var exts = "txt|asp|jsp|php|xml|doc"  //支持的文件类型
            
            fsys.enum(folder, "*.*",
                function(dir,filename,fullpath,findData){ 
                    if(!filename){ 
                        search(fullpath,keyword)
                        thread.command.print("正在扫描目录:"+fullpath)
                        sleep(100)
                    }
                    else{
                        thread.command.print("正在处理文件:"+fullpath)
                        
                        var extindex = string.lastIndexAny(fullpath,".")
                        var ext = string.sub(fullpath,extindex+1);
                        ext = string.lower(ext)
                        
                        var content = null;
                        if( string.find(ext,"doc") ){
                            content = getdoccontent(fullpath)
                        }else if( checkexts(ext,exts) ){
                            content = gettxtcontent(fullpath)
                        }
                        
                        if( haskey(content,keyword) == true )
                        {
                            thread.command.add(fullpath);
                        }
                        sleep(100)
                    }
                } 
                ,false
            );
        }
        //遍历目录结束
        
        haskey = function(content,keys){
            if( content == null)
                return false;
            var tab = string.split(keys,"|")
            for(i=1;#tab;1){
                if( string.find(content,tab[i]) )
                    return true        
            }
            return false;        
        }
        
        
        
        //判断是否为支持的文件类型开始
        checkexts = function(ext,exts){
            import string;
            
            var tab = string.split(exts,"|")        
            for(i=1;#tab;1){
                if( string.find(ext,tab[i]) )
                {
                    return true;        
                }
            }
            return false;        
        }    
        //判断是否为支持的文件类型结束
        
        //获取文本格式内容开始
        gettxtcontent = function(path){
            import fsys.codepage
            
            if( !io.exist(path)    )
                return ""
            else
                return fsys.codepage.load(path);
        }
        //获取文本格式内容结束
        
        //获取word内容开始
        getdoccontent = function(...){
            import fsys;
            import thread;
            import thread.command;
            import win.guid;
            import string;
            import io;
            
            //获取com组件开始
            getobj = function(){
                import com
                
                var msobj = null;
                var wpsobj = null;
    
                try{
                    wpsobj = ..com.CreateObject("WPS.Application")
                }
                if(!wpsobj){
                    try{
                        msobj = com.CreateObject("Word.Application")
                    }
                }
                if( wpsobj)
                    return wpsobj,"wps";
                else if(msobj)
                    return msobj,"word";
                else{
                    return null,"";
                }
            }
            //获取com组件结束
            
            getcontent = function(path){
                var guid = win.guid.create()
                
                var filenameindex = string.lastIndexAny(path,"")
                var filename = string.sub(path,filenameindex);
                
                //不处理word临时文件
                if(string.match(filename,"^<~$>")){
                    return null;
                }
                            
                var doc,content = null;
                var obj,otype = getobj();
                            
                try{
                    if(obj){
                        obj.Visible = false
                        obj.DisplayAlerts = false
                        obj.Caption = tostring(guid)
                        
                        //监控doc运行时间开始
                        //如果文档打开时间超过 existtime,则强行关闭
                        thread.create(
                            function( ... ){
                                import win;         
                                import process;
                                import winex;
                                
                                var existtime = 5;
                                sleep( existtime *1000)
                                handle,tid,pid = winex.find(,...)
                                
                                if(handle)
                                    process(pid).kill();
                            },
                            guid
                        )
                        //监控运行时间开始                    
    
                        doc = obj.Documents.Open(path,false,true,false,"123")   //随便给个密码,以防部分带密码文档弹出输入框
                        if(!doc){
                            return null;
                        }
                        content = obj.ActiveDocument.Content.text;
                    }
                    else{                
                        thread.command.log("系统没有安装wps或者word,"+path+" 文件无法打开")
                    }
                    
                    if(doc)
                        doc.close()
                    if(obj)
                        obj.close()
                }
                catch(e)
                {
                    thread.command.log(path+" 文件打开异常")
                    return null;                
                }
                return content;
                
            }
            
            return  getcontent(...); 
        }    
        //获取word内容结束
    }
  • 相关阅读:
    performance benchmark : memcached vs Kyoto Tycoon
    Adhesive框架系列文章应用程序信息中心模块实现
    神奇的Redis
    使用代码测试ASP.NET MVC2执行流程
    linq2sql代码大全
    7/17博客园活动浅谈网站架构中缓存的应用所有资料
    浅谈.NET下的多线程和并行计算(十四)并行计算前言
    公司.NET 3.5培训资料分享
    mongodb分片集群(sharding with replica set)配置
    mongodb有关的研究
  • 原文地址:https://www.cnblogs.com/theluther/p/4216658.html
Copyright © 2011-2022 走看看