Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created August 6, 2010 03:07
Show Gist options
  • Save qichunren/510775 to your computer and use it in GitHub Desktop.
Save qichunren/510775 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# 批量替换 指定目录及其子目录中所有文件内的字符串
class File
class << self
def gsub!(s,t,dir=Dir.pwd)
Dir.entries(dir).each do |f|
puts f
next if(f == "." || f == "..")
if(File.file? f) then
ct = IO.readlines(f).join
if ct.index(s) then
puts "replace file <#{f}>."
ct.gsub!(s,t)
File.open(f,"w+"){|ff|ff.write ct}
end
elsif(File.directory? f) then
self.gsub!(s,t,File.join(dir,f))
end
end
end
#end of <<
end
end
#!/usr/bin/env ruby
# 批量删除指定目录及子目录下含有某个扩展名的文件
class File
class << self
def grm(path,ext)
Dir.entries(path).each do |f|
next if (File.basename(f) == '.' || File.basename(f) == '..')
if File.directory?(File.join(path,f))
gdel(File.join(path,f),ext)
elsif f != File.basename(f).chomp(ext)
puts File.join(path,f)
FileUtils.rm_f(File.join(path,f))
end
end
end
#end of <<
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment