找回密码
 加入
搜索
查看: 1484|回复: 8

怎样列出指定目录中的所有EXE,并显示在Combo中

[复制链接]
发表于 2009-9-21 17:25:08 | 显示全部楼层 |阅读模式
本帖最后由 xlcwxl 于 2009-10-2 01:32 编辑

怎样列出指定目录中的所有EXE,并显示在Combo中

UDF函数: _FileListToArray.au3
Func  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函数有问题?
发表于 2009-9-21 17:58:08 | 显示全部楼层
含子目录不?
发表于 2009-9-21 18:25:01 | 显示全部楼层
咋都这样呢!丢个问题在这里就不管了。

既然是求知,是不是该时不时的来看看呢!
发表于 2009-9-21 18:59:06 | 显示全部楼层
3# 水木子
嗨,管呢,没回音说不定个人能力强呢,自己找到答案了
发表于 2009-9-21 19:07:04 | 显示全部楼层
并显示在Combo中

看到这后半句,估计这楼主多半还没有基本的au3知识,就想着要做出个什么工具来了。。。
 楼主| 发表于 2009-9-22 16:50:02 | 显示全部楼层
我的目的就是想列出某个文件夹下的所有EXE文件
发表于 2009-9-22 16:58:07 | 显示全部楼层
本帖最后由 yrloy 于 2009-9-22 16:59 编辑

看一下帮助中函数文件、驱动器那段,用通配符*.EXE,然后FileFindNextFile一个个文件句柄传递就可以了,不知道你看没看过SKY皮肤例子的那个源码
发表于 2009-9-22 17:07:27 | 显示全部楼层
如果及时配合别人提出的疑问,早解决了...
 楼主| 发表于 2009-9-22 17:34:38 | 显示全部楼层
不好意思,有一点忙
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-5 03:03 , Processed in 0.073293 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表