2009년 2월 5일 목요일

하위 폴더를 포함한 폴더 및 파일 검색 스크립트

FileSearch.au3#include <Array.au3>

#cs ----------------------------------------------------------------------------

  프로그램 이름 : FileSearch
  파일          : FileSearch.au3
  Autoit버전    : 3.3.0.0
  함수          : _FileSearch($sPath[, $sExt[, $iFlag]])
  설명          : 하위 폴더를 포함하여 폴더 및 파일 검색
                 $sPath - 파일 찾기 경로(지정하지 않을 경우 작업 폴더를 기본으로 함)
                 $sExt - 찾고자 하는 필터(기본값 '*')
                 $iFlag - 찾기 옵션
                    $iFlag = 0 폴더와 파일 모두 검색(기본값)
                    $iFlag = 1 파일만 검색
                    $iFlag = 2 폴더만 검색
  저자         : Kuro™
  연락처        : kuro78@gmil.com, http://kuroz.tistory.com
  환경          : WInXP SP3(윈도우NT기반)
  수정          : 2009.02.04 - 최초 작성

#ce ----------------------------------------------------------------------------

Func _FileSearch($sPath = '', $sExt = '*', $iFlag = 0)
    Local $aResult[1], $aPath[1]
    Local $iHead = 1, $iTail = 1, $iCount = 0
    Local $hSearch
   
    If $sPath = '' Then $sPath = @WorkingDir
    If StringRight($sPath, 1) = '\' Then $sPath = StringTrimRight($sPath, 1)
   
    _ArrayAdd($aPath, $sPath)
    $iTail += 1
   
    while $iHead <> $iTail
        Local $sTmp, $sTmpPath
        $sTmpPath = $aPath[$iHead] & '\'
       
        $hSearch = FileFindFirstFile($sTmpPath&$sExt)
        If @error <> -1 Then
            
            $iHead += 1
           
            Do
                $sTmp = FileFindNextFile($hSearch)
                If @error Then
                    FileClose($hSearch)
                    ExitLoop
                EndIf
               
                If $sTmp = '.' Or $sTmp = '..' Then ContinueLoop
               
                If StringInStr(FileGetAttrib($sTmpPath & $sTmp), 'D') Then
                    _ArrayAdd($aPath, $sTmpPath & $sTmp)
                    
                    If $iFlag <> 1 Then $iCount = _ArrayAdd($aResult, $sTmpPath & $sTmp)
                   
                    $iTail += 1
                    ContinueLoop
                EndIf
               
                If $iFlag <> 2 Then $iCount = _ArrayAdd($aResult, $sTmpPath & $sTmp)
            Until 0
        EndIf
    WEnd
   
    $aResult[0] = $iCount

    Return $aResult
EndFunc


최종 버전... 처음엔 재귀함수 형태로 파일 검색 로직을 만들었다가 좌절했던...
bfs 방식으로 검색하도록 수정 후 확실히 속도는 많이 올라갔음...

약 4000개 파일 2000개 폴더 검색하는데 평균 45초 정도 걸린다...;;

아래는 처음에 작성했던 소스.....

재귀함수는 저하늘에 별에게나 줘버려 ㅡ_ㅢ;;


#include-once
#include <Array.au3>


Func _FileSearch($sParam, $iSub = 0, $iDirInc = 0)
    Local $hSearch, $aList[1], $aDirs[1], $sPath, $sExt
    
    $sPath = StringLeft($sParam, StringInStr($sParam, '\', 0, -1))
    If $sPath = '' Then $sPath = @WorkingDir & '\'
    $sExt = StringTrimLeft($sParam, StringInStr($sParam, '\', 0, -1))
    If $sExt = '' Then $sExt = '*.*'
    
    $hSearch = FileFindFirstFile($sPath & $sExt)
    If @error <> -1 Then
        Local $sFile, $iCount
        Do
            $sFile = FileFindNextFile($hSearch)
            If @error Then
                FileClose($hSearch)
                ExitLoop
            EndIf
            If $sFile = '.' Or $sFile = '..' Then ContinueLoop
            
            If StringInStr(FileGetAttrib($sPath & $sFile), 'D') Then
                $iCnt = _ArrayAdd($aDirs, $sPath & $sFile)
                $aDirs[0] = $iCnt
                If Not $iDirInc Then ContinueLoop
            EndIf
            $iCount = _ArrayAdd($aList, $sPath & $sFile)
        until 0
        $aList[0] = $iCount
    EndIf
    
    If $iSub Then
        Local $aTmp
        For $i = 1 to $aDirs[0]
            $aTmp = _FileSearch($aDirs[$i] & '\' & $sExt, $iSub, $iDirInc)
            For $j = 1 to $aTmp[0]
                _ArrayAdd($aList, $aTmp[$j])
            Next
            $aList[0] += $aTmp[0]
        Next
    EndIf
    
    Return $aList
EndFunc




댓글 4개:

  1. 요고 사용 예좀 주세요~

    (List에 결과를 표시하고 싶은데...)

    답글삭제
  2. 이거 오토잇으로 만든건가요?

    답글삭제
  3. @마른개비 - 2009/02/06 22:59
    FileSearch.au3 파일 받으시고 아래 내용을 다른 파일에 저장하고 FileSearch파일과 같은 폴더에 저장한 후 실행해 보세요.



    #include <Array.au3>

    #include "FileSearch.au3"



    $aList = _FileSearch(@MyDocumentsDir, "*", 0)



    _ArrayDisplay($aList)



    Exit

    답글삭제
  4. @R봇 - 2009/02/07 21:16
    네.. ^^

    AutoIt으로 작성했습니다 ^^

    답글삭제