FileSearch

Post your working scripts, libraries and tools
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

FileSearch

16 May 2014, 00:46

FileSearch can search for filenames and file content.
It is possible to search with regex and filter by date and size.
You can choose the language (english, german) in tray menu.

You will need my library : Holles_Lib.ahk

And here is the script:

Code: Select all

SetWorkingDir,%A_ScriptDir%
#NoEnv
#Include Holles_Lib.ahk

IniRead,Language,FileSearch.ini,Options,Language,%A_Space%
if !Language
    Language:="English"
gosub %Language%

Menu,tray,Icon,shell32.dll,23,1
Menu,tray,NoStandard
Menu,tray,add,,MenuHandler
Menu,tray,add,%TextChangeLanguage%,MenuHandler
Menu,tray,add,%TextClose%,MainGuiClose

CreateGui:
{
    Gui,Main:Font,S12 cDefault,arial
    Gui,Main:add,Text,x10 y15 w60,%TextFolder%:
    IniRead,Folder,FileSearch.ini,Options,LastFolder,%A_Space%
    temp:=Folder?Folder:TextChooseFolder
    While(StrPixel(temp,"s12","Arial").width>294) ;reduce stringlength, if path is to long for the button
        temp:=StrLeft(temp,3) "..." StrTrimLeft(temp,a_index)
    Gui,Main:add,Button,x70 y10 w300 gSelectFolder vSelectFolder,%temp%
    Gui,Main:add,CheckBox,w300 Checked vSubFolder,%TextSubFolder%
    Gui,Main:add,Text,x10 y90 w60 vTextSearch,%TextSearch%
    Gui,Main:add,Edit,x70 yp-3 w300 gCheckValidStart vEditSearch
    Gui,Main:add,Text,x70 y120 w70 vTextSearchIn,%TextSearchIn%
    Gui,Main:add,Checkbox,x152 w110 yp Checked vCheckSearchName gCheckSearchName,%TextFileName%
    Gui,Main:add,Checkbox,x270 w110 yp Checked vCheckSearchContent gCheckSearchContent,%TextFileContent%
    Gui,Main:add,CheckBox,x70 y160 vCheckCS,a <> A
    Gui,Main:add,CheckBox,x152 y160 vCheckRegEx,%TextRegexSearch%
    Gui,Main:add,CheckBox,x70 y200 w140 gCheckFromCreated vCheckFromCreated,%TextFromCreated%
    Gui,Main:add,DateTime,x+1 yp-3 w120 Section Disabled vEditFromCreated
    Gui,Main:add,CheckBox,x70 y230 gCheckToCreated vCheckToCreated,%TextToCreated%
    Gui,Main:add,DateTime,xs ys+30 w120 Disabled vEditToCreated
    Gui,Main:add,CheckBox,x70 y270 w140 gCheckFromModified vCheckFromModified,%TextFromModified%
    Gui,Main:add,DateTime,x+1 yp-3 w120 Section Disabled vEditFromModified
    Gui,Main:add,CheckBox,x70 y300 gCheckToModified vCheckToModified,%TextToModified%
    Gui,Main:add,DateTime,xs ys+30 w120 Disabled vEditToModified
    Gui,Main:add,CheckBox,x70 y340 gCheckMin vCheckMin,%TextMinSize%
    Gui,Main:add,Edit,x+1 yp-3 w60 Section Disabled gEditMin vEditMin
    Gui,Main:add,Radio,x+5 y340 w35 vRadioMinByte Checked,B
    Gui,Main:add,Radio,xp+35 yp w45 vRadioMinKilo,KB
    Gui,Main:add,Radio,xp+45 yp vRadioMinMega,MB
    Gui,Main:add,CheckBox,x70 y370 gCheckMax vCheckMax,%TextMaxSize%
    Gui,Main:add,Edit,xs ys+30 w60 Disabled gEditMax vEditMax
    Gui,Main:add,Radio,x+5 y370 w35 vRadioMaxByte Checked,B
    Gui,Main:add,Radio,xp+35 yp w45 vRadioMaxKilo,KB
    Gui,Main:add,Radio,xp+45 yp vRadioMaxMega,MB  
    Gui,Main:add,Button,x10 w360 gStartSearch vStartSearch Default Disabled,%TextStartSearch%
    Gui,Main:show,w380,FileSearch
Return
}

StartSearch:
{
    SearchAbort:=False
    GuiControlGet,EditSearch,Main:
    GuiControlGet,SubFolder,Main:
    GuiControlGet,CheckSearchName,Main:
    GuiControlGet,CheckSearchContent,Main:
    GuiControlGet,CheckCS,Main:
    GuiControlGet,CheckRegEx,Main:
    GuiControlGet,CheckFromCreated,Main:
    GuiControlGet,CheckToCreated,Main:
    GuiControlGet,CheckFromModified,Main:
    GuiControlGet,CheckToModified,Main:
    GuiControlGet,CheckMin,Main:
    GuiControlGet,CheckMax,Main:
    if CheckFromCreated
        GuiControlGet,EditFromCreated,Main:
    if CheckToCreated
        GuiControlGet,EditToCreated,Main:
    if CheckFromModified
        GuiControlGet,EditFromModified,Main:
    if CheckToModified
        GuiControlGet,EditToModified,Main:
    if CheckMin {
        GuiControlGet,EditMin,Main:
        GuiControlGet,RadioMinKilo,Main:
        GuiControlGet,RadioMinMega,Main:
        if RadioMinKilo
            EditMin*=1024
        else if RadioMinMega
            EditMin*=1048576
    }
    if CheckMax {
        GuiControlGet,EditMax,Main:
        GuiControlGet,RadioMaxKilo,Main:
        GuiControlGet,RadioMaxMega,Main:
        if RadioMaxKilo
            EditMax*=1024
        else if RadioMaxMega
            EditMax*=1048576
    }
    Gui,Result:Destroy
    Gui,Result:font,S12 cDefault,arial
    Gui,Result:+ownerMain +AlwaysOnTop
    Gui,Result:add,Text,x10 y10 vTextFoundInName,%TextFoundInName%
    Gui,Result:add,ListBox,x10 yp+20 w600 r10 gListBox vLBName hwndhLBName
    Gui,Result:add,Text,x10 y+20 vTextFoundInContent,%TextFoundInContent%
    Gui,Result:add,ListBox,x10 yp+20 w600 r10 gListBox vLBContent hwndhLBContent
    Gui,Result:Font,S12 cDefault,arial
    Gui,Result:add,Text,x10 y+5 w600 vStatus
    Gui,Result:add,Button,x120 y+10 w100 vTextOpen,%TextOpen%
    Gui,Result:add,Button,x260 yp w100 Default vTextCancel,%TextCancel%
    Gui,Result:add,Button,x400 yp w100 vTextClose,%TextClose%
    Gui,Result:Font,S12 cTeal,arial
    SetTimer,SearchIsRunning,500
    Gui,Result:add,Text,x10 yp+5 w110 vRunning,%TextSearchIsRunning%
    Gui,Result:show
	OnMessage(0x200,"MouseOver") ;WM_MOUSEMOVE
    Folder:=StrRight(Folder,1)="\"?Folder:Folder "\"
    Loop,% Folder "*.*",,%SubFolder% ;check each file...
    {
        if SearchAbort
            break
        if (LastFolder!=A_LoopFileDir) {
            LastFolder:=A_LoopFileDir,temp:=A_LoopFileFullPath
            While(StrPixel(temp,"s12","Arial").width>600) ;reduce stringlength, if path is to long for the button
                temp:=StrLeft(temp,3) "..." StrTrimLeft(temp,a_index)
            GuiControl,Result:,Status,%temp%
        }
        if (CheckFromCreated&&(StrLeft(EditFromCreated,8)>StrLeft(A_LoopFileTimeCreated,8)))
        ||(CheckToCreated&&(StrLeft(A_LoopFileTimeCreated,8)>StrLeft(EditToCreated,8)))
        ||(if CheckFromModified&&(StrLeft(EditFromModified,8)>StrLeft(A_LoopFileTimeModified,8)))
        ||(CheckToModified&&(StrLeft(A_LoopFileTimeModified,8)>StrLeft(EditToModified,8)))
        ||(CheckMin&&(EditMin>A_LoopFileSize))
        ||(CheckMax&&(A_LoopFileSize>EditMax))
            continue
        if CheckSearchName { ;search in file name...
            HitNames:=CheckRegEx?RegExMatch(A_LoopFileName,CheckCS?EditSearch:"i)" EditSearch)?A_LoopFileFullPath:"":InStr(A_LoopFileName,EditSearch,CheckCS)?A_LoopFileFullPath:""
            if HitNames&&(HitNames!=HitNamesBefore)
                GuiControl,Result:,LBName,%HitNames%
            HitNamesBefore:=HitNames
        }
        if CheckSearchContent { ;search in file content
            file := FileOpen(A_LoopFileFullPath, "r")
            HitContent:=CheckRegEx?RegExMatch(file.Read(),CheckCS?EditSearch:"i)" EditSearch)?A_LoopFileFullPath:"":InStr(file.Read(),EditSearch,CheckCS)?A_LoopFileFullPath:""
            if HitContent&&(HitContent!=HitContentBefore) {
                GuiControl,Result:,LBContent,%HitContent%
                HitContentBefore:=HitContent
            }
            file.close()
        }
    }
    SetTimer,SearchIsRunning,off
    GuiControl,Result:,running
    if SearchAbort {
        Gui,Result:Font,S12 cRed,Arial
        GuiControl,Result:Font,Status
        GuiControl,Result:,Status,%TextSearchAborted%
    }
    else {
        Gui,Result:Font,S12 cGreen,Arial
        GuiControl,Result:Font,Status
        GuiControl,Result:,Status,%TextSearchComplete%
    }
return
}

SearchIsRunning:
{
    Points:=!Points||(Points>4)?1:Points+1
    GuiControl,Result:,running,% TextSearchIsRunning StringChain(".",Points)
return
}

SelectFolder:
{
    Gui +OwnDialogs
    IniRead,Folder,FileSearch.ini,Options,LastFolder,%A_Space%
    FileSelectFolder,Folder,*%Folder%,,%TextChooseFolder%
    if !Folder {
        GuiControl,Main:,SelectFolder,%TextChooseFolder%
        return
    }
    IniWrite,%Folder%,FileSearch.ini,Options,LastFolder
    GuiControl,Main:-Disabled,StartSearch
    temp:=Folder
    While(StrPixel(temp,"s12","Arial").width>294) ;reduce string length if path is too long for the button
        temp:=StrLeft(temp,3) "..." StrTrimLeft(temp,a_index)
    GuiControl,Main:,SelectFolder,%temp%
return
}

CheckFromCreated:
{
    GuiControlGet,CheckFromCreated,Main:
    if CheckFromCreated {
        GuiControl,Main:-Disabled,EditFromCreated
        GuiControl,Main:-Disabled,StartSearch
    }
    else {
        GuiControl,Main:Disable,EditFromCreated
        gosub CheckValidStart
    }
return
}

CheckToCreated:
{
    GuiControlGet,CheckToCreated,Main:
    if CheckToCreated {
        GuiControl,Main:-Disabled,EditToCreated
        GuiControl,Main:-Disabled,StartSearch
    }
    else {
        GuiControl,Main:Disable,EditToCreated
        gosub CheckValidStart
    }
return
}

CheckFromModified:
{
    GuiControlGet,CheckFromModified,Main:
    if CheckFromModified {
        GuiControl,Main:-Disabled,EditFromModified
        GuiControl,Main:-Disabled,StartSearch
    }
    else {
        GuiControl,Main:Disable,EditFromModified
        gosub CheckValidStart
    }
return
}

CheckToModified:
{
    GuiControlGet,CheckToModified,Main:
    if CheckToModified {
        GuiControl,Main:-Disabled,EditToModified
        GuiControl,Main:-Disabled,StartSearch
    }
    else {
        GuiControl,Main:Disable,EditToModified
        gosub CheckValidStart
    }
return
}

CheckMin:
{
    GuiControlGet,CheckMin,Main:
    GuiControlGet,EditMin,Main:
    if CheckMin
        GuiControl,Main:-Disabled,EditMin
    else
        GuiControl,Main:Disable,EditMin
    if CheckMin&&EditMin
        GuiControl,Main:-Disabled,StartSearch
    else
        gosub CheckValidStart
return
}

CheckMax:
{
    GuiControlGet,CheckMax,Main:
    GuiControlGet,EditMax,Main:
    if CheckMax
        GuiControl,Main:-Disabled,EditMax
    else
        GuiControl,Main:Disable,EditMax
    if CheckMax&&EditMax
        GuiControl,Main:-Disabled,StartSearch
    else
        gosub CheckValidStart
return
}

EditMin:
{
    GuiControlGet,CheckMin,Main:
    GuiControlGet,EditMin,Main:
    if CheckMin&&EditMin
        GuiControl,Main:-Disabled,StartSearch
    else
        gosub CheckValidStart
return
}

EditMax:
{
    GuiControlGet,CheckMax,Main:
    GuiControlGet,EditMax,Main:
    if CheckMax&&EditMax
        GuiControl,Main:-Disabled,StartSearch
    else
        gosub CheckValidStart
return
}

CheckValidStart:
{
    GuiControlGet,EditSearch,Main:
    GuiControlGet,CheckFromCreated,Main:
    GuiControlGet,CheckToCreated,Main:
    GuiControlGet,CheckFromModified,Main:
    GuiControlGet,CheckToModified,Main:
    GuiControlGet,CheckMin,Main:
    GuiControlGet,EditMin,Main:
    GuiControlGet,CheckMax,Main:
    GuiControlGet,EditMax,Main:
    if EditSearch||CheckFromCreated||CheckToCreated||CheckFromModified||CheckToModified||(CheckMin&&EditMin)||(CheckMax&&EditMax)
        GuiControl,Main:-Disabled,StartSearch
    else
        GuiControl,Main:Disable,StartSearch
return
}

CheckSearchName:
CheckSearchContent:
{
    GuiControlGet,CheckSearchName,Main:
    GuiControlGet,CheckSearchContent,Main:
    if !CheckSearchName&&!CheckSearchContent
        GuiControl,Main:,%A_GuiControl%,1
return
}

ResultGuiClose:
ResultButtonBeenden:
ResultButtonClose:
ResultButtonAbbrechen:
ResultButtonCancel:
{
    SearchAbort:=True
    If (A_GuiControl="TextCancel") {
        GuiControl,Result:Disable,TextCancel
        return
    }
	OnMessage(0x200,"") ;WM_MOUSEMOVE
	ToolTip	
    Gui,Result:Destroy
return
}

ResultButtonÖffnen:
ResultButtonOpen:
{
    Gui,Result:-AlwaysOnTop
    Gui,Result:+OwnDialogs  
    GuiControlGet,LBName,Result:
    GuiControlGet,LBContent,Result:
    if LBName  
        Run,%LBName%
    else if LBContent  
        Run,%LBContent%
return
}

ListBox:
{
    GuiControlGet,LBName,Result:
    GuiControlGet,LBContent,Result:
    if (A_GuiControl="LBName")&&LBName
        GuiControl,Choose,ListBox2,0
    else if (A_GuiControl="LBContent")&&LBContent
        GuiControl,Choose,ListBox1,0
	if (A_GuiEvent = "DoubleClick")
		gosub ResultButtonOpen
return
}

MenuHandler:
{
    Language:=A_ThisMenuItem="Change to English"?"English":"Deutsch"
    Menu,Tray,Rename,%A_ThisMenuItem%,% Language="English"?"Zu Deutsch wechseln":"Change to English"
    Menu,Tray,Rename,%TextClose%,% Language="English"?"Close":"Beenden"
    IniWrite,%Language%,FileSearch.ini,Options,Language
    gosub %Language%
return
}

Deutsch:
{
    TextClose:="Beenden"
    TextChangeLanguage:="Change to English"
    TextFolder:="Ordner"
    TextChooseFolder:="Bitte einen Ordner auswählen..."
    TextSubFolder:="Unterordner einbeziehen"
    TextSearch:="Suchen:"
    TextSearchIn:="Suche im:"
    TextFileName:="Datei-Name"
    TextFileContent:="Datei-Inhalt"
    TextRegexSearch:="RegEx-Suche"
    TextFromCreated:="Vom (Erstellt)"
    TextToCreated:="Bis   (Erstellt)"
    TextFromModified:="Vom (Modifiziert)"
    TextToModified:="Bis   (Modifiziert)"
    TextMinSize:="Min.  Größe"
    TextMaxSize:="Max. Größe"
    TextStartSearch:="Suche starten"
    TextFoundInName:="Gefunden in Dateinamen:"
    TextFoundInContent:="Gefunden im Datei-Inhalt:"
    TextOpen:="Öffnen"
    TextCancel:="Abbrechen"
    TextSearchIsRunning:="Suche läuft"
    TextSearchAborted:="Die Suche wurde abgebrochen!"
    TextSearchComplete:="Die Suche ist abgeschlossen!"
    gosub RefreshGui
return
}

English:
{
    TextClose:="Close"
    TextChangeLanguage:="Zu Deutsch wechseln"
    TextFolder:="Folder"
    TextChooseFolder:="Please choose a folder..."
    TextSubFolder:="Include subfolders"
    TextSearch:="Search:"
    TextSearchIn:="Search in:"
    TextFileName:="File name"
    TextFileContent:="File content"
    TextRegexSearch:="RegEx Search"
    TextFromCreated:="From (Created)"
    TextToCreated:="To     (Created)"
    TextFromModified:="From (Modified)"
    TextToModified:="To     (Modified)"
    TextMinSize:="Min.  size"
    TextMaxSize:="Max. size"
    TextStartSearch:="Start search!"
    TextFoundInName:="Found in file name:"
    TextFoundInContent:="Found in file content:"
    TextOpen:="Open"
    TextCancel:="Cancel"
    TextSearchIsRunning:="Searching"
    TextSearchAborted:="Search is aborted!"
    TextSearchComplete:="Search is complete!"
    gosub RefreshGui
return
}

RefreshGui:
{
    if !Folder
        GuiControl,Main:,SelectFolder,%TextChooseFolder%
    GuiControl,Main:,SubFolder,%TextSubFolder%
    GuiControl,Main:,TextSearch,%TextSearch%
    GuiControl,Main:,TextSearchIn,%TextSearchIn%
    GuiControl,Main:,CheckSearchName,%TextFileName%
    GuiControl,Main:,CheckSearchContent,%TextFileContent%
    GuiControl,Main:,CheckRegEx,%TextRegexSearch%
    GuiControl,Main:,CheckFromCreated,%TextFromCreated%
    GuiControl,Main:,CheckToCreated,%TextToCreated%
    GuiControl,Main:,CheckFromModified,%TextFromModified%
    GuiControl,Main:,CheckToModified,%TextToModified%
    GuiControl,Main:,CheckMin,%TextMinSize%
    GuiControl,Main:,CheckMax,%TextMaxSize%
    GuiControl,Main:,StartSearch,%TextStartSearch%
    GuiControlGet,Status,Result:
    If Status { ;Gui "Result" is open
        GuiControl,Result:,TextFoundInName,%TextFoundInName%
        GuiControl,Result:,TextFoundInContent,%TextFoundInContent%
        GuiControl,Result:,TextOpen,%TextOpen%
        GuiControl,Result:,TextCancel,%TextCancel%
        GuiControl,Result:,TextClose,%TextClose%
        if SearchAbort
            GuiControl,Result:,Status,%TextSearchAborted%
        else if !IsIn(SearchIsRunning,GetTimer().names) ;timer is not active... search is complete!
            GuiControl,Result:,Status,%TextSearchComplete%
    }
return
}

MainGuiClose:
ExitApp

MouseOver() {
	global hLBContent,hLBName
	static TextBefore
	MouseGetPos,,ypos
	if (A_GuiControl = "LBName") {
		StartRow:=LB_GETTOPINDEX(hLBName) 
		GuiControlGet,LBName,Pos
		yPos-=26+LBNameY
		ControlGet,Hits,List,,ListBox1,A		
	}
	else if (A_GuiControl = "LBContent") {
		StartRow:=LB_GETTOPINDEX(hLBContent)
		GuiControlGet,LBContent,Pos
		yPos-=26+LBContentY	
		ControlGet,Hits,List,,ListBox2,A
	}
	else {
		TextBefore:=""
		ToolTip
		return
	}
	Row:=Ceil(yPos/18)+StartRow-1
	StringSplit,Text,Hits,`n
	Text:=Text%Row%
	if (Text!=TextBefore)
		ToolTip %Text%
	TextBefore:=Text
return
}

LB_GETTOPINDEX(HWND) { ;by hd0202
   ; LB_GETTOPINDEX = 0x018E -> msdn.microsoft.com/en-us/library/bb761317(v=vs.85).aspx
   Return (DllCall("User32.dll\SendMessage", "Ptr", HWND, "UInt", 0x018E, "Ptr", 0, "Ptr", 0, "Ptr") + 1)
}

Pause::Pause
Last change: Tooltip (by mouse over) in the search results added. Open a file in the search result by double click (same like click on the button "open").
Last edited by Holle on 04 Jun 2014, 17:10, edited 4 times in total.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: FileSearch

16 May 2014, 01:19

minor edits to comments:

;reduce stringlength, if path is to long for the button -> reduce string length if path is too long for the button
; timer is not active ...also search is complete -> timer is not active... search is complete!
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: FileSearch

16 May 2014, 01:48

thanks, it´s changed now.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: FileSearch

21 May 2014, 17:40

this is great but the search results are not fully visible or readable in the "Found in" box, specially when they have long paths. is there anyway to modify the code so that search results are fully visible regardless of the length of their paths?
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: FileSearch

23 May 2014, 09:03

Thank you for report.
I think there 2 ways to show full path...
1.) Maximize the window and if that is not enough than scale the font size.
2.) A "mouse over" , that shows the full path in a tooltip.

What do you think? Option 1 oder option 2 ?
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: FileSearch

23 May 2014, 10:39

thanks for the reply. i think {A "mouse over" that shows the full path in a tooltip} would be the simplest and best option for now. i look forward to the implementation.
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: FileSearch

04 Jun 2014, 16:55

It´s done...
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: FileSearch

04 Jun 2014, 19:10

thanks, well done! tested and works great. specially the added mouse-over tooltips!
Best-Code-in-Use
Posts: 139
Joined: 01 Dec 2015, 05:13
Location: Germany

Re: FileSearch German

09 Feb 2016, 03:11

Hello Holle,

thanks for the Script.
In German it is better, when You not use: ö, ä, ß, or somthing else...
And i changed "Modifiziert" to "Bearbeitet", that sounds a little bit more familiar for me...

Here are the changes that I have made... You can use it, if you like it... ;) ;-)

Code: Select all

Deutsch:
{
    TextClose:="Beenden"
    TextChangeLanguage:="Change to English"
    TextFolder:="Ordner"
    TextChooseFolder:="Bitte einen Ordner auswaehlen..."
    TextSubFolder:="Unterordner einbeziehen"
    TextSearch:="Suchen:"
    TextSearchIn:="Suche im:"
    TextFileName:="Datei-Name"
    TextFileContent:="Datei-Inhalt"
    TextRegexSearch:="RegEx-Suche"
    TextFromCreated:="Vom (Erstellt)"
    TextToCreated:="Bis   (Erstellt)"
    TextFromModified:="Vom (Bearbeitet)"
    TextToModified:="Bis   (Bearbeitet)"
    TextMinSize:="Min. Groesse"
    TextMaxSize:="Max. Groesse"
    TextStartSearch:="Suche starten"
    TextFoundInName:="Gefunden in Dateinamen:"
    TextFoundInContent:="Gefunden im Datei-Inhalt:"
    TextOpen:="Oeffnen"
    TextCancel:="Abbrechen"
    TextSearchIsRunning:="Suche laeuft"
    TextSearchAborted:="Die Suche wurde abgebrochen!"
    TextSearchComplete:="Die Suche ist abgeschlossen!"
    gosub RefreshGui
return
}
Greetings Best-Code-in-Use

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot] and 23 guests