找回密码
 加入
搜索
查看: 4412|回复: 6

求助怎样用 AutoIT 实现读取文件夹列表功能?

[复制链接]
发表于 2009-3-4 12:50:09 | 显示全部楼层 |阅读模式
请问,想读取指定目录下的全部文件并建立列表到 TXT 文件中,会有四、五层的子文件夹,但只能用 DOS 下的 DIR 命令实现吗?

求助怎样用 AutoIT 实现这个功能?

[ 本帖最后由 zxyy 于 2009-3-5 17:52 编辑 ]
发表于 2009-3-4 15:37:14 | 显示全部楼层
;===============================================================================
;
;  描述:        列出目录下所有文件或文件夹,包括子文件夹.(注:autoit官方版本_FileListToArray不包括搜索子文件夹,也不支持正则表达式)
;
;  函数:                myFileListToArray($sPath,$rPath=0,  $iFlag  =  0,$sPathExclude=0)
;
;  参数:          $sPath  =  要搜索的路径
;                 $iFlag  =  决定只返回文件或只返回文件夹或都返回
;                        $iFlag=0(Default)    返回所有文件和文件夹
;                        $iFlag=1   仅返回文件
;                        $iFlag=2    仅返回文件夹
;                 $rPath  =  用于搜索的正则表达式,用法同StringRegExp函数
;                 $sPathExclude  =  在路径中要要排除的词语,多个有,分隔
;
;  最低版本需求:    autoit  v3.2.2.0
;  返回值:      成功 -  返回搜索路径中的文件和文件夹,以数组模式返回
;               失败 -  返回一个空的字串,如果没有查询到符合条件的文件或文件夹,设置@error值
;                        @Error=1  搜索路径不存在
;                        @Error=3  搜索类型错误
;                        @Error=4  未找到符合条件的文件(文件夹)
;                                    
;  标注:            返回的数字的解释.
;                    $array[0]  =  返回文件/文件夹的数量
;                    $array[1]  =  第一个文件/文件夹
;                    $array[2]  =  第二个文件/文件夹
;                    $array[3]  =  第三个文件/文件夹
;                    $array[n]  =  第四个文件/文件夹
;                     ......以下类推...
;===============================================================================



Func  myFileListToArray($sPath,  $rPath  =  0,  $iFlag  =  0,  $sPathExclude  =  0)
    Local  $asFileList[1]       
    $asFileList  =  myFileListToArrayTemp($asFileList,  $sPath,  $rPath,  $iFlag,  $sPathExclude)
    Return  $asFileList
EndFunc  

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   
                            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    
                            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    
发表于 2009-3-4 15:43:39 | 显示全部楼层
FileDelete('out.txt')
_filelist("d:\autoit3", 'exe')
if FileExists("out.txt") then ShellExecuteWait("out.txt")
FileDelete("out.txt")
;枚举文件UDF
;作者: pcbar
;日期:2008-08-01
;$searchdir,要搜索的目录,$filetype,文件类型(如exe,txt),$outfile输出文件,可以包含路径
Func _filelist($searchdir, $filetype = "*.*", $outfile = "out.txt") ;;;函数名(形参)
        Local $temp
        If StringRight($searchdir, 1) <> "\" Then $searchdir &= '\'
        $search = FileFindFirstFile($searchdir & "*.*")
        If $search = -1 Then Return -1 ;;;;如果找不到,返回值 -1
        While 1
                $file = FileFindNextFile($search) ;;;查找下一个文件
                If @error Then ;;;如果找不到文件
                        FileClose($search) ;;;则关闭此句柄
                        Return
                ElseIf $file = "." Or $file = ".." Then ;;如果找到的文件名为.或..则 ContinueLoop
                        ContinueLoop ;;;在某些版本的AU3里面可以不需要上行和这行。
                ElseIf StringInStr(FileGetAttrib($searchdir & $file), "D") Then ;;如果找到的是一个文件夹,则
                        _filelist($searchdir & $file, $filetype, $outfile) ;;递归调用filelist函数,并传参数  "$searchdir & "\" & $file"
                EndIf ;;;$file为查找到的文件夹名称,上一行意思就是进入此文件夹继续查找文件.如此循环
                If $filetype = '*.*' Then
                        FileWriteLine($outfile, $searchdir & $file)
                Else
                        $temp = StringRegExp($file, '(?<=\.)\w+', 2, -1)
                        If IsArray($temp) And StringLower($temp[0]) = StringLower($filetype) Then
                                FileWriteLine($outfile, $searchdir & $file)
                        EndIf
                EndIf
        WEnd
EndFunc   ;==>_filelist
发表于 2009-3-4 20:38:10 | 显示全部楼层
我是一个宿命论者 宿命论再一次显示了他的强大

今天晚上正好要做一个 解压缩全部压缩文件的小工具..........
谁还能逃脱宿命?
 楼主| 发表于 2009-3-5 17:51:29 | 显示全部楼层
谢谢你了,感谢你们的帮助。
发表于 2010-1-21 12:49:42 | 显示全部楼层
我是一个宿命论者 宿命论再一次显示了他的强大

今天晚上正好要做一个 解压缩全部压缩文件的小工具...... ...
sxd 发表于 2009-3-4 20:38



神,可以逃脱宿命!...因为他不是人!...
发表于 2010-9-18 18:46:14 | 显示全部楼层
功能不错 值得学习
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-3 06:37 , Processed in 0.081936 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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