I am attempting to delete files based on a pattern from all directories contained in a given path. I have the following but it acts like an infinite loop. When I cancel out of the loop, no files are deleted. Where am I going wrong?
我正在尝试基于模式从给定路径中包含的所有目录中删除文件。我有下面这些,但它的作用就像一个无限循环。当我取消退出循环时,没有文件被删除。我哪里做错了?
def recursive_delete (dirPath, pattern)
if (defined? dirPath and defined? pattern && File.exists?(dirPath))
stack = [dirPath]
while !stack.empty?
current = stack.delete_at(0)
Dir.foreach(current) do |file|
if File.directory?(file)
stack << current+file
else
File.delete(dirPath + file) if (pattern).match(file)
end
end
end
end
end
# to call:
recursive_delete("c:\Test_Directory\", /^*.cs$/)
3 个解决方案
#1
33
You don't need to re-implement this wheel. Recursive file glob is already part of the core library.
你不需要重新实现这个*。递归文件glob已经是核心库的一部分。
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).
目录中的目录列出文件,并可以接受通配符。**是一个超级通配符,表示“匹配任何东西,包括整个目录树”,因此它将匹配任何级别的深度(包括“no”级别的深度:C:\Test_Directory中的.cs文件本身也将使用我提供的模式进行匹配)。
@kkurian points out (in the comments) that File#delete
can accept a list, so this could be simplified to:
@kkurian在评论中指出,文件#delete可以接受列表,因此可以简化为:
File.delete(*Dir.glob('C:\Test_Directory\**\*.cs'))
#2
12
Since you're using Rake already you can use the convenient FileList
object. For example:
由于您已经在使用Rake,所以可以使用方便的FileList对象。例如:
require 'rubygems'
require 'rake'
FileList['c:/Test_Directory/**/*.cs'].each {|x| File.delete(x)}
#3
11
another ruby one liner shortcuts using FileUtils to recursive delete files under a directory
另一个ruby one线性快捷键使用FileUtils递归地删除目录下的文件
FileUtils.rm Dir.glob("c:/temp/**/*.so")
even shorter:
更短:
FileUtils.rm Dir["c:/temp/**/*.so"]
another complex usage: multiple patterns (multiple extension in different directory). Warning you cannot use Dir.glob()
另一个复杂的用法:多个模式(不同目录中的多个扩展)。警告您不能使用Dir.glob()
FileUtils.rm Dir["c:/temp/**/*.so","c:/temp1/**/*.txt","d:/temp2/**/*.so"]
#1
33
You don't need to re-implement this wheel. Recursive file glob is already part of the core library.
你不需要重新实现这个*。递归文件glob已经是核心库的一部分。
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).
目录中的目录列出文件,并可以接受通配符。**是一个超级通配符,表示“匹配任何东西,包括整个目录树”,因此它将匹配任何级别的深度(包括“no”级别的深度:C:\Test_Directory中的.cs文件本身也将使用我提供的模式进行匹配)。
@kkurian points out (in the comments) that File#delete
can accept a list, so this could be simplified to:
@kkurian在评论中指出,文件#delete可以接受列表,因此可以简化为:
File.delete(*Dir.glob('C:\Test_Directory\**\*.cs'))
#2
12
Since you're using Rake already you can use the convenient FileList
object. For example:
由于您已经在使用Rake,所以可以使用方便的FileList对象。例如:
require 'rubygems'
require 'rake'
FileList['c:/Test_Directory/**/*.cs'].each {|x| File.delete(x)}
#3
11
another ruby one liner shortcuts using FileUtils to recursive delete files under a directory
另一个ruby one线性快捷键使用FileUtils递归地删除目录下的文件
FileUtils.rm Dir.glob("c:/temp/**/*.so")
even shorter:
更短:
FileUtils.rm Dir["c:/temp/**/*.so"]
another complex usage: multiple patterns (multiple extension in different directory). Warning you cannot use Dir.glob()
另一个复杂的用法:多个模式(不同目录中的多个扩展)。警告您不能使用Dir.glob()
FileUtils.rm Dir["c:/temp/**/*.so","c:/temp1/**/*.txt","d:/temp2/**/*.so"]