Jump to content

Limit file seaching in subfolders


Skunken
 Share

Recommended Posts

Hallo all,

Im trying to find out if i can limit my search for files to only go 2 or 3 subfolders down.

eksampel:

i want to search for "*.doc" in c:\

i want it to search in c:\somefolder\thisfolder\

but not in c:\somefolder\thisfolder\subfolder\

Is there a way to do this ???

Kind regards

Skunken

Link to comment
Share on other sites

Hi, and wellcome to the forum! :)

Is there a way to do this ?

Like this, this is an old script of mine, it's a litle bit slow and may be unstable (but without a recursive function call, all done by loops :) ):

#include <Array.au3>

$aFilesList = _FileListToArrayEx("C:\", "*.doc", 1, 2)
_ArrayDisplay($aFilesList)

;$iMode <= 0 -> Folders + files
;$iMode = 1 -> Files only
;$iMode = 2 -> Folders only
Func _FileListToArrayEx($sPath, $sMask="*", $iMode=-1, $iLevel=-1)
    If Not StringInStr(FileGetAttrib($sPath), "D") Then Return SetError(1, 0, 0)
    
    $sPath = StringRegExpReplace($sPath, "\\+$", "")
    $sMask = "(?i)" & StringReplace(StringReplace($sMask, ".", "\."), "*", ".*")
    
    StringReplace($sPath, "\", "")
    Local $iLevel_Slashes = @extended + 1
    
    Local $aPathesArr[2] = [1, $sPath]
    Local $hSearch, $sFindNext, $i, $iIsDir
    
    While $i < $aPathesArr[0]
        $i += 1
        
        $hSearch = FileFindFirstFile($aPathesArr[$i] & "\*")
        If $hSearch = -1 Then ContinueLoop
        
        While 1
            $sFindNext = FileFindNextFile($hSearch)
            If @error Then ExitLoop
            
            $iIsDir = StringInStr(FileGetAttrib($aPathesArr[$i] & "\" & $sFindNext), "D")
            
            If $iMode < 2 Or ($iMode = 2 And $iIsDir) Then
                If Not $iIsDir And Not StringRegExp($sFindNext, $sMask) Then ContinueLoop
                
                $aPathesArr[0] += 1
                ReDim $aPathesArr[$aPathesArr[0]+1]
                
                $aPathesArr[$aPathesArr[0]] = $aPathesArr[$i] & "\" & $sFindNext
            EndIf
        WEnd
        
        FileClose($hSearch)
        
        StringReplace($aPathesArr[$aPathesArr[0]], "\", "")
        If @extended - $iLevel_Slashes = $iLevel Then ExitLoop
    Wend
    
    If $iMode = 1 Then
        Local $aTmp_Arr = $aPathesArr
        Local $iTmp_Count = 0
        
        For $i = 1 To $aPathesArr[0]
            If StringInStr(FileGetAttrib($aPathesArr[$i]), "D") Then ContinueLoop
            
            $iTmp_Count += 1
            $aTmp_Arr[$iTmp_Count] = $aPathesArr[$i]
        Next
        
        $aTmp_Arr[0] = $iTmp_Count
        ReDim $aTmp_Arr[$iTmp_Count+1]
        
        $aPathesArr = $aTmp_Arr
    EndIf
    
    Return $aPathesArr
EndFunc
Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Hi MrCreatoR,

Thanks for the quick reply :)

Its a bit hard for me to see whats really going on in the script, but it looks like it first searches all folders and then removes the files that are over 2 subfolders away ?

right now im doing it like this:

$file1 = "Profiel.xml"
$d = "dir /b /s " & $file1 & " > c:\temp\filer.txt"
RunWAIT(@ComSpec & " /C " & $d, "", @SW_HIDE)

but need some sort of max subfolders to search.

My problem is that the subfolder has 3 million files in 2000 subfolder, and when i try to search through them all it takes for ever :)

but the files im looking for is allways in the folder right before

if its not to much to ask it would be nice with a couple of comments in our code

Kind Regards

Kasper

Link to comment
Share on other sites

Here's a recursive function that should do what you want.

_SearchForFile("C:", "*.doc", 2)

Func _SearchForFile($sDirectory, $sFileFilter, $nFolderDepth = -1, $nCurrentDepth = 0)
    If $nCurrentDepth > $nFolderDepth Then Return
    
    Local $sFilter, $hSearch, $sFile, $sFileName
    If IsKeyword($nFolderDepth) Then $nFolderDepth = -1
    $sFilter = $sFileFilter
    $sFilter = StringReplace($sFilter, ".", "\.")
    $sFilter = StringReplace($sFilter, "*", ".*")
    
    $hSearch = FileFindFirstFile($sDirectory & "\*")
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        $sFile = $sDirectory & "\" & $sFileName
        If StringInStr(FileGetAttrib($sFile), "D") <> 0 Then
            _SearchForFile($sFile, $sFileFilter, $nFolderDepth, $nCurrentDepth + 1)
        Else
            If StringRegExp($sFileName, "(?i)" & $sFilter, 0) Then ConsoleWrite("  " & $sFile & @LF)
        EndIf
    WEnd
EndFunc
Link to comment
Share on other sites

it looks like it first searches all folders and then removes the files that are over 2 subfolders away ?

No, it searches only the specified level.

But here is a little bit(?) modified function by zorphnog:

#include <Array.au3>

$sFiles_String = _FileListToString("C:\", "*.doc", 2)
ConsoleWrite($sFiles_String)

$aFiles_Array = StringSplit($sFiles_String, @CRLF, 1)
_ArrayDisplay($aFiles_Array, "_FileListToString")

Func _FileListToString($sDirectory, $sFileFilter, $nFolderDepth = -1, $nCurrentDepth = -1)
    $sDirectory = StringRegExpReplace($sDirectory, "\\+$", "")
    
    If $nCurrentDepth = -1 Then
        StringReplace($sDirectory, "\", "")
        $nCurrentDepth = @extended
        If $nCurrentDepth < 0 Then $nCurrentDepth = 0
    EndIf
    
    Local $sRet_Dirs_List = "", $sSubFilesList, $sRegExpFilter, $hSearch, $sFile, $sFileName
    
    If $nCurrentDepth > $nFolderDepth Then Return SetError(1, 0, "")
    If $nFolderDepth < 0 Or IsKeyword($nFolderDepth) Then $nFolderDepth = -1
    
    $sRegExpFilter = $sFileFilter
    $sRegExpFilter = StringReplace($sRegExpFilter, ".", "\.")
    $sRegExpFilter = StringReplace($sRegExpFilter, "*", ".*")
    
    $hSearch = FileFindFirstFile($sDirectory & "\*")
    
    While 1
        $sFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        
        $sFile = $sDirectory & "\" & $sFileName
        
        If StringInStr(FileGetAttrib($sFile), "D") <> 0 Then
            $sSubFilesList = _FileListToString($sFile, $sFileFilter, $nFolderDepth, $nCurrentDepth + 1)
            If $sSubFilesList <> "" Then $sRet_Dirs_List &= $sSubFilesList & @CRLF
        Else
            If StringRegExp($sFileName, "(?i)" & $sRegExpFilter) Then $sRet_Dirs_List &= $sFile & @CRLF
        EndIf
    WEnd
    
    FileClose($hSearch)
    
    Return StringStripWS($sRet_Dirs_List, 2)
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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...