2010-06-10 6 views

답변

12

내 댓글 에 대한 답변으로 "얼마나 자주 실행해야합니까?"이 예제는 strPattern과 일치하는 이름을 가진 모든 파일을 나열 할 때까지 실행됩니다. strFolder을 상수로 변경하십시오.

Public Sub ListESY() 
Const strFolder As String = "C:\SomeFolder\" 
Const strPattern As String = "*.ESY" 
Dim strFile As String 
strFile = Dir(strFolder & strPattern, vbNormal) 
Do While Len(strFile) > 0 
    Debug.Print strFile '<- view this in Immediate window; Ctrl+g will take you there 
    strFile = Dir 
Loop 
End Sub 
+0

'Debug.Print'가하는 일에 대해 궁금한 분들은 다음을 참조하십시오 : http://stackoverflow.com/questions/2916287/where-does-vba-debug-print-log-to – ecoe

3

디렉터리 사용하고 있기 때문에

은 내가, Application.FileSearch을 할 수없는입니다. Dir()을 호출 할 때마다 다음 호출이 반환됩니다.

+0

큰, 그래서 얼마나 많은 시간을 어떻게 알 수 있습니까 그것을 실행하려면? –

+1

WHILE 또는 DO 루프에서 결과의 길이를 테스트하십시오. 길이가 0이면 완료됩니다. – mohnston

2

대체 옵션 : 개체의 FileSystemObject 제품군에 대해 "Microsoft Scripting Runtime"라이브러리 (도구 ... 참조)를 사용하십시오. 아마도 다음과 같은 것일 수 있습니다 :

Public Function ESYFileCount(dir_path as String) as Long 

Dim fil As File 

    With New FileSystemObject 
     With .GetFolder(dir_path) 
      For Each fil In .Files 
       If LCase(Right(fil.Name, 4)) = ".esy" Then 
        ESYFileCount = ESYFileCount + 1 
       End If 
      Next 
     End With   
    End With 

End Function 
+1

이 코드는 더 좋을 것입니다. FSO에 대한 참조를 요구하는 대신 늦은 바인딩을 사용하는 경우). –

+0

@ David-W-Fenton - 왜 늦은 바인딩이 더 좋을지 이해하지 못합니다. –

+2

FSO의 자동화가 도메인 정책에 의해 차단 될 수 있기 때문에 후기 바인딩이 더 좋습니다. 런타임 바인딩은 참조의 기본 액세스 집합에 속하지 않는 구성 요소 (예외는 거의 없음)에 대해 * 항상 * 더 좋습니다. 퍼포먼스 히트는 레퍼런스를 캐싱하고 그것을 사용할 때마다 다시 초기화하는 대신이를 사용함으로써 쉽게 피할 수 있습니다. –

1

다음 코드는 FileSystemObject를 사용하는 것보다 약 19 배 빠르게 실행됩니다. 내 컴퓨터에서 FileSystemObject를 사용하여 세 개의 다른 부서에서 4000 개의 파일을 찾는 데 1.57 초가 걸렸지 만이 코드를 사용하는 데는 0.08 초 밖에 걸리지 않았습니다.

Public Function CountFilesWithGivenExtension(_ 
      i_strFolderWithTerminalBackslant As String, _ 
      i_strExtensionIncludingPeriod As String _ 
     ) As Long 

     If Len(Dir$(i_strFolderWithTerminalBackslant & "*" _ 
      & i_strExtensionIncludingPeriod)) > 0 Then 

      CountFilesWithGivenExtension = 1 

      While Len(Dir$) > 0 

      CountFilesWithGivenExtension = _ 
        CountFilesWithGivenExtension + 1 

      DoEvents 

      Wend 
     Else 
      CountFilesWithGivenExtension = 0 
     End If 

    End Function 

샘플 사용 :

Debug.Print CountFilesWithGivenExtension("C:\", ".ex*") 

(이하 "DoEvents는"필요하지 않습니다,하지만 당신은 필요한 경우 브레이크/일시 정지를 사용할 수 있습니다.)

관련 문제