开发者

systematically changing filenames in a directory w/ Ruby

开发者 https://www.devze.com 2023-04-03 21:24 出处:网络
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'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.entriesor Dir.foreach

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号