2009-04-02 2 views
5

루비 스크립트를 작성하여 재귀 적으로 디렉토리 구조를 복사하지만 특정 파일 유형은 제외하고 싶습니다.특정 파일 확장명을 제외한 루비의 디렉토리 구조 복사 방법

folder1 
    folder2 
    file1.txt 
    file2.txt 
    file3.cs 
    file4.html 
    folder2 
    folder3 
    file4.dll 

내가이 구조를 복사 할,하지만 exlcude 가 .txt 및 .cs 파일 : 그래서, 다음 디렉토리 구조를 제공. 그래서, 결과 디렉토리 구조는 다음과 같아야합니다

folder1 
    folder2 
    file4.html 
    folder2 
    folder3 
    file4.dll 

답변

1

을 내가 시작 지점이 무엇인지 확실하지 않다, 또는 당신이 수동으로 파일의 모음 반복하고 걷고 있지만, 가정에서 무엇을 의미하는지, 부울 조건의 평가에 따라 항목을 제외 시키려면 reject 메소드를 사용할 수 있습니다.

예 :이 예에서

Dir.glob(File.join('.', '**', '*')).reject {|filename| File.extname(filename)== '.cs' }.each {|filename| do_copy_operation filename destination} 

는 글롭는 (디렉토리 포함) 파일 이름의 열거 가능한 컬렉션을 반환합니다. 거부 필터에서 원하지 않는 항목을 제외합니다. 그런 다음 복사본을 만들기 위해 파일 이름과 대상을 사용하는 메서드를 구현합니다.

배열 메소드 include를 사용할 수 있습니까? 리 젝트 블록에서 Geo의 Find example 라인을 따라 이동합니다.

Dir.glob(File.join('.', '**', '*')).reject {|file| ['.cs','.txt'].include?(File.extname(file)) } 
9

당신은 모듈을 찾을 사용할 수 있습니다. 코드 스 니펫은 다음과 같습니다.


require "find" 

ignored_extensions = [".cs",".txt"] 

Find.find(path_to_directory) do |file| 
    # the name of the current file is in the variable file 
    # you have to test it to see if it's a dir or a file using File.directory? 
    # and you can get the extension using File.extname 

    # this skips over the .cs and .txt files 
    next if ignored_extensions.include?(File.extname(file)) 
    # insert logic to handle other type of files here 
    # if the file is a directory, you have to create on your destination dir 
    # and if it's a regular file, you just copy it. 
end 
0

일부 쉘 스크립트를 사용합니까?

files = `find | grep -v "\.\(txt\|cs\)$"`.split 
관련 문제