Jump to content

GetWildPath Function


aleeksunder
 Share

Recommended Posts

Hello!

I've just finished a function and decided to share it. Maybe you know some better alternatives or can give some advices to optimize it, since finally it completely blows up my brain :)

Function retruns array of paths that match the entire pattern. You pattern can be wild as you wish.

Usage:

;   Get <any files and folders that matches *.exe> inside <anydrive> \ <anyfolder that match 'W*nd*s'> \ <anyfolder that match 'Sys'>

_ArrayDisplay( GetWildPath( '*:\W*nd*s\Sys*\*.exe' ) )



;   Get <anyfolder that match 'W*nd*s'> inside <anydrive>

_ArrayDisplay( GetWildPath( '*:\Wind*s' , $FLTA_FOLDERS ) )



;   If pattern begins with '\' function interprets it as _root_ of a working directory's drive or directory passed as 3rd paramter

_ArrayDisplay( GetWildPath( '\*Te*\*34.*t*t*' , $FLTA_FOLDERS ) )



;   If pattern not begins with '\' function interprets it as relative path to working directory or directory passed as 3rd paramter

_ArrayDisplay( GetWildPath( '*Te*\*3*.*t*' , $FLTA_FILESFOLDERS , 'D:\' ) )

 

Function itself, maybe a bit hard-coded but as is:

#include-once
#include <script\autoit\AutoItConstants.au3>
#include <script\autoit\StringConstants.au3>
#include <script\autoit\File.au3>
#include <script\autoit\Array.au3>

Func GetWildPath( $s_pattern , $i_flag = $FLTA_FILESFOLDERS , $s_working_directory = @WorkingDir )

    $s_working_directory = StringRegExpReplace( $s_working_directory , '[\\/]+$' , '' )

    Local $a_split = StringSplit( $s_pattern , ':' )

    If Not @error Then
        $s_drive = $a_split[1]
        $s_path  = $a_split[2]
    Else
        $s_drive = StringSplit( $s_working_directory , ':' )[1]
        $s_path  = $a_split[1]
    EndIf

    If $s_drive = '*' Then
        Local $a_drives = DriveGetDrive( $DT_ALL )
    Else
        Local $a_drives[1]
        $a_drives[0] = _ArrayAdd( $a_drives , $s_drive & ':' )
    EndIf

    Local $a_result = []

    For $i_drive = 1 To $a_drives[0]
        If StringLeft( $s_path , 1 ) = '\' Or StringLeft( $s_path , 1 ) = '/' Then
            $s_path_root     = StringUpper( $a_drives[ $i_drive ] ) & '\'
            $s_path_relative = StringTrimLeft( $s_path , 1 )
        Else
            $s_path_root     = StringUpper( $a_drives[ $i_drive ] ) & StringSplit( $s_working_directory , ':' )[2]
            $s_path_relative = $s_path
        EndIf

        Local $a_path = StringSplit( $s_path_relative , '\/' )
        Local $a_final = []

        If $a_path[ 0 ] > 1 Then
            Local $a_relative = _FileListToArray( $s_path_root , $a_path[ 1 ] , $FLTA_FOLDERS , True )
            If Not @error Then
                For $i_path = 2 To $a_path[ 0 ]
                    If $i_path < $a_path[ 0 ] Then
                        Local $a_relative_result = []
                        For $i_relative = 1 To $a_relative[ 0 ]
                            Local $a = _FileListToArray( $a_relative[ $i_relative ] , $a_path[ $i_path ] , $FLTA_FOLDERS , True )
                            If Not @error Then
                                _ArrayConcatenate( $a_relative_result , $a , 1 )
                            EndIf
                        Next
                        $a_relative_result[0] = UBound( $a_relative_result ) - 1
                        $a_relative = $a_relative_result
                    Else
                        For $i_relative = 1 To $a_relative[0]
                            Local $a = _FileListToArray( $a_relative[ $i_relative ] , $a_path[ $i_path ] , $i_flag , True )
                            If Not @error Then
                                _ArrayConcatenate( $a_final , $a , 1 )
                            EndIf
                        Next
                        $a_final[0] = UBound( $a_final ) - 1
                    EndIf
                Next
            EndIf
        Else
            Local $a_final = _FileListToArray( $s_path_root , $a_path[ 1 ] , $i_flag , True )
        EndIf

        _ArrayConcatenate( $a_result , $a_final , 1 )

        $a_result[0] = UBound( $a_result ) - 1

    Next

    Return $a_result

EndFunc

Since I'm new to AutoIt all of your comments and ideas are welcome ;)

Edited by aleeksunder
Link to comment
Share on other sites

  • 8 months later...

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

×
×
  • Create New...