Jump to content

Search Function help


mkmcst
 Share

Recommended Posts

Hi all i got this nice script off the forum its a nice and fast Search Function very nice script

i'm trying to use it in my own script i'm trying to rid its GUI so i can use it in just a script with no gui but everytime i try to take its gui out seems it wont work for me i'm not understanding how to use the search part without the gui can this be done?

#include <GuiConstantsEx.au3>
#include <ListViewConstants.au3>

$GUI = GUICreate("Files Search Demo!", 500, 420)

GUICtrlCreateLabel("Path to search:", 20, 5, -1, 15)
$Path_Input = GUICtrlCreateInput("C:\", 20, 20, 460, 20)

GUICtrlCreateLabel("Search request:", 20, 55, -1, 15)
$Request_Input = GUICtrlCreateInput("*.txt", 20, 70, 460, 20)

$Search_Button = GUICtrlCreateButton("Search", 20, 100, 60, 20)
$ListView = GUICtrlCreateListView("Results", 20, 130, 460, 250)

$SearhInfo_Label = GUICtrlCreateLabel("", 110, 105, 370)
$SearhStatus_Label = GUICtrlCreateLabel("", 20, 383, 450, 40)

GUISetState(@SW_SHOW, $GUI)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Search_Button
            $sPath = GUICtrlRead($Path_Input)
            $sRequest = GUICtrlRead($Request_Input)
            
            GUICtrlSendMsg($ListView, $LVM_DELETEALLITEMS, 0, 0)
            GUICtrlSetData($SearhInfo_Label, "Please wait, seaching...")
            
            $aResults = _FileSearch($sPath, $sRequest, 1, 0, $SearhStatus_Label)
            
            If @error Then
                MsgBox(48, "Attention!", "No files found.", 0, $GUI)
            Else
                For $i = 1 To UBound($aResults)-1
                    GUICtrlCreateListViewItem($aResults[$i], $ListView)
                Next
                
                GUICtrlSendMsg($ListView, $LVM_SETCOLUMNWIDTH, 0, -1)
            EndIf
            
            GUICtrlSetData($SearhStatus_Label, "")
            GUICtrlSetData($SearhInfo_Label, StringFormat("Done: %i files found.", UBound($aResults)-1))
    EndSwitch
WEnd

;================================================================================
;Flag = 1 search with recurse
;Flag <> 1 search without recurse
;
;$iRet = 1 return parent directory of the file path
;$iRet <> 1 return full file path
;
;$iStatus_CtrlID - If this is an id of the label control, the function will set the current status of the search
;
;On Failure set @error as following:
;   1 - $sPath is not a dir or it not exists (in this case returned -1).
;   2 - $sPath is empty dir.
;   3 - No files found in $sPath dir.
;
;On Seccess return an array with found files.
;================================================================================
Func _FileSearch($sPath, $sMask, $iFlag=0, $iRet=0, $iStatus_CtrlID=0)
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, -1)
    
    Local $aRetPathArr[1], $sFindNextFile, $sCurrentPath, $aSubDirFindArr
    If StringInStr($sMask, "*") Then $sMask = StringReplace($sMask, "*.", "")
    
    $sPath = StringRegExpReplace($sPath, '\\+ *$', '\')
    
    Local $hSearch = FileFindFirstFile($sPath & "\*.*")
    If @error = 1 Then Return SetError(2, 0, 0)
    If $hSearch = -1 Then Return SetError(3, 0, 0)
    
    While 1
        $sFindNextFile = FileFindNextFile($hSearch)
        If @error = 1 Then ExitLoop
        
        If $iStatus_CtrlID Then _GUICtrlSetData($iStatus_CtrlID, $sPath)
        
        $sCurrentPath = $sPath & "\" & $sFindNextFile
        
        If $iFlag = 1 And StringInStr(FileGetAttrib($sCurrentPath), "D") Then
            $aSubDirFindArr = _FileSearch($sCurrentPath, $sMask, $iFlag, 0, $iStatus_CtrlID)
            If @error Then ContinueLoop
            
            For $i = 1 To $aSubDirFindArr[0]
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]
                
                $aRetPathArr[$aRetPathArr[0]] = $aSubDirFindArr[$i]
                If $iRet = 1 Then _
                    $aRetPathArr[$aRetPathArr[0]] = StringRegExpReplace($aRetPathArr[$aRetPathArr[0]], "\\[^\\]*$", "")
                
                If $iStatus_CtrlID Then _GUICtrlSetData($iStatus_CtrlID, $aSubDirFindArr[$i])
            Next
        Else
            If $sMask = "*" Or $sFindNextFile = $sMask Or StringRegExpReplace($sCurrentPath, '^.*\.', '') = $sMask Then
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]
                
                $aRetPathArr[$aRetPathArr[0]] = $sCurrentPath
                If $iRet = 1 Then $aRetPathArr[$aRetPathArr[0]] = $sPath
            EndIf
        EndIf
    WEnd
    
    FileClose($hSearch)
    
    If $aRetPathArr[0] = 0 Then Return SetError(3, 0, 0)
    
    Return $aRetPathArr
EndFunc

Func _GUICtrlSetData($iCtrlID, $sData)
    If GUICtrlRead($iCtrlID) <> $sData Then GUICtrlSetData($iCtrlID, $sData)
EndFunc[/quote]
Edited by mkmcst
Link to comment
Share on other sites

Hi all i got this nice script off the forum its a nice and fast Search Function very nice script

i'm trying to use it in my own script i'm trying to rid its GUI so i can use it in just a script with no gui but everytime i try to take its gui out seems it wont work for me i'm not understanding how to use the search part without the gui can this be done?

Hi,

next time please use

 
tags for posting code.

Is this, what you mean:

#include <array.au3>

$spath = "c:\temp"
$sRequest = "*.txt"

$aResults = _FileSearch($sPath, $sRequest, 1, 0)
If @error Then 
    MsgBox(48, "Attention!", "No files found.")
Else
    _ArrayDisplay ($aResults)
EndIf
;================================================================================
;Flag = 1 search with recurse
;Flag <> 1 search without recurse
;
;$iRet = 1 return parent directory of the file path
;$iRet <> 1 return full file path
;
;$iStatus_CtrlID - If this is an id of the label control, the function will set the current status of the search
;
;On Failure set @error as following:
; 1 - $sPath is not a dir or it not exists (in this case returned -1).
; 2 - $sPath is empty dir.
; 3 - No files found in $sPath dir.
;
;On Seccess return an array with found files.
;================================================================================
Func _FileSearch($sPath, $sMask, $iFlag=0, $iRet=0, $iStatus_CtrlID=0)
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, -1)

    Local $aRetPathArr[1], $sFindNextFile, $sCurrentPath, $aSubDirFindArr
    If StringInStr($sMask, "*") Then $sMask = StringReplace($sMask, "*.", "")

    $sPath = StringRegExpReplace($sPath, '\\+ *$', '\')

    Local $hSearch = FileFindFirstFile($sPath & "\*.*")
    If @error = 1 Then Return SetError(2, 0, 0)
    If $hSearch = -1 Then Return SetError(3, 0, 0)

    While 1
        $sFindNextFile = FileFindNextFile($hSearch)
        If @error = 1 Then ExitLoop

        $sCurrentPath = $sPath & "\" & $sFindNextFile

        If $iFlag = 1 And StringInStr(FileGetAttrib($sCurrentPath), "D") Then
            $aSubDirFindArr = _FileSearch($sCurrentPath, $sMask, $iFlag, 0, $iStatus_CtrlID)
            If @error Then ContinueLoop

            For $i = 1 To $aSubDirFindArr[0]
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]

                $aRetPathArr[$aRetPathArr[0]] = $aSubDirFindArr[$i]
                If $iRet = 1 Then _
                $aRetPathArr[$aRetPathArr[0]] = StringRegExpReplace($aRetPathArr[$aRetPathArr[0]], "\\[^\\]*$", "")
            Next
        Else
            If $sMask = "*" Or $sFindNextFile = $sMask Or StringRegExpReplace($sCurrentPath, '^.*\.', '') = $sMask Then
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]

                $aRetPathArr[$aRetPathArr[0]] = $sCurrentPath
                If $iRet = 1 Then $aRetPathArr[$aRetPathArr[0]] = $sPath
            EndIf
        EndIf
    WEnd

    FileClose($hSearch)

    If $aRetPathArr[0] = 0 Then Return SetError(3, 0, 0)

    Return $aRetPathArr
EndFunc

;-))

Stefan

Link to comment
Share on other sites

yes thank you 99ojo thank you for changing that for me i could not get it to work i will study the new code and learn from it thanks so much

and im sorry about the post i understand i fixed it

mike

Hi,

next time please use

 
tags for posting code.

Is this, what you mean:

#include <array.au3>

$spath = "c:\temp"
$sRequest = "*.txt"

$aResults = _FileSearch($sPath, $sRequest, 1, 0)
If @error Then 
    MsgBox(48, "Attention!", "No files found.")
Else
    _ArrayDisplay ($aResults)
EndIf
;================================================================================
;Flag = 1 search with recurse
;Flag <> 1 search without recurse
;
;$iRet = 1 return parent directory of the file path
;$iRet <> 1 return full file path
;
;$iStatus_CtrlID - If this is an id of the label control, the function will set the current status of the search
;
;On Failure set @error as following:
; 1 - $sPath is not a dir or it not exists (in this case returned -1).
; 2 - $sPath is empty dir.
; 3 - No files found in $sPath dir.
;
;On Seccess return an array with found files.
;================================================================================
Func _FileSearch($sPath, $sMask, $iFlag=0, $iRet=0, $iStatus_CtrlID=0)
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, -1)

    Local $aRetPathArr[1], $sFindNextFile, $sCurrentPath, $aSubDirFindArr
    If StringInStr($sMask, "*") Then $sMask = StringReplace($sMask, "*.", "")

    $sPath = StringRegExpReplace($sPath, '\\+ *$', '\')

    Local $hSearch = FileFindFirstFile($sPath & "\*.*")
    If @error = 1 Then Return SetError(2, 0, 0)
    If $hSearch = -1 Then Return SetError(3, 0, 0)

    While 1
        $sFindNextFile = FileFindNextFile($hSearch)
        If @error = 1 Then ExitLoop

        $sCurrentPath = $sPath & "\" & $sFindNextFile

        If $iFlag = 1 And StringInStr(FileGetAttrib($sCurrentPath), "D") Then
            $aSubDirFindArr = _FileSearch($sCurrentPath, $sMask, $iFlag, 0, $iStatus_CtrlID)
            If @error Then ContinueLoop

            For $i = 1 To $aSubDirFindArr[0]
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]

                $aRetPathArr[$aRetPathArr[0]] = $aSubDirFindArr[$i]
                If $iRet = 1 Then _
                $aRetPathArr[$aRetPathArr[0]] = StringRegExpReplace($aRetPathArr[$aRetPathArr[0]], "\\[^\\]*$", "")
            Next
        Else
            If $sMask = "*" Or $sFindNextFile = $sMask Or StringRegExpReplace($sCurrentPath, '^.*\.', '') = $sMask Then
                $aRetPathArr[0] += 1
                ReDim $aRetPathArr[$aRetPathArr[0]+1]

                $aRetPathArr[$aRetPathArr[0]] = $sCurrentPath
                If $iRet = 1 Then $aRetPathArr[$aRetPathArr[0]] = $sPath
            EndIf
        EndIf
    WEnd

    FileClose($hSearch)

    If $aRetPathArr[0] = 0 Then Return SetError(3, 0, 0)

    Return $aRetPathArr
EndFunc

;-))

Stefan

Link to comment
Share on other sites

yes thank you 99ojo thank you for changing that for me i could not get it to work i will study the new code and learn from it thanks so much

and im sorry about the post i understand i fixed it

mike

Hi,

you are welcome.

You don't have to feel sorry about your posting style, just keep it in mind for future.

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...