Sign in to follow this  
Followers 0
ashley

_findfile?

6 posts in this topic

#1 ·  Posted (edited)

ok im making a program that search for whatever file type the user puts in the input box... and then it shows the programs in a list view with the program name, size and were it is....

Problems...

Cant get it to search...( i cant do anything else until then)

Can Anybody help...

here is my code:

#include <GUIConstants.au3>




#Region ### START Koda GUI section ### Form=
$Main_gui = GUICreate("Search your pc", 497, 231, 193, 115)
$Label1 = GUICtrlCreateLabel("You are searching for:", 88, 170, 108, 17)
$FileType = GUICtrlCreateInput("\*.exe", 200, 168, 121, 21)
$Label2 = GUICtrlCreateLabel("File types!", 328, 170, 51, 17)
$Progress1 = GUICtrlCreateProgress(8, 210, 481, 17)
$_ = GUICtrlCreateLabel("----------------------------------------------------------------------------------------------------------------------------------------------------------------", 8, 192, 484, 17)
$Searchfile = GUICtrlCreateButton("Search", 408, 168, 49, 25, 0)
$ListView1 = GUICtrlCreateListView("Program found|Program size|Program destantion", 16, 8, 466, 150)
GUICtrlSendMsg(-1, 0x101E, 0, 162)
GUICtrlSendMsg(-1, 0x101E, 1, 100)
GUICtrlSendMsg(-1, 0x101E, 2, 200)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Searchfile
$a = _FileSearch($FileType,1)
If $a[0] > 0 Then
For $i = 1 to $a[0]
MsgBox(4096,"",$a[$i]) ; this dont need to be here i need it to add it to the list view...
Next
EndIf
EndSwitch
WEnd


; search for file stuff

Func _FileSearch($szMask,$nOption)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]

    If Not StringInStr($szMask,"\") Then
         $szRoot = @SCRIPTDIR & "\"
    Else
         While StringInStr($szMask,"\")
              $szRoot = $szRoot & StringLeft($szMask,StringInStr($szMask,"\"))
              $szMask = StringTrimLeft($szMask,StringInStr($szMask,"\"))
         Wend
    EndIf
    If $nOption = 0 Then
         _FileSearchUtil($szRoot, $szMask, $szReturn)
    Else
         While 1
              $hFile = FileFindFirstFile($szRoot & "*.*")
              If $hFile >= 0 Then
                   $szBuffer = FileFindNextFile($hFile)
                   While Not @ERROR
                        If $szBuffer <> "." And $szBuffer <> ".." And _
                             StringInStr(FileGetAttrib($szRoot & $szBuffer),"D") Then _
                             $szPathList = $szPathList & $szRoot & $szBuffer & "*"
                        $szBuffer = FileFindNextFile($hFile)
                   Wend
                   FileClose($hFile)
              EndIf
              _FileSearchUtil($szRoot, $szMask, $szReturn)
              If $szPathList == "*" Then ExitLoop
              $szPathList = StringTrimLeft($szPathList,1)
              $szRoot = StringLeft($szPathList,StringInStr($szPathList,"*")-1) & "\"
              $szPathList = StringTrimLeft($szPathList,StringInStr($szPathList,"*")-1)
         Wend
    EndIf
    If $szReturn = "" Then
         $aNULL[0] = 0
         Return $aNULL
    Else
         Return StringSplit(StringTrimRight($szReturn,1),"*")
    EndIf
EndFunc

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
         $szBuffer = FileFindNextFile($hFile)
         While Not @ERROR
              If $szBuffer <> "." And $szBuffer <> ".." Then _
                   $RETURN = $RETURN & $ROOT & $szBuffer & "*"
              $szBuffer = FileFindNextFile($hFile)
         Wend
         FileClose($hFile)
    EndIf
EndFunc

;end search for file stuff

thanks

Edited by ashley

Share this post


Link to post
Share on other sites

ok. I think you want the founded file insert in the listview


My UDFs:- _RegEnumKey

Share this post


Link to post
Share on other sites

Heres my version, uses similar GUI. Credit goes to whomever made the _FileListToArrayEx function because it's built on that.

-You can seperate filenames by semicolon.

-You can use wildcards

-Spaces will be stripped, insert question marks for spaces

-I would advise against searching without choosing a folder, since there is no way to monitor progress

#include 

$Main_gui = GUICreate("Search your pc", 600, 231, 193, 115)
GUISetState (@SW_SHOW)

$ListView = GUICtrlCreateListView("Path|Size", 5, 5, 590, 150, BitOR($LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER))
GUICtrlCreateLabel ( "Filename(s):", 80,170, 60, 20)
$FileType = GUICtrlCreateInput("", 140, 168, 250, 21)
$BUTTONSearch = GUICtrlCreateButton("Search", 408, 165, 49, 25, 0)

GUICtrlCreateLabel ( "Directory:", 90,200, 60, 20)
$StartingDir = GUICtrlCreateInput("", 140, 198, 250, 21)
$BUTTONDir = GUICtrlCreateButton("Dir", 408, 195, 49, 25, 0)

;GUI Loop
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BUTTONSearch
            $Results = _FileSearch(GuiCtrlRead($StartingDir), GuiCtrlRead($FileType), 1, '', True)
            ;_ArrayDisplay($Results)
        Case $BUTTONDir
            GUICtrlSetData ( $StartingDir, FileSelectFolder ( "Choose the root folder to search", "" ))
    EndSwitch
WEnd

Func _FileSearch($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '', $iRecurse = False)
    ;Verify folder exists
    If Not FileExists($sPath) Then Return SetError(1, 1, '')
    
    ;Set file filter to default
    If $sFilter = -1 Or $sFilter = Default Then $sFilter = '*.*'
    
    ;Set folder/file flag to default
    If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0
    
    ;Set exclude filter to default
    If $sExclude = -1 Or $sExclude = Default Then $sExclude = ''
    
    ;Declare characters not allowed in file filter or exclude filter
    Local $aBadChar[6] = ['\', '/', ':', '>', '<', '|']
    
    ;Check file filter and exclude filter for bad characters
    For $iCC = 0 To 5
        If StringInStr($sFilter, $aBadChar[$iCC]) Or StringInStr($sExclude, $aBadChar[$iCC]) Then Return SetError(2, 2, '')
    Next
    
    ;Verify file filter isn't just empty space
    If StringStripWS($sFilter, 8) = '' Then Return SetError(2, 2, '')
    
    ;Verify file flag is 0(Files and folders),1(Files), or 2(Folders)
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, '')
    
    ;Append semi-colon to file filter (multiple filters allowed)
    If Not StringInStr($sFilter, ';') Then $sFilter &= ';'
    
    ;Split file filter string into array with whitespace removed (Filters with spaces will not work!)
    Local $aSplit = StringSplit(StringStripWS($sFilter, 8), ';'), $sRead
    ;Loop through file filters
    For $iCC = 1 To $aSplit[0]
        ;Skip empty filters
        If StringStripWS($aSplit[$iCC], 8) = '' Then ContinueLoop
        
        ;Check if leftmost character is a period and ????
        If StringLeft($aSplit[$iCC], 1) = '.' And UBound(StringSplit($aSplit[$iCC], '.')) - 2 = 1 Then 
            ;Set current file filter to wildcards
            $aSplit[$iCC] = '*' & $aSplit[$iCC]
        EndIf
        
        Local $iPid
        
        ;Recursive search if $iRecurse is set true
        If Not $iRecurse Then
            $iPid = Run(@ComSpec & ' /c ' & 'dir "' & $sPath & '\' & $aSplit[$iCC] & '" /b /o-e /od', '', @SW_HIDE, 6)
        Else
            $iPid = Run(@Comspec & ' /c dir /b /s /a "' & $sPath & '\' & $aSplit[$iCC] & '"', '', @SW_HIDE, 6)
        EndIf
        
        ;Capture output from dir command
        While 1
            $sRead &= StdoutRead($iPid)
            If @error Then ExitLoop
        WEnd
    Next
    
    ;Verify dir result isn't whitespace
    If StringStripWS($sRead, 8) = '' Then Return SetError(4, 4, '')
    
    ;Split search results by line
    Local $aFSplit = StringSplit(StringTrimRight(StringStripCR($sRead), 1), @LF)
    Local $sHold
    
    ;Loop through results array
    For $iCC = 1 To $aFSplit[0]
        ;If exclusion filter is greater than zero
        If $sExclude And StringLeft(StringTrimLeft($aFSplit[$iCC], StringInStr($aFSplit[$iCC], '\', 0, -1)), StringLen(StringReplace($sExclude, '*', ''))) = StringReplace($sExclude, '*', '') Then 
            ContinueLoop
        EndIf
        
        ;Check result against file/folder flag
        Switch $iFlag
            Case 0 ;Files and folders
                $sHold &= $aFSplit[$iCC] & Chr(1)
            Case 1 ;Files ONLY
                If StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
                
                ;Add item to listview, show size in mb
                GUICtrlCreateListViewItem ( $aFSplit[$iCC] & "|" & StringFormat("%.2f mb", FileGetSize($aFSplit[$iCC]) / 1048576), $ListView)
                _GUICtrlListViewSetColumnWidth ($ListView, 0, $LVSCW_AUTOSIZE)
            Case 2 ;Folders ONLY
                If Not StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') Then ContinueLoop
                $sHold &= $aFSplit[$iCC] & Chr(1)
        EndSwitch
    Next
    ;If # of characters in result is greater than 1, split by Start of Header special character and return
    If StringTrimRight($sHold, 1) Then 
        Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
    EndIf
    
    Return SetError(4, 4, '')
EndFunc

Share this post


Link to post
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
Sign in to follow this  
Followers 0