[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 ;=============================================================================== 2 ; 3 ; Description: lists all or preferred files and or folders in a specified path (Similar to using Dir with the /B Switch) 4 ; Syntax: _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '') 5 ; Parameter(s): $sPath = Path to generate filelist for 6 ; $sFilter = The filter to use. Search the Autoit3 manual for the word "WildCards" For details, support now for multiple searches 7 ; Example *.exe; *.txt will find all .exe and .txt files 8 ; $iFlag = determines weather to return file or folders or both. 9 ; $sExclude = exclude a file from the list by all or part of its name 10 ; Example: Unins* will remove all files/folders that start with Unins 11 ; $iFlag=0(Default) Return both files and folders 12 ; $iFlag=1 Return files Only 13 ; $iFlag=2 Return Folders Only 14 ; 15 ; Requirement(s): None 16 ; Return Value(s): On Success - Returns an array containing the list of files and folders in the specified path 17 ; On Failure - Returns the an empty string "" if no files are found and sets @Error on errors 18 ; @Error or @extended = 1 Path not found or invalid 19 ; @Error or @extended = 2 Invalid $sFilter or Invalid $sExclude 20 ; @Error or @extended = 3 Invalid $iFlag 21 ; @Error or @extended = 4 No File(s) Found 22 ; 23 ; Author(s): SmOke_N 24 ; Note(s): The array returned is one-dimensional and is made up as follows: 25 ; $array[0] = Number of Files\Folders returned 26 ; $array[1] = 1st File\Folder 27 ; $array[2] = 2nd File\Folder 28 ; $array[3] = 3rd File\Folder 29 ; $array[n] = nth File\Folder 30 ; 31 ; All files are written to a "reserved" .tmp file (Thanks to gafrost) for the example 32 ; The Reserved file is then read into an array, then deleted 33 ;=============================================================================== 34 #include-once 35 36 Func _FileListToArrayEx($sPath, $sFilter = '*.*', $iFlag = 0, $sExclude = '', $iRecurse = False) 37 If Not FileExists($sPath) Then Return SetError(1, 1, '') 38 If $sFilter = -1 Or $sFilter = Default Then $sFilter = '*.*' 39 If $iFlag = -1 Or $iFlag = Default Then $iFlag = 0 40 If $sExclude = -1 Or $sExclude = Default Then $sExclude = '' 41 Local $aBadChar[6] = ['\', '/', ':', '>', '<', '|'] 42 $sFilter = StringRegExpReplace($sFilter, '\s*;\s*', ';') 43 If StringRight($sPath, 1) <> '\' Then $sPath &= '\' 44 For $iCC = 0 To 5 45 If StringInStr($sFilter, $aBadChar[$iCC]) Or _ 46 StringInStr($sExclude, $aBadChar[$iCC]) Then Return SetError(2, 2, '') 47 Next 48 If StringStripWS($sFilter, 8) = '' Then Return SetError(2, 2, '') 49 If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, '') 50 Local $oFSO = ObjCreate("Scripting.FileSystemObject"), $sTFolder 51 $sTFolder = $oFSO.GetSpecialFolder(2) 52 Local $hOutFile = @TempDir & $oFSO.GetTempName 53 If Not StringInStr($sFilter, ';') Then $sFilter &= ';' 54 Local $aSplit = StringSplit(StringStripWS($sFilter, 8), ';'), $sRead, $sHoldSplit 55 For $iCC = 1 To $aSplit[0] 56 If StringStripWS($aSplit[$iCC],8) = '' Then ContinueLoop 57 If StringLeft($aSplit[$iCC], 1) = '.' And _ 58 UBound(StringSplit($aSplit[$iCC], '.')) - 2 = 1 Then $aSplit[$iCC] = '*' & $aSplit[$iCC] 59 $sHoldSplit &= '"' & $sPath & $aSplit[$iCC] & '" ' 60 Next 61 $sHoldSplit = StringTrimRight($sHoldSplit, 1) 62 If $iRecurse Then 63 RunWait(@Comspec & ' /c dir /b /s /a ' & $sHoldSplit & ' > "' & $hOutFile & '"', '', @SW_HIDE) 64 Else 65 RunWait(@ComSpec & ' /c dir /b /a ' & $sHoldSplit & ' /o-e /od > "' & $hOutFile & '"', '', @SW_HIDE) 66 EndIf 67 $sRead &= FileRead($hOutFile) 68 If Not FileExists($hOutFile) Then Return SetError(4, 4, '') 69 FileDelete($hOutFile) 70 If StringStripWS($sRead, 8) = '' Then SetError(4, 4, '') 71 Local $aFSplit = StringSplit(StringTrimRight(StringStripCR($sRead), 1), @LF) 72 Local $sHold 73 For $iCC = 1 To $aFSplit[0] 74 If $sExclude And StringLeft($aFSplit[$iCC], _ 75 StringLen(StringReplace($sExclude, '*', ''))) = StringReplace($sExclude, '*', '') Then ContinueLoop 76 Switch $iFlag 77 Case 0 78 If StringRegExp($aFSplit[$iCC], '\w:\\') = 0 Then 79 $sHold &= $sPath & $aFSplit[$iCC] & Chr(1) 80 Else 81 $sHold &= $aFSplit[$iCC] & Chr(1) 82 EndIf 83 Case 1 84 If StringInStr(FileGetAttrib($sPath & '\' & $aFSplit[$iCC]), 'd') = 0 And _ 85 StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') = 0 Then 86 If StringRegExp($aFSplit[$iCC], '\w:\\') = 0 Then 87 $sHold &= $sPath & $aFSplit[$iCC] & Chr(1) 88 Else 89 $sHold &= $aFSplit[$iCC] & Chr(1) 90 EndIf 91 EndIf 92 Case 2 93 If StringInStr(FileGetAttrib($sPath & '\' & $aFSplit[$iCC]), 'd') Or _ 94 StringInStr(FileGetAttrib($aFSplit[$iCC]), 'd') Then 95 If StringRegExp($aFSplit[$iCC], '\w:\\') = 0 Then 96 $sHold &= $sPath & $aFSplit[$iCC] & Chr(1) 97 Else 98 $sHold &= $aFSplit[$iCC] & Chr(1) 99 EndIf 100 EndIf 101 EndSwitch 102 Next 103 If StringTrimRight($sHold, 1) Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1)) 104 Return SetError(4, 4, '') 105 EndFunc 106 107 108 ;$sPath="C:\Documents and Settings\Administrateur\Bureau\arbo" 109 ;$sFilter = '*.*' 110 ;$iFlag=1 111 ;$sExclude = '' 112 ;$iRecurse=True 113 ;$TAB=_FileListToArrayEx($sPath, $sFilter, $iFlag, $sExclude, $iRecurse) 114 ;For $i = 0 To UBound($TAB) -1 115 ;MsgBox(0,"Info","$TAB["&$i&"]=" & $TAB[$i]) 116 ;Next
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Mar 17 22:47:18 2015 | Cross-referenced by PHPXref 0.7.1 |