找回密码
 加入
搜索
查看: 4041|回复: 7

[效率算法] _FileSearchToArray 如何跳过指定目录

[复制链接]
发表于 2011-1-2 12:13:25 | 显示全部楼层 |阅读模式
本帖最后由 touch_xu 于 2011-1-7 21:44 编辑

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

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2011-1-2 15:03:39 | 显示全部楼层
这么多高手,怎么没人帮助
 楼主| 发表于 2011-1-2 15:06:22 | 显示全部楼层
肯请高手出现,如果有更好方法更好了,我想肯定有在生成数组时就跳过,就大大提高了效率
发表于 2011-1-2 16:06:58 | 显示全部楼层
只能多次运行 _FileSearchToArray查各个目录然后再合并数组
发表于 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
自己改改就好了
发表于 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
 楼主| 发表于 2011-1-4 21:55:11 | 显示全部楼层
回复  touch_xu
不知道合不合楼主的要求。
3mile 发表于 2011-1-4 11:45



    多谢3mile

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

http://www.autoitx.com/thread-12130-1-4.html
 楼主| 发表于 2011-1-4 21:57:09 | 显示全部楼层
还有这个:_FileListToArray支持正则表达式和子目录搜索
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
原文地址 http://www.autoitx.com/thread-43-1-6.html
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-4 06:09 , Processed in 0.080234 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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