2011-10-27

Ruby : 파일(디렉토리)이름에 특정 문자열이 포함된 목록 출력하기

Ruby에서 파일(디렉토리)이름에 특정 문자열이 포함된 목록 출력하기위해서 아래와 같이 하면 된다. Unix(Linux)/Cygwin 에서는 기본적인 도구만으로도 쉽게 할 수 있지만, 그런 환경에 안되는 곳에서는 유용하다. 아주 사소한 팁이지만, 이런 것이 여러가지가 모이면, 나중에 큰 도움이 될 것이다. ^^

# -*- coding: cp949 -*-
require 'find'
 
$dirlist    = ["C:\\"]
$sub_string = "애니메이션"
 
$dirlist.each() do |dirname|
  puts dirname if dirname.include?($sub_string)
 
  Find.find(dirname) do |file|
    next if file == nil
    next if not File.file?(file)
    next if not file.include?($sub_string)
 
    puts file
  end
end

댓글 없음:

댓글 쓰기