2011-03-23

(Perl) 디렉토리 순회하기

윈도우 파일서버에서 100MB 가 넘는 파일을 찾기 위해서 디렉토리를 순회하는 방법을 찾아보았다. 보통은 File-Find 를 많이 쓰지만, 1TB이상 사용하고 있는 드라이브에서는 검색 도중에 Out of memory 메시지와 함께 멈추는 현상이 있었고, 대신 File-Find-Object 로 해보니, 아주 깔끔하게 검색이 되었다.


디렉토리 순회하기

File-Find

  • 일반적으로 많이 알려진 방법인데, 1TB 용량의 드라이브 검색시 죽는 현상을 발견하였다.
    # 100MB 넘는 파일 목록 출력
     
    use strict;
    use warnings;
    use File::Find;
     
    my $limited_size = 100_000_000; # 100MB
    find(\&process_file, ("P:/"));
     
    sub process_file {
        my $file = $File::Find::name;
        return unless -e $file;
        return unless -f $file;
        return unless -r $file;
        return unless -R $file;
        return if -s $file < $limited_size;
     
        print "$file\n";
    }

File-Find-Object

  • File-Find 를 객체지향적으로 재구성한 모듈으로, 대량의 디렉토리,파일에도 잘 동작한다.
  • 별도로 모듈을 설치해야하는 번거로움이 있긴하다.
    C:\> ppm install File-Find-Object
  • 예제 코드는 다음과 같다.
    # 100MB 넘는 파일 목록 출력
     
    use strict;
    use warnings;
    use File::Find::Object;
     
    my $limited_size = 100_000_000; # 100MB
    my $tree = File::Find::Object->new({}, ("P:\\"));
     
    while(my $file = $tree->next()) {
        next unless -e $file;
        next unless -f $file;
        next unless -r $file;
        next unless -R $file;
        next if -s $file < $limited_size;
     
        print "$file\n";
    }

댓글 없음:

댓글 쓰기