touch_xu 发表于 2011-1-2 12:13:25

_FileSearchToArray 如何跳过指定目录

本帖最后由 touch_xu 于 2011-1-7 21:44 编辑

情况是这样的,我要从下图所示目录中:
找出Data,Data1,Data11,等目录中定日期的txt文件,由于文件目录名不定,所的我要对父目录C:\Summary进行_FileSearchToArray 操作。
问题1Images目录是图片目录,动辄几个G的图片文件
问题2 Reports也是txt 格式文件,同样是数量相当多。

所以我想请求大家,我如何在_FileSearchToArray一次跳过指定Images Reports目录,而不是在复制文件时才这样操作,太慢了。If StringInStr($_FileList[$i], BitAND("Reports","Images")) Then
        ContinueLoop
Ensif

touch_xu 发表于 2011-1-2 15:03:39

这么多高手,怎么没人帮助

touch_xu 发表于 2011-1-2 15:06:22

肯请高手出现,如果有更好方法更好了,我想肯定有在生成数组时就跳过,就大大提高了效率

lainline 发表于 2011-1-2 16:06:58

只能多次运行 _FileSearchToArray查各个目录然后再合并数组

ceoguang 发表于 2011-1-2 17:17:07

_FileListToArray吧?
这个也是自定义函数来的,原形:Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
        Local $hSearch, $sFile, $sFileList, $sDelim = "|"
        $sPath = StringRegExpReplace($sPath, "[\\/]+\z", "") & "\" ; ensure single trailing backslash
        If Not FileExists($sPath) Then Return SetError(1, 1, "")
        If StringRegExp($sFilter, "[\\/:><\|]|(?s)\A\s*\z") Then Return SetError(2, 2, "")
        If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
        $hSearch = FileFindFirstFile($sPath & $sFilter)
        If @error Then Return SetError(4, 4, "")
        While 1
                $sFile = FileFindNextFile($hSearch)
                If @error Then ExitLoop
                If ($iFlag + @extended = 2) Then ContinueLoop
                $sFileList &= $sDelim & $sFile
        WEnd
        FileClose($hSearch)
        If Not $sFileList Then Return SetError(4, 4, "")
        Return StringSplit(StringTrimLeft($sFileList, 1), "|")
EndFunc;==>_FileListToArray自己改改就好了

3mile 发表于 2011-1-4 11:45:30

回复 1# touch_xu
不知道合不合楼主的要求。
#include <array.au3>
$searchdir1 = "c:\windows"
Local $flag='*.dll'
Local $flag1 = "c:\windows\system", $flag2 = 'c:\windows\system32'
Local $lines, $direct, $array

_filelist($searchdir1, $flag,$flag1, $flag2);_filelist('搜索目录','要搜索的扩展名','排除目录1','排除目录2','排除目录3','排除目录4')

$a = StringSplit(StringTrimRight($lines,1), '|')
_ArrayDisplay($a)

Func _filelist($searchdir, $flag, $es1 = '', $es2 = '', $es3 = '',$es4='')

    $search = FileFindFirstFile($searchdir & "\*")
    If $search = -1 Then Return -1
    While 1
      $File = FileFindNextFile($search)
      If @error Then
            FileClose($search)
            Return
      ElseIf $File = "." Or $File = ".." Then
            ContinueLoop
      ElseIf StringInStr(FileGetAttrib($searchdir & "\" & $File), "D") Then ;And $searchdir & "\" & $File <> $flag Then
            Select
                Case $searchdir & "\" & $File = $es1
                  ContinueLoop
                Case $searchdir & "\" & $File = $es2
                  ContinueLoop
                Case $searchdir & "\" & $File = $es3
                  ContinueLoop
                Case $searchdir & "\" & $File = $es4
                  ContinueLoop
                Case Else
                  _filelist($searchdir & "\" & $File, $flag, $es1 = '', $es2 = '', $es3 = '',$es4)
                  ContinueLoop
            EndSelect
      EndIf
      If StringRight($searchdir & "\" & $File,4)=StringRight($flag,4) Then
            $lines &= $searchdir & "\" & $File & "|"
            $array = StringSplit(StringTrimRight($lines, 1), '|')
      EndIf
    WEnd
    Return $array
EndFunc   ;==>_filelist

touch_xu 发表于 2011-1-4 21:55:11

回复touch_xu
不知道合不合楼主的要求。
3mile 发表于 2011-1-4 11:45 http://www.autoitx.com/images/common/back.gif


    多谢3mile

我也刚刚从UDF区找到了一个:

http://www.autoitx.com/thread-12130-1-4.html

touch_xu 发表于 2011-1-4 21:57:09

还有这个:_FileListToArray支持正则表达式和子目录搜索myFileListToArray()函数源代码如下:


;~example:
;~$sPath="D:\AutoIt\article\autoitv3.0"
;~$aFile=myFileListToArray($sPath,"\.au3$",1,"zt_,_private,BackUp,images")
;~IfIsArray($aFile)Then
;~      For$i=1To$aFile    Step1
;~          ConsoleWrite($i&"="&$aFile[$i]&@CRLF)
;~      Next
;~EndIf
;===============================================================================
;
;Description:      listsfilesordirectory(subdirectory)inaspecifiedpath(SimilartousingDirwiththe/b/sSwitch)
;                  列出目录下所有文件或文件夹,包括子文件夹.(yidabu.com注:autoit官方版本_FileListToArray不包括搜索子文件夹,也不支持正则表达式)
;Syntax:                myFileListToArray($sPath,$rPath=0,$iFlag=0,$sPathExclude=0)
;Parameter(s):          $sPath=Pathtogeneratefilelistfor要搜索的路径
;                        $iFlag=determinesweathertoreturnfileorfoldersorboth
;                  $rPath=Theregularexpressiontocompare.StringRegExpFunctionReferenceFordetails
;                  用于搜索的正则表达式,用法同StringRegExp函数
;                        $iFlag=0(Default)Returnbothfilesandfolders返回所有文件和文件夹
;                              $iFlag=1ReturnfilesOnly仅返回文件
;                        $iFlag=2ReturnFoldersOnly仅返回文件夹
;                  $sPathExclude=whenwordinpath,thefile/folderwillexclude,mulitewordsseparatebycomma(,)
;                  在路径中要要排除的词语,多个有,分隔
;
;Requirement(s):    autoitv3.2.2.0
;ReturnValue(s):    OnSuccess-Returnsanarraycontainingthelistoffilesandfoldersinthespecifiedpath
;                              OnFailure-Returnstheanemptystring""ifnofilesarefoundandsets@Erroronerrors
;                        @Error=1Pathnotfoundorinvalid
;                              @Error=3Invalid$iFlag
;                              @Error=4NoFile(s)Found
;
;Author(s):          byhttp://www.yidabu.com一大步成功社区    http://bbs.yidabu.com/forum-2-1.html
;update:            20070125                                       
;Note(s):            Thearrayreturnedisone-dimensionalandismadeupasfollows:
;                  $array=NumberofFiles\Foldersreturned返回文件/文件夹的数量
;                  $array=1stFile\Folder(notinclude$sPath)第一个文件/文件夹
;                  $array=2ndFile\Folder(notinclude$sPath)
;                  $array=3rdFile\Folder(notinclude$sPath)
;                  $array=nthFile\Folder(notinclude$sPath)
;                        SpecialThankstoSolidSnake<MetalGX91atGMaildotcom>(his_FileListToArray)
;===============================================================================



FuncmyFileListToArray($sPath,$rPath=0,$iFlag=0,$sPathExclude=0)
    Local$asFileList      ;yidabu.com提示:因为要用递归调用,$asFileList参数要单独出来
    $asFileList=myFileListToArrayTemp($asFileList,$sPath,$rPath,$iFlag,$sPathExclude)
    Return$asFileList
EndFunc    ;==>myFileListToArray

FuncmyFileListToArrayTemp(ByRef$asFileList,$sPath,$rPath=0,$iFlag=0,$sPathExclude=0)
    Local$hSearch,$sFile
    IfNotFileExists($sPath)ThenReturnSetError(1,1,"")
    IfNot($iFlag=0Or$iFlag=1Or$iFlag=2)ThenReturnSetError(3,3,"")
    $hSearch=FileFindFirstFile($sPath&"\*")
    If$hSearch=-1ThenReturnSetError(4,4,"")
    While1
      $sFile=FileFindNextFile($hSearch)
      If@errorThen
            SetError(0)
            ExitLoop
      EndIf
      
      ;yidabu.com提示:已经被排除的路径,就不要搜索子目录了
      If$sPathExcludeAndStringLen($sPathExclude)>0Then$sPathExclude=StringSplit($sPathExclude,",")
      $bExclude=False
      IfIsArray($sPathExclude)Then
            For$ii=1To$sPathExcludeStep1
                IfStringInStr($sPath&"\"&$sFile,$sPathExclude[$ii])Then
                  $bExclude=True
                  ExitLoop
                EndIf
            Next
      EndIf
      If$bExcludeThenContinueLoop
      
      Select
            CaseStringInStr(FileGetAttrib($sPath&"\"&$sFile),"D")    ;如果遇到目录
                Select
                  Case$iFlag=1    ;求文件时就递归
                        myFileListToArrayTemp($asFileList,$sPath&"\"&$sFile,$rPath,$iFlag,$sPathExclude)
                        ContinueLoop    ;求文件时跳过目录
                  Case$iFlag=2Or$iFlag=0    ;求目录时分两种情况
                        If$rPathThen    ;1如果要求对路径进行正则匹配
                            IfNotStringRegExp($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
               
            CaseNotStringInStr(FileGetAttrib($sPath&"\"&$sFile),"D")    ;如果遇到文件
                If$iFlag=2ThenContinueLoop    ;求目录时就跳过
                ;yidabu.com提示:要求正则匹配路径,且匹配失败时就跳过。遇文件就不要递归调用了。
                If$rPathAndNotStringRegExp($sPath&"\"&$sFile,$rPath,0)ThenContinueLoop
      EndSelect
      
      ReDim$asFileList
      $asFileList=$asFileList+1
      $asFileList=$sPath&"\"&$sFile
      
    WEnd
    FileClose($hSearch)
    Return$asFileList
EndFunc    ;==>myFileListToArrayTemp
原文地址 http://www.autoitx.com/thread-43-1-6.html
页: [1]
查看完整版本: _FileSearchToArray 如何跳过指定目录