systematically changing filenames in a directory w/ Ruby
I'd like to grab all the files in a particular directory, and then apply a gsub(/abc/,'z') to all the filenames and essentially resave the files under the new filenames, how do I do that?
I've b开发者_Go百科een looking at File but I don't seem to have any of the parameters that it requires, aka the filename, etc.
M
File.rename(from, to) along with Dir.entries (or Dir.foreach)?
Dave's answer is right on. Here's an example:
Dir.glob("*.rb").each do |fname|
File.rename(fname, fname.gsub(/\.rb/,".rbb"))
end
Dir.glob
allows you to select files based on some given criteria, but like Dave says, you could also use Dir.entries
or Dir.foreach
精彩评论