mkmcst Posted July 18, 2009 Share Posted July 18, 2009 hi all i got this bit of code of the forum but it runs real slow and i was looking for a way to search c drive a lil bit fast this bit of code for me moves slow i would love some Ideas if anyone has one thank you michael ; Searches all subfolders of $sPath for $sFindFile (* and ? wildcards accepted) ; Returns an array containing full path and name of all matches. ; Number of matches is in zero index of array Func _FindPathName($sPath, $sFindFile) Local $sSubFolderPath, $iIndex, $aFolders $search = FileFindFirstFile($sPath & "\" & $sFindFile) $aFolders = _FileListToArray($sPath, "*", 2) While 1 $file = FileFindNextFile($search) If @error Then ExitLoop Else $sRet &= $sPath & "\" & $file & "|" EndIf WEnd FileClose($search) For $iIndex = 1 To $aFolders[0] $sSubFolderPath = $sPath & "\" & $aFolders[$iIndex] $aFoldersSubs = _FileListToArray($sSubFolderPath, "*", 2) If IsArray($aFoldersSubs) Then _FindPathName($sSubFolderPath, $sFindFile) Next Return StringSplit(StringTrimRight($sRet,1), "|") EndFunc ;==>_FindPathName ^SLEEPY^sleepys-sin scripting Link to comment Share on other sites More sharing options...
BugFix Posted July 18, 2009 Share Posted July 18, 2009 It's not exactly, what you want. But so you get (with good speed) the first occurance of an file. ;=============================================================================== ; Function Name: _SearchTreeForFile($sRootPath, $sFileName) ; Description:: Filesearch in given path and all subdirectories ; Parameter(s): $sRootPath directory to start search ; $sFileName file to search ; Return Value(s): if found path of file ; not found 0 ; Author(s): BugFix (bugfix@autoit.de) ;=============================================================================== Func _SearchTreeForFile($sRootPath, $sFileName) If StringRight($sRootPath, 1) <> '\' Then $sRootPath &= '\' Local $sOutputPathBuffer = '' Local $aRet = DllCall("imagehlp", 'long', 'SearchTreeForFile', 'str', _ $sRootPath, 'str', $sFileName, 'str', $sOutputPathBuffer) If $aRet[0] = 1 Then Return $aRet[3] Else Return 0 EndIf EndFunc ;==>_SearchTreeForFile Best Regards BugFix Link to comment Share on other sites More sharing options...
Nirvana6 Posted July 18, 2009 Share Posted July 18, 2009 This is code I found here on these forums (thanks to weaponx). I just added some input boxes for easier use. hope this helps expandcollapse popup#comments-start; _FileSearch( "Path and Mask", <$nOption>, <$cpusaver>, <$dirfilter>, <$filefilter>) ;----PARAMETERS----- ;$nOption - <Optional (0 - normal, 1- recursive)> ;$cpusaver - <Optional (0 - normal, 1 - CPU Friendly, but Slower)> ;$dirfilter - <Optional (0 - list directories, 1 - filter out directories)> ;$filefilter- <Optional (0 - list files, 1 - filter out files)> #comments-end $Path= InputBox ("Disk Drive" , "Drive Letter") $Mask= InputBox ("File&Folders" , "Mask") #include<array.au3> $a = _FileSearch( $Path&$Mask, 1, 0, 1, 0) _ArrayDisplay($a) MsgBox (0 ,"User Name " ,@UserName ) Func _FileSearch($szMask, $nOption = 0, $cpusaver = 0, $dirfilter = 0, $filefilter = 0) $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, $cpusaver, $dirfilter, $filefilter) 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, $cpusaver, $dirfilter, $filefilter) 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 ;==>_FileSearch Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN, $cpusaver, $dirfilter, $filefilter) $hFile = FileFindFirstFile($ROOT & $MASK) If $hFile >= 0 Then $szBuffer = FileFindNextFile($hFile) While Not @error If $cpusaver = 1 Then Sleep(1) ;OPTIONAL FOR CPU SAKE If $szBuffer <> "." And $szBuffer <> ".." Then If StringInStr(FileGetAttrib($ROOT & $szBuffer), "D") Then If $dirfilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*" Else If $filefilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*" EndIf EndIf $szBuffer = FileFindNextFile($hFile) WEnd FileClose($hFile) EndIf EndFunc Link to comment Share on other sites More sharing options...
mkmcst Posted July 20, 2009 Author Share Posted July 20, 2009 ok thank you i'll give them a shot thanks for the help ^SLEEPY^sleepys-sin scripting Link to comment Share on other sites More sharing options...
Spiff59 Posted July 20, 2009 Share Posted July 20, 2009 Or look at some of the many _FileListToArray examples in this thread Link to comment Share on other sites More sharing options...
ssimpson Posted October 16, 2009 Share Posted October 16, 2009 This code works great but is there any way to use wildcards? for instance I want to search all application data folders for .EXE files. Well each App Data folder resides within a user names folder. I.E. C:\Documents and Settings\John\Application Data, C:\Documents and Settings\Larry\Application Data, etc.... This is code I found here on these forums (thanks to weaponx). I just added some input boxes for easier use. hope this helps expandcollapse popup#comments-start; _FileSearch( "Path and Mask", <$nOption>, <$cpusaver>, <$dirfilter>, <$filefilter>) ;----PARAMETERS----- ;$nOption - <Optional (0 - normal, 1- recursive)> ;$cpusaver - <Optional (0 - normal, 1 - CPU Friendly, but Slower)> ;$dirfilter - <Optional (0 - list directories, 1 - filter out directories)> ;$filefilter- <Optional (0 - list files, 1 - filter out files)> #comments-end $Path= InputBox ("Disk Drive" , "Drive Letter") $Mask= InputBox ("File&Folders" , "Mask") #include<array.au3> $a = _FileSearch( $Path&$Mask, 1, 0, 1, 0) _ArrayDisplay($a) MsgBox (0 ,"User Name " ,@UserName ) Func _FileSearch($szMask, $nOption = 0, $cpusaver = 0, $dirfilter = 0, $filefilter = 0) $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, $cpusaver, $dirfilter, $filefilter) 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, $cpusaver, $dirfilter, $filefilter) 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 ;==>_FileSearch Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN, $cpusaver, $dirfilter, $filefilter) $hFile = FileFindFirstFile($ROOT & $MASK) If $hFile >= 0 Then $szBuffer = FileFindNextFile($hFile) While Not @error If $cpusaver = 1 Then Sleep(1) ;OPTIONAL FOR CPU SAKE If $szBuffer <> "." And $szBuffer <> ".." Then If StringInStr(FileGetAttrib($ROOT & $szBuffer), "D") Then If $dirfilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*" Else If $filefilter = 0 Then $RETURN = $RETURN & $ROOT & $szBuffer & "*" EndIf EndIf $szBuffer = FileFindNextFile($hFile) WEnd FileClose($hFile) EndIf EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now