Recursive Patterned File Delete in Ruby/Rake
You don't need to re-implement this wheel. Recursive file glob is already part of the core library.
Dir.glob('C:\Test_Directory\**\*.cs').each { |f| File.delete(f) }
Dir#glob lists files in a directory and can accept wildcards. **
is a super-wildcard that means "match anything, including entire trees of directories", so it will match any level deep (including "no" levels deep: .cs
files in C:\Test_Directory
itself will also match using the pattern I supplied).
@kkurian points out (in the comments) that File#delete
can accept a list, so this could be simplified to:
File.delete(*Dir.glob('C:\Test_Directory\**\*.cs'))
Since you're using Rake already you can use the convenient FileList
object. For example:
require 'rubygems'
require 'rake'
FileList['c:/Test_Directory/**/*.cs'].each {|x| File.delete(x)}