Ticket #1128: _WinAPI_PathFindOnPath.au3

File _WinAPI_PathFindOnPath.au3, 3.0 KB (added by danielkza2@…, 15 years ago)

_WinAPI_PathFindOnPath

Line 
1; #EXAMPLE# =====================================================================================================================
2MsgBox(0, "PathFindOnPath Example", _
3        StringFormat(   "Full path of notepad.exe:\n%s\n\n" & _
4                                        "Find ntuser.dat in profile folder, using custom paths:\n%s", _
5                                        _WinAPI_PathFindOnPath("notepad.exe"), _WinAPI_PathFindOnPath("ntuser.dat", @UserProfileDir) _
6))
7; ===============================================================================================================================
8
9; #FUNCTION# ====================================================================================================================
10; Name...........: _WinAPI_PathFindOnPath
11; Description ...: Searchs for a file in the default system paths
12; Syntax.........: _WinAPI_PathFindOnPath($szFile, $aExtraPaths="", $szPathDelimiter=@LF)
13; Parameters ....: $szFile          - Filename to search for
14;                  $aExtraPaths     - Extra paths to check before any others.
15;                  $szPathDelimiter - Delimiter used to split $aExtraPaths if it's an non-empty string (StringSplit with flag 2).
16; Return values .: Success      - Full path of found file
17;                  Failure      - Unchanged filename, @error=1
18; Author ........: Daniel Miranda (danielkza)
19; Modified.......:
20; Remarks .......: $aExtraPaths can contain a list of paths to be checked before any system defaults.
21;                  It can be an array or a string. If the former, it shall not have a count in it's first element.
22;                  If the latter, it will be split using $szPathDelimiter as the delimiter, that defaults to @LF.
23; Related .......:
24; Link ..........; @@MsdnLink@@ PathFindOnpath
25; Example .......; No
26; ===============================================================================================================================
27
28Func _WinAPI_PathFindOnPath(Const $szFile, $aExtraPaths = "", Const $szPathDelimiter=@LF)
29        Local $iExtraCount = 0
30        If IsString($aExtraPaths) Then
31                If StringLen($aExtraPaths) Then
32                        $aExtraPaths = StringSplit($aExtraPaths, $szPathDelimiter, 1+2)
33                        $iExtraCount = UBound($aExtraPaths, 1)
34                EndIf
35        ElseIf IsArray($aExtraPaths) Then
36                $iExtraCount = UBound($aExtraPaths)
37        EndIf
38       
39        Local $tPaths, $tPathPtrs
40        If $iExtraCount Then
41                Local $szStruct = ""
42                For $path In $aExtraPaths
43                        $szStruct &= StringFormat("wchar[%d];", StringLen($path)+1)
44                Next
45               
46                $tPaths = DllStructCreate($szStruct)
47                $tPathPtrs = DLLStructCreate(StringFormat("ptr[%d];", $iExtraCount+1))
48               
49                For $i=1 To $iExtraCount
50                        DLLStructSetData($tPaths, $i, $aExtraPaths[$i-1])
51                        DLLStructSetData($tPathPtrs, 1, DLLStructGetPtr($tPaths, $i), $i)
52                Next
53                DLLStructSetData($tPathPtrs, 1, Ptr(0), $iExtraCount+1)
54        EndIf
55       
56        Local $aRet = DLLCall("shlwapi.dll", "int", "PathFindOnPathW", _
57                "wstr"  , $szFile, _
58                "ptr"   , DLLStructGetPtr($tPathPtrs) _
59        )
60        If @error OR NOT $aRet[0] Then Return SetError(1, 0, $szFile)
61       
62        Return $aRet[1]
63EndFunc