Jump to content

File search...


ezzetabi
 Share

Recommended Posts

Func _FileSearch($sIstr, $bSF)
; $bSF = 1 means looking in subfolders
; $sSF = 0 means looking only in the current folder.
; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
  Local $sIstr, $bSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
  $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
  If $sCP = '' Then $sCP = @WorkingDir & '\'
  $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
  If $sCriteria = '' Then $sCriteria = '*.*'
  
;To begin we seek in the starting path.
  $sCS = FileFindFirstFile($sCP & $sCriteria)
  If $sCS <> - 1 Then
  While 1
     $sCF = FileFindNextFile($sCS)
     If @error Then
        FileClose($sCS)
        ExitLoop
     EndIf
     If $sCF = '.' Or $sCF = '..' Then ContinueLoop
     $sOutPut = $sOutPut & $sCP & $sCF & @LF
  Wend
  EndIf
  
;And after, if needed, in the rest of the folders.
  If $bSF = 1 Then
     $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
     Do 
        $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1),1);current search.
        $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
        $iH = FileFindFirstFile($sCS)
        If $iH <> - 1 Then
        While 1
           $sCF = FileFindNextFile($iH)
           If @error Then
              FileClose($iH)
              ExitLoop
           EndIf
           If $sCF = '.' Or $sCF = '..' Then ContinueLoop
           If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
              $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
              $sFP = $sCP & $sCF & '\';                         for future searches
              $iH2 = FileFindFirstFile($sFP & $sCriteria);       and checked with the criteria.
              If $iH2 <> - 1 Then
              While 1
                 $sCF2 = FileFindNextFile($iH2)
                 If @error Then
                    FileClose($iH2)
                    ExitLoop
                 EndIf
                 If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                 $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
              Wend
              EndIf
           EndIf
        Wend
        EndIf
        $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
     Until $sBuffer = ''
  EndIf
  
  If $sOutPut = '' Then
     $aNull[0] = 0
     Return $aNull
  Else
     Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
  EndIf
EndFunc ;==>_FileSearch

:)

Edited by ezzetabi
Link to comment
Share on other sites

Hey ezzetabi could you check on my code in the following forum? I just wrote a code that is supposed to be looking up folder names for me and matching them up with a username and password from a text file.

http://www.autoitscript.com/forum/index.php?showtopic=5455

For some reason after like 8-9 hours now the code is still running, with no sucess.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Hi,

don't be angry, i modified u're script a little bit. have a look ...

Dim $Files
Dim $Dir="C:\"                      ; specify the directory to search
Dim $Filemask="*.cfg"               ; specify the filemask

                                    ; Options are :

                                    ; 1 - looking in subfolders and not in current only
                                    ; 2 - show search status window
                                    ; 4 - cancel search by first match found
    
$Files = _FileSearch($Dir, $Filemask, 1+2+4)

If $Files[0] > 0 Then
    For $i = 1 to $Files[0]
         MsgBox(4096,"",$Files[$i])
    Next
EndIf

#Include "FileSearch.au3"

Func _FileSearch($StartDir, $FileToFind, $Options)

; $StartDir     =   specify the directory to search
; $FileToFind   =   specify the filemask
; $Options      =   1 - looking in subfolders and not in current only
;                   2 - show search status window
;                   4 - cancel search by first match found

Local $StartDir
Local $FileToFind
Local $Options
Local $FileIndex
Local $CurrDir

Local $sCriteria
Local $sBuffer
Local $iH, $iH2
Local $sCS, $sCF, $sCF2, $sCP, $sFP
Local $sOutPut = ''
Local $aNull[1]

; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.

$sCP = StringLeft($StartDir, StringInStr($StartDir, '\', 0, -1))
If $sCP = '' Then $sCP = @WorkingDir & '\'
$sCriteria = StringTrimLeft($FileToFind, StringInStr($FileToFind, '\', 0, -1))
If $sCriteria = '' Then $sCriteria = '*.*'
  
; To begin we seek in the starting path

$sCS = FileFindFirstFile($sCP & $sCriteria)

If BitAND($Options,2) = 2 then
    SplashTextOn("Datei wird gesucht ...", $sCP, 400, 130, -1, -1, 22, "Arial", 10)
EndIf

While $sCS <> - 1
    $sCF = FileFindNextFile($sCS)
    If @error Then
        FileClose($sCS)
        ExitLoop
    EndIf
    If $sCF = '.' Or $sCF = '..' Then ContinueLoop
    $sOutPut = $sOutPut & $sCP & $sCF & @LF
    $FileIndex = $FileIndex + 1
    If $FileIndex > 0 AND BitAND($Options,4) = 4 Then
        FileClose($sCS)
        ExitLoop
    EndIf
Wend
  
; and after, if needed, in the rest of the folders.

If $FileIndex > 0 AND BitAND($Options,4) = 4 Then
Else
    If BitAND($Options,1) = 1 Then
        $sBuffer = @CR & $sCP & '*' & @LF; The buffer is set for keeping the given path plus a *.
        Do

            $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1),1); current search.
            $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1))                          ; current search path.
            $iH = FileFindFirstFile($sCS)
            If BitAND($Options,2) = 2 then
                If StringLen($sCP) > 55 Then
                    $CurrDir = StringLeft($sCP, 10) & " ... " & StringRight($sCP, 40)
                Else
                    $CurrDir = $sCP
                EndIf
        
                ControlSetText("Datei wird gesucht ...", _
                                "", _
                                "Static1", _
                                @CRLF & "Aktuelles Verzeichnis :" & _
                                @CRLF & $CurrDir & _
                                @CRLF & @CRLF & _
                                @CRLF & "Suche nach" & @TAB & @TAB & $sCriteria & _
                                @CRLF & "Gefundene Datei(en)" & @TAB & $FileIndex)
            EndIf
            While $iH <> - 1

                $sCF = FileFindNextFile($iH)
                If @error Then
                    FileClose($iH)
                    ExitLoop
                EndIf
                If $sCF = '.' Or $sCF = '..' Then ContinueLoop
                If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
                    $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer; Every folder found is added in the begin of buffer
                    $sFP = $sCP & $sCF & '\'                            ; for future searches
                    $iH2 = FileFindFirstFile($sFP & $sCriteria)         ; and checked with the criteria.
                    While $iH2 <> - 1

                        $sCF2 = FileFindNextFile($iH2)
                        If @error Then
                            FileClose($iH2)
                            ExitLoop
                        EndIf
                        If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                        $sOutPut = $sOutPut & $sFP & $sCF2 & @LF        ; Found items are put in the Output.
                        $FileIndex = $FileIndex + 1
                        If $FileIndex > 0 AND BitAND($Options,4) = 4 Then ExitLoop
                    Wend
                EndIf

            Wend
            $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')

        Until $sBuffer = '' Or $FileIndex > 0 AND BitAND($Options,4) = 4
    EndIf
EndIf
If BitAND($Options,2) = 2 then
    SplashOff()
EndIf

If $sOutPut = '' Then
    $aNull[0] = 0
    Return $aNull
Else
    Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
EndIf

EndFunc                                                             ; ==>_FileSearch

greetz

cape-city

Edited by Cape-City
Link to comment
Share on other sites

Angry? Not at all. I am instead happy that you found my script useful and you could use it for your needs (after some changes). The two options you asked for where indeed easy to implement.

Just I wonder, why stop to the first match? How can you know that is the right file?

About the seach window I do not think is a great idea, it is better a tooltip IMO.

Link to comment
Share on other sites

'coz i searching for such an unique filename, which can't exist twice.

When found it, the script hadn't to search further ...

Searchwindow, o.k. but is a Tooltip such as fast ? In some special

cases the script has to be run an NT machines and there is no tooltip.

Edited by Cape-City
Link to comment
Share on other sites

just another piece of code :)

Output-window:

#include <GUIConstants.au3>
#include <_FileSearch.au3>

$folder_count = 0

;search
$alpha = _FileSearch("C:\Programme\",0,"H",1)

; count folders
For $k = 1 To $alpha[0]
   If StringRight($alpha[$k],1) = "\" Then $folder_count = $folder_count + 1
Next

;GUI-stuff
$guiwidth = @DesktopWidth/2
$guiheight = @DesktopHeight/2

$gui = GUICreate("list folders",$guiwidth,$guiheight,-1,-1,$WS_SIZEBOX+$WS_MINIMIZEBOX+$WS_MAXIMIZEBOX)
GuiSetState(@SW_SHOW)

$tv = GuiCtrlCreateTreeView(10, 10, $guiwidth-20, $guiheight-50)


GUICtrlCreateTreeViewItem($alpha[0] & " (" & $alpha[0]-$folder_count & " files / " & $folder_count & " folders)",$tv)

For $i = 1 to $alpha[0]
      GUICtrlCreateTreeViewItem($alpha[$i],$tv)
Next



While 1

$msg = GuiGetMsg()

    Select
    
    Case $msg = $GUI_EVENT_CLOSE 
            ExitLoop        
   
   EndSelect
WEnd

The changed _FileSearch-function

Func _FileSearch($sIstr, $bSF, $attrib, $show)
   
; $bSF = 1 means looking in subfolders
; $sSF = 0 means looking only in the current folder
; $attrib = string with attribs, e.g. "" (treat all), "D" (treat directories) or "HD" (treat hidden directories)
; $show = 0     show files/directories with attribs
; $show = 1     show all but not files/diretories with given attribs
; An array is returned with the full path of all files found. The pos [0] keeps the number of elements
; folders have a finishing "\"

 Local $sIstr, $bSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1],$attrib_cut
 
 $attrib_cut = StringSplit($attrib,"");split the attribs
 
 $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
 If $sCP = '' Then $sCP = @WorkingDir & '\'
 $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
 If $sCriteria = '' Then $sCriteria = '*.*'
 
;To begin we seek in the starting path.
 $sCS = FileFindFirstFile($sCP & $sCriteria)
 While $sCS <> - 1
    $sCF = FileFindNextFile($sCS)
    If @error Then
       FileClose($sCS)
       ExitLoop
    EndIf
    If $sCF = '.' Or $sCF = '..' Then ContinueLoop
    
  ; show or show not files with given attribs
    For $i = 1 To $attrib_cut[0]
      If $show Then
         If NOT StringInStr(FileGetAttrib($sCP & $sCF),$attrib_cut[$i]) Then ContinueLoop 2; 2 cause of For..Next
      ElseIf StringInStr(FileGetAttrib($sCP & $sCF),$attrib_cut[$i]) Then
            ContinueLoop 2
      EndIf
    Next
    
  ; if folder add "\" (differ folders from files with no extension)
    If StringInStr(FileGetAttrib($sCP & $sCF),"D") Then
       $sOutPut = $sOutPut & $sCP & $sCF & "\" & @LF; << folder
    Else   
      $sOutPut = $sOutPut & $sCP & $sCF & @LF; << file
    EndIf
   
 Wend
 
;And after, if needed, in the rest of the folders.
 If $bSF = 1 Then
    $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
    Do 
       $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1),1);current search.
       $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
       $iH = FileFindFirstFile($sCS)
       While $iH <> - 1
          $sCF = FileFindNextFile($iH)
          If @error Then
             FileClose($iH)
             ExitLoop
          EndIf
          If $sCF = '.' Or $sCF = '..' Then ContinueLoop
          If StringInStr(FileGetAttrib($sCP & $sCF), 'D') Then
             $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
             $sFP = $sCP & $sCF & '\';                            for future searches
             $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
             While $iH2 <> - 1
                $sCF2 = FileFindNextFile($iH2)
                If @error Then
                   FileClose($iH2)
                   ExitLoop
                EndIf
                If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                
              ; show or show not files with given attribs
                For $j = 1 To $attrib_cut[0]
                  If $show Then
                     If NOT StringInStr(FileGetAttrib($sFP & $sCF2),$attrib_cut[$j]) Then ContinueLoop 2; 2 cause of For..Next
                  ElseIf StringInStr(FileGetAttrib($sFP & $sCF2),$attrib_cut[$j]) Then
                        ContinueLoop 2
                  EndIf
                Next
                
              ; if folder add "\" (differ folders from files with no extension)
                If StringInStr(FileGetAttrib($sFP & $sCF2),"D") Then
                   $sOutPut = $sOutPut & $sFP & $sCF2 & "\" & @LF; << folder
                Else
                   $sOutPut = $sOutPut & $sFP & $sCF2 & @LF; << file
                EndIf

                
             Wend
          EndIf
       Wend
       $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
    Until $sBuffer = ''
 EndIf
 
 If $sOutPut = '' Then
    $aNull[0] = 0
    Return $aNull
 Else
    Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
 EndIf
EndFunc;==>_FileSearch

edit: comment: I am not sure, maybe it's not good to use $attrib_cutted, wasn't there sth. about variables and the first 6 (?) letters that they have to be different.

Edited by sPeziFisH
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...