Why Ruby? It’s syntax is easy to learn and you end up with easy to read code. The Solution how to look through a directory for files Here is the code I used to rename any files with the name ._something (like ._Apple) into something.icns: puts "hello world\n" Dir["._*"].each do |file| unless file =~ /._(\w|\s)+.rtf/ || file =~ /._(\w|\s)+.webloc/ puts "#{file} renamed" newfile = String.new(file.delete "._") newfile << ".icns" File.rename(file, newfile) puts "Renamed to #{file}\n" end end puts "\n\n" unless file =~ /._(\w|\s)+.rtf/ || file =~ /._(\w|\s)+.webloc/ This line says if a string matches ._(one or more letters or spaces).rtf or ._(one or more letters or spaces).webloc it will ignore it. The unless means unless this is true do the following, pretty straightforward. The + means one or more of the things inside the parenthesis. The =~ is used to match something to a regular expression. And finally the || means ‘or’ much like in many other computer language. In order to run this script, I saved it as rename.rb file and then just typed ruby rename.rb on the command line in the directory I wanted it to perform the task.ruby regexp tutorial
2012年12月4日 星期二
Ruby Command Line Scripting
Labels:
Ruby
嗨,大家好!我是一位資訊人、畫畫人、科技人和行銷人。定位自己為網路觀察家,永遠保持好奇心與熱情,學習跨領域新事物,希望最終能成為一個全方位的人。喜歡探索新技術和創意,並將其應用於工作和生活中。也喜歡畫畫、旅行、閱讀和寫作。希望我的部落格可以為您帶來啟發和樂趣,並與您分享我的經驗和見解。謝謝您的訪問,期待與您交流!
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言