Sign in to follow this  
Followers 0

_FileSearch

12 posts in this topic

Posted (edited)

decided to gave it its own post since it was faster then anything i have seen.

Func _FileSearch($s_Mask = '', $i_Recurse = 1)
    Local $s_Command = ' /c dir /B "'
    If $i_Recurse = 1 Then $s_Command = ' /c dir /B /S "'
    Local $s_Buf = '', $i_Pid = Run(@ComSpec & $s_Command & $s_Mask & '"', @WorkingDir, @SW_HIDE, 2+4)
    ProcessSetPriority($i_Pid, 5)
    While Not @error
        $s_Buf &= StdoutRead($i_Pid)
    WEnd
    $s_Buf = StringSplit(StringTrimRight($s_Buf, 2), @CRLF, 1)
    ProcessClose($i_Pid)
    If UBound($s_Buf) = 2 AND $s_Buf[1] = '' Then SetError(1)
    Return $s_Buf
EndFunc  ;==>_FileSearch

new: added optional sub search

Edited by w0uter

Share this post


Link to post
Share on other sites



Posted

Stupid question, how do i use this? ;)

Share this post


Link to post
Share on other sites

Posted

$t = TimerInit()
$var = _FileSearch('C:\*.bat')
ConsoleWrite(Timerdiff($t) & @CRLF)

For $i = 1 to $var[0]
ConsoleWrite($var[$i] & @LF)
Next

Share this post


Link to post
Share on other sites

Posted (edited)

Here is a text file search using similar algorithm.

Func _TextSearch($s_Mask = "",$text = "",$recursive = 0)
    Local $s_Buf = ""
    Dim $array[1]
    
    If $recursive Then
        $i_Pid = Run(@ComSpec & ' /c findstr /I /M /P /S /C:"' & $text & '" "' & $s_Mask & '"',@WorkingDir,@SW_HIDE,2)
    Else
        $i_Pid = Run(@ComSpec & ' /c findstr /I /M /P /C:"' & $text & '" "' & $s_Mask & '"',@WorkingDir,@SW_HIDE,2)
    EndIf
    
    While Not @error
        $s_Buf &= StdoutRead($i_Pid)
    WEnd
    
    return StringSplit(StringTrimRight($s_Buf, 2), @CRLF, 1)
EndFunc
Edited by livewire

Share this post


Link to post
Share on other sites

Posted

Yep wOuter, these are the fastest that I've seen so far. I figured out why I got bad data comparing the scripts...I had "Indexing Service" turned off.

Note: Make sure "Indexing service" is enabled for best results.

-Livewire

Share this post


Link to post
Share on other sites

Posted

This is damn fast. Was exactly what I was looking for. Other scripts using _FileFindFirst/Next were prohibitively slow.

Thanks w0uter

Share this post


Link to post
Share on other sites

Posted

Can anyone please tell me why this doesn't work for me?

$var = _FileSearch('C:\*.bat')

dim $results
For $i = 1 to $var[0]
$results &= $var[$i] & @CRLF
Next

msgbox(0, 'Test', $results)

Func _FileSearch($s_Mask = '', $i_Recurse = 1)
    Local $s_Command = ' /c dir /B "'
    If $i_Recurse = 1 Then $s_Command = ' /c dir /B /S "'
    Local $s_Buf = '', $i_Pid = Run(@ComSpec & $s_Command & $s_Mask & '"', @WorkingDir, @SW_HIDE, 2+4)
    ProcessSetPriority($i_Pid, 5)
    While Not @error
        $s_Buf &= StdoutRead($i_Pid)
    WEnd
    $s_Buf = StringSplit(StringTrimRight($s_Buf, 2), @CRLF, 1)
    ProcessClose($i_Pid)
    If UBound($s_Buf) = 2 AND $s_Buf[1] = '' Then SetError(1)
    Return $s_Buf
EndFunc ;==>_FileSearch

It's basically the exact code that's pasted above, just using a msgbox() rather than consolewrite().

It should return 3 batch files, but I don't get anything returned. Here's what command shows when I run it manually:

C:\>cmd /c dir /B /S c:\*.bat
c:\AUTOEXEC.BAT
c:\Program Files\Vim\vimtutor.bat
c:\WINDOWS\system32\MsDtc\Trace\msdtcvtr.bat

So what am I doing wrong? I'm running AutoIt 3.2.0.1. Thanks.

Share this post


Link to post
Share on other sites

Posted

Nevermind. It figures that I figure it out as soon as I post the question. The ProcessSetPriority() line seems to be causing the problem. When I remove that line, it works everytime. If I leave it in, it works about 1 out of every 10 times. Weird.

Anyway, just a heads up if anyone else has a similar problem.

Share this post


Link to post
Share on other sites

Posted (edited)

Change the While Not error to Do...Until @error as the previous is also checking ProcessSetPriority() for error which you do not want.

Removing ProcessSetPriority() altogether is a much better idea as I see it doing little anyway.

Edited by MHz

Share this post


Link to post
Share on other sites

Posted

I ended up just removing it altogether. Like you said, there didn't seem to be much benefit, at least not in my case.

Share this post


Link to post
Share on other sites

Posted

I had the same problems a long time ago, I removed the Process Priority line as well.

Share this post


Link to post
Share on other sites

Posted

I have use this to write a small file search:

; start directory
const $programpath = @SystemDir

; Search recursive?
; @param  bool  True/False
Local $SearchRecurse = False

; example - search this files
Local $DateiArray[4]
$DateiArray[0] = "eula.txt"; exist
$DateiArray[1] = "foo.dll" ; not exist
$DateiArray[2] = "hid.dll" ; exist
$DateiArray[3] = "ias.mdb" ; exist in system32/ias/


Local $DateiGefunden
For $i = 0 to UBound($DateiArray) -1
  Local $var = _FileSearch($programpath & '\' & $DateiArray[$i], $SearchRecurse)
  if $var[1] <> '' Then $DateiGefunden &= $DateiArray[$i] & @LF
Next

MsgBox(0, "File found!", 'This files found: '& @CRLF&@CRLF & $DateiGefunden )

; File Search
;
; @param  string  file name or file mask (*.txt)
; @param  bool  True/False
; @return array   [0] - ?, [1] - file name
Func _FileSearch($s_Mask = '', $i_Recurse = 1)
    Local $s_Command = ' /c dir /B "'
    If $i_Recurse = 1 Then $s_Command = ' /c dir /B /S "'
    Local $s_Buf = '', $i_Pid = Run(@ComSpec & $s_Command & $s_Mask & '"', @WorkingDir, @SW_HIDE, 2+4)
;~   ProcessSetPriority($i_Pid, 5)
    While Not @error
      $s_Buf &= StdoutRead($i_Pid)
    WEnd
    $s_Buf = StringSplit(StringTrimRight($s_Buf, 2), @CRLF, 1)
;~   ProcessClose($i_Pid)
    If UBound($s_Buf) = 2 AND $s_Buf[1] = '' Then SetError(1)
    Return $s_Buf
EndFunc ;==>_FileSearch

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

  • Recently Browsing   0 members

    No registered users viewing this page.