2009-04-02 10 views
5

ディレクトリ構造を再帰的にコピーするが、特定のファイルタイプは除外するrubyスクリプトを作成したい。特定のファイル拡張子を除いたrubyでディレクトリ構造をコピーする方法

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 
関連する問題