本帖最后由 xlcwxl 于 2009-10-2 01:32 编辑
怎样列出指定目录中的所有EXE,并显示在Combo中
UDF函数: _FileListToArray.au3Func myFileListToArray($sPath, $rPath = 0, $iFlag = 0, $sPathExclude = 0)
Local $asFileList[1] ;因为要用递归调用,$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
;已经被排除的路径,就不要搜索子目录了
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 ;求目录时就跳过
;要求正则匹配路径,且匹配失败时就跳过。遇文件就不要递归调用了。
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
例子 - 搜索D:\autoit3所有EXE文件; *******************************************************
; 例子 - 搜索D:\autoit3所有EXE文件
; *******************************************************
#include "_FileListToArray.au3"
$sPath = "D:\autoit3"
$aFile = myFileListToArray($sPath, "*.exe", 1, "manifest,images")
MsgBox(0, "找到的文件", $aFile)
为什么没反映,是我代码写错了还是,UDF函数有问题? |