myFileListToArray()函数源代码如下:
;~ example:
;~ $sPath = "D:\AutoIt\article\autoitv3.0"
;~ $aFile = myFileListToArray($sPath, "\.au3$", 1, "zt_,_private,BackUp,images")
;~ If IsArray($aFile) Then
;~ For $i = 1 To $aFile[0] Step 1
;~ ConsoleWrite($i & "=" & $aFile[$i] & @CRLF)
;~ Next
;~ EndIf
;===============================================================================
;
; Description: lists files or directory(subdirectory) in a specified path (Similar to using Dir with the /b /s Switch)
; 列出目录下所有文件或文件夹,包括子文件夹.(yidabu.com注:autoit官方版本_FileListToArray不包括搜索子文件夹,也不支持正则表达式)
; Syntax: myFileListToArray($sPath,$rPath=0, $iFlag = 0,$sPathExclude=0)
; Parameter(s): $sPath = Path to generate filelist for 要搜索的路径
; $iFlag = determines weather to return file or folders or both
; $rPath = The regular expression to compare. StringRegExp Function Reference For details
; 用于搜索的正则表达式,用法同StringRegExp函数
; $iFlag=0(Default) Return both files and folders 返回所有文件和文件夹
; $iFlag=1 Return files Only 仅返回文件
; $iFlag=2 Return Folders Only 仅返回文件夹
; $sPathExclude = when word in path, the file/folder will exclude,mulite words separate by comma(,)
; 在路径中要要排除的词语,多个有,分隔
;
; Requirement(s): autoit v3.2.2.0
; Return Value(s): On Success - Returns an array containing the list of files and folders in the specified path
; On Failure - Returns the an empty string "" if no files are found and sets @Error on errors
; @Error=1 Path not found or invalid
; @Error=3 Invalid $iFlag
; @Error=4 No File(s) Found
;
; Author(s): by http://www.yidabu.com 一大步成功社区 http://bbs.yidabu.com/forum-2-1.html
; update: 20070125
; Note(s): The array returned is one-dimensional and is made up as follows:
; $array[0] = Number of Files\Folders returned 返回文件/文件夹的数量
; $array[1] = 1st File\Folder (not include $sPath) 第一个文件/文件夹
; $array[2] = 2nd File\Folder (not include $sPath)
; $array[3] = 3rd File\Folder (not include $sPath)
; $array[n] = nth File\Folder (not include $sPath)
; Special Thanks to SolidSnake <MetalGX91 at GMail dot com> (his _FileListToArray)
;===============================================================================
Func myFileListToArray($sPath, $rPath = 0, $iFlag = 0, $sPathExclude = 0)
Local $asFileList[1] ;yidabu.com提示:因为要用递归调用,$asFileList参数要单独出来
$asFileList = myFileListToArrayTemp($asFileList, $sPath, $rPath, $iFlag, $sPathExclude)
Return $asFileList
EndFunc ;==>myFileListToArray
Func myFileListToArrayTemp(ByRef $asFileList, $sPath, $rPath = 0, $iFlag = 0, $sPathExclude = 0)
Local $hSearch, $sFile
If Not FileExists($sPath) Then Return SetError(1, 1, "")
If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
$hSearch = FileFindFirstFile($sPath & "\*")
If $hSearch = -1 Then Return SetError(4, 4, "")
While 1
$sFile = FileFindNextFile($hSearch)
If @error Then
SetError(0)
ExitLoop
EndIf
;yidabu.com提示:已经被排除的路径,就不要搜索子目录了
If $sPathExclude And StringLen($sPathExclude) > 0 Then $sPathExclude = StringSplit($sPathExclude, ",")
$bExclude = False
If IsArray($sPathExclude) Then
For $ii = 1 To $sPathExclude[0] Step 1
If StringInStr($sPath & "" & $sFile, $sPathExclude[$ii]) Then
$bExclude = True
ExitLoop
EndIf
Next
EndIf
If $bExclude Then ContinueLoop
Select
Case StringInStr(FileGetAttrib($sPath & "" & $sFile), "D") ;如果遇到目录
Select
Case $iFlag = 1 ;求文件时就递归
myFileListToArrayTemp($asFileList, $sPath & "" & $sFile, $rPath, $iFlag, $sPathExclude)
ContinueLoop ;求文件时跳过目录
Case $iFlag = 2 Or $iFlag = 0 ;求目录时分两种情况
If $rPath Then ;1如果要求对路径进行正则匹配
If Not StringRegExp($sPath & "" & $sFile, $rPath, 0) Then ;正则匹配失败就递归
myFileListToArrayTemp($asFileList, $sPath & "" & $sFile, $rPath, $iFlag, $sPathExclude)
ContinueLoop ;正则匹配失败时跳过本目录
Else ;正则匹配成功就递归,并把本目录加入匹配成功
myFileListToArrayTemp($asFileList, $sPath & "" & $sFile, $rPath, $iFlag, $sPathExclude)
EndIf
Else ;2如果不要求对路径进行正则匹配,递归,并把本目录加入匹配成功,
myFileListToArrayTemp($asFileList, $sPath & "" & $sFile, $rPath, $iFlag, $sPathExclude)
EndIf
EndSelect
Case Not StringInStr(FileGetAttrib($sPath & "" & $sFile), "D") ;如果遇到文件
If $iFlag = 2 Then ContinueLoop ;求目录时就跳过
;yidabu.com提示:要求正则匹配路径,且匹配失败时就跳过。遇文件就不要递归调用了。
If $rPath And Not StringRegExp($sPath & "" & $sFile, $rPath, 0) Then ContinueLoop
EndSelect
ReDim $asFileList[UBound($asFileList) + 1]
$asFileList[0] = $asFileList[0] + 1
$asFileList[UBound($asFileList) - 1] = $sPath & "" & $sFile
WEnd
FileClose($hSearch)
Return $asFileList
EndFunc ;==>myFileListToArrayTemp
原文地址