Hard disk full-text Retrieval tool

Source: Internet
Author: User
Tags response code

Several issues to be addressed:

1, hard disk file retrieval

2. Multithreading

3. Word open time-out or unexpected condition

The first problem, using recursive methods, depth first. According to the data, there is an API command "FindFirstFile", "FindNextFile", you can more efficiently list all directories, fast, no in-depth study

The second problem, using manage, makes it easy to manage the execution of a thread, quickly pause, resume

The third problem, the use of COM mode to open the document, as long as the computer can open, there is no problem. The efficiency may not be too high, there is no better way to find out for the time being. When open, give word a name for the GUID, then pass it to the generated monitoring thread, see how much time has been executed, and, if it exceeds the specified time, find a handle through the API and forcibly close the Word document.

The other code, there is no content.

Main interface Code

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= "select Directory"; left=441;top=19;right=512;bottom=43;z=5};button2={cls= "button"; text= "Start"; left=398;top=55;right=451;bottom=79;z=6};button3={cls= "button"; text= "pause"; left=458;top=55;right=511;bottom=79;z=7};edit={cls= "edit"; text= "keywords | keywords"; 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= "Configuration"; 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= "keywords"; left=28;top=61;right=69;bottom=79;transparent=1;z=3};static2={cls= "Static"; text= "directory"; 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})/*}}*/varSdir;varkeyword;//Create thread ManagervarManage =thread.manage ()//get directory to retrieveMainForm.button.oncommand =function(id,event) {Import Fsys.dlg mainForm.edit2.text=fsys.dlg.opendir ()}//Start SearchMainForm.button2.oncommand =function(id,event) {Sdir=MainForm.edit2.text; if( !io.exist (sdir)) {Mainform.msgbox ("Please select a valid directory!" ")        return ; } mainForm.button2.disabled=true; Keyword=MainForm.edit.text manage.create (function(ARG) {import thread.works;                        Import Myfun; varSdir =Arg.sdir; varKey =Arg.key; varTcount =Arg.tcount; Myfun.search (Sdir,key)}, {Sdir=sdir;key=keyword;} )}mainform.button3.oncommand=function(id,event) {if(mainform.button3.text== "Pause") {MainForm.button3.text= "Recovery"manage.suspend ()}Else{MainForm.button3.text= "Paused"Manage.resume ()}} MainForm.listview.insertColumn ("File path", 400,,) import mouse;import win.uiimport win.ui.menu;import win.util.tray;import process;mainform.popmenu= Win.ui.popmenu (mainform);//Create pop-up menuMainForm.popmenu.add (' positioning ',function(ID) {//Enter the menu response code below    varindex =MainForm.listview.selIndex; varPath = MainForm.listview.getItemText (index,1) Process.explore_select (Path)}); MainForm.listview.onnotify=function(id,code,ptr) {Select (code) { Case0xFFFFFFFB/*_nm_rclick*/  {            varX, y =Mouse.getpos () mainForm.popmenu.popup (x, Y,true);//pop-up menu        }    }    }//Message response functionvarListener =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 ( ...+ ' \ r \ n ') File.close ()}mainform.show () win.loopmessage (); //Secure Terminate threadmanage.quitmessage () manage.waitclose ( )

Myfun Tool function code

namespace myfun{//Traverse Directory StartSearch =function(Folder,keyword) {import Fsys;        Import io;                Import Thread.command Import string; //The directory does not exist, it returns        if( !io.exist (folder)) {            return ; }                        varexts = "Txt|asp|jsp|php|xml|doc"//Supported file Typesfsys.enum (folder,"*.*",            function(dir,filename,fullpath,finddata) {if(!filename) {Search (Fullpath,keyword) thread.command.print ("Scanning directory:" +fullpath) Sleep (100)                }                Else{thread.command.print ("Processing file:" +FullPath)varExtindex = String.lastindexany (FullPath, "."))                    varext = string.sub (fullpath,extindex+1); Ext=string.lower (EXT)varContent =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        ); }    //Traverse Directory EndHaskey=function(Content,keys) {if(Content = =NULL)            return false; vartab = String.Split (keys, "|")         for(I=1; #tab; 1){            if(String.find (Content,tab[i]))return true                }        return false; }                //determine if a supported file type startsCheckexts =function(ext,exts) {import string; vartab = String.Split (exts, "|")                 for(I=1; #tab; 1){            if(String.find (Ext,tab[i])) {return true; }        }        return false; }        //to determine whether the supported file types end        //get text Format content startGettxtcontent =function(path) {Import Fsys.codepageif( !io.exist (path))return""Else            returnfsys.codepage.load (path); }    //get text Format content End        //get Word content startGetdoccontent =function(...)        {Import Fsys;        Import thread;        Import Thread.command;        Import Win.guid;        Import string;                Import io; //get COM component startGetobj =function() {Import COMvarMsobj =NULL; varWpsobj =NULL; Try{wpsobj=: Com.createobject ("WPS. Application ")            }            if(!wpsobj) {                Try{msobj= com. CreateObject ("Word.Application")                }            }            if(wpsobj)returnWpsobj, "WPS"; Else if(msobj)returnMsobj, "word"; Else{                return NULL,""; }        }        //get COM Component Endgetcontent=function(path) {varGUID =win.guid.create ()varFilenameindex = String.lastindexany (Path, "\ ") var filename = string.sub (path,filenameindex); Do not handle Word temp file 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)//monitor Doc Run time start//If the document is open longer than existtime, strong Line off 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)//monitor run time start doc = obj. Documents.Open (Path,false,true,false,"123")//Give a password, in case the part with the Password document popup input box if (!doc) {return null; Content = obj.                ActiveDocument.Content.text; } else{Thread.command.log ("The system is not installed with WPS or Word," +path+ "file cannot be opened")} if (DOC) Doc.close () if (obj) Obj.close ()} catch (e) {Thread.command.log (path+"File Open Exception") return null;                    } return content;     } return GetContent (...); }//Get Word end of content}

Hard disk full-text Retrieval tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.