Simple file search

You can post your code snippets here for others to use and learn from
Post Reply
User avatar
Redlion
Posts: 94
Joined: Sun Jul 10, 2011 5:05 am
Location: Perth, Western Australia

Simple file search

Post by Redlion »

Hi Everyone,

I found an other small program that may interest someone.

It is a very simple file search program, it finds all the devices connected, and lists them.

Just enter a file name or part name select the devices and press "Search"

It will them display all the matches it found.

Code: Select all

@VERSION 6,0
@REQUIRE "MUIRoyale"
@APPTITLE "QSearch"
@APPVERSION "$VER: 1.0 (01.03.15)"
@APPCOPYRIGHT "©2015, George Leo den Hollander"
@APPAUTHOR "Leo den Hollander"
@APPDESCRIPTION "Directory Search Program"

@DISPLAY {Hidden = True}

MUI.CreateGui([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application base="DidL">
<window title="Q-Search - Directory Search Program" id="window" width="500" notify="closerequest" >
    <vgroup background="pre_ShadowFill">
        <hgroup>
             <colgroup columns="3" frame="group" frametitle="Search for File">
                 <label> File : </label>
                 <string id="Mystring" contents="" />
                 <button id="SearchOK" weight="25" notify="pressed">SEARCH</button>
             </colgroup>
        </hgroup>
        <vgroup>
            <hgroup>
                <colgroup columns="1"  frame="group" frametitle="Drives" weight="20">
                    <listview Id="Vols" ScrollerPos="None">
                    <column/>
                    </listview>
                </colgroup>
                <colgroup columns="1" frame="group" frametitle="Matching Files">
                    <listview Id="Found">
                    <column/>
                    </listview>
                </colgroup>
            </hgroup>
        </vgroup>
        <hgroup>
            <vgroup>
                <hgroup>
                    <label>Searching :</label>
                    <string id="Directories" contents=""/>
                </hgroup>
                <hgroup>
                    <label>No. Found :</label>
                    <string id="Searching" contents=""/>
                </hgroup>
            </vgroup>
        </hgroup>
    </vgroup>
</window>
</application>
]])

devices = {}
harddisks = {}
optical = {}
usb = {}
Devs = {}

Function p_ListDrives() ; ---------------------------------------------------------------------------
    Execute("info devices sort volume >TEXTCLIP:")
    type, data = GetClipboard()
    h = 0
    o = 0
    u = 0
    devices, NoofDevices = SplitStr(data,"\n")
    For Local x = 1 to NoofDevices-1
        line = devices[x-1]
        If FindStr(line,"DH",False) = 0
            harddisks[h] = MidStr(line,0, FindStr(line,":"))
            h = h +1
        ElseIf FindStr(line,"CD",False) = 0
            optical[o] = MidStr(line,0, FindStr(line,":"))
            o = o +1
        ElseIf FindStr(line,"USB",False) = 0
            usb[u] = MidStr(line,0, FindStr(line,":"))
            u = u +1
        EndIf
    Next
    L=0
    For Local d = 0 to h-1
        MUI.DoMethod("Vols","Insert","Bottom",Harddisks[d]..":")
    Next
    For Local d = 0 to o-1
        MUI.DoMethod("Vols","Insert","Bottom",optical[d]..":")
    Next
    For Local d = 0 to u-1
        MUI.DoMethod("Vols","Insert","Bottom",USB[d]..":")
    Next
EndFunction

Function p_search(SearchDir$) ; * Search Drive *************************************************************
    Local dirno
    Local Lookdir$
    LookDir$ =  SearchDir$
    Dirno = OpenDirectory(NIL, LookDir$)
    MUI.set("Directories","Contents",LookDir$)
    Local Entry
    Entry = NextDirectoryEntry(Dirno)
    While Entry <> Nil
        If Entry.type = #DOSTYPE_FILE
            If FindStr(Entry.name, Search$, False) > -1
                MUI.DoMethod("Found", "Insert", "Bottom",LookDir$..Entry.name)
                SN = SN + 1
                MUI.Set("Searching", "Contents"," ".. SN.." files ...")
            EndIf
        EndIf
        If Entry.type = #DOSTYPE_DIRECTORY
            LookDir$ = LookDir$..Entry.name.."/"
            p_search(Lookdir$)
            LookDir$ = SearchDir$
        EndIf
        Entry = NextDirectoryEntry(dirno)
    Wend
    CloseDirectory(Dirno)
Endfunction

Function p_MUIEvent(id) ; * Joint event handler for MUI buttons and menus ***********************************
    Switch id
        Case "SearchOK"
            SN = 0
            MUI.DoMethod("Found","Clear")
            MUI.Set("Searching", "Contents","") 
            Pos = MUI.get("Vols","Active")
            SearchFrom$ = MUI.DoMethod("Vols","GetEntry",Pos)
            Search$ = MUI.Get("MyString","Contents")
            p_Search(SearchFrom$)
            MUI.Set("Searching","Contents"," "..SN.." files - Finished.")
     
        Case "quit":
            End
    EndSwitch
EndFunction

Function p_EventFunc(msg) ; ================================================================================
    Switch msg.action
        Case "MUIRoyale":
            Switch msg.attribute
                Case "CloseRequest":
                     End
        
                Case "Pressed":
                    p_MUIEvent(msg.id)
        
            Endswitch

        Case "HideWindow":
            MUI.Set("app", "iconified", True)

        Case "ShowWindow":
            MUI.Set("app", "iconified", False)

    EndSwitch
EndFunction

; listen to these events!
InstallEventHandler({MUIRoyale = p_EventFunc, HideWindow = p_EventFunc, ShowWindow = p_EventFunc})

p_ListDrives()
MUI.set("Vols","Active",0)
SearchFrom$ = MUI.DoMethod("Vols","GetEntry",0)

; main loop!
Repeat
    WaitEvent
Forever
Copy it, run it, mess with the code. As I said before, if you improve it post your version here, I'm always looking for better or different ways of do things.

Enjoy, Leo
----------------------------------------------------------------------------------------
Redlion
Sam460 Lite
A4000 A3000 A2000 A1200 A1000 A600 A500 CD32
Post Reply