歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 修改目錄文件名-Ruby實現

修改目錄文件名-Ruby實現

日期:2017/3/1 10:37:38   编辑:Linux編程
修改目錄文件名-Ruby實現
  1. require 'pathname'
  2. def change_name
  3. puts "------------"
  4. current_path = Pathname.new(File.dirname(__FILE__)).realpath
  5. current_file_name = __FILE__
  6. begin
  7. Dir::foreach(current_path) do |file|
  8. if file!="." and file!=".." and file!=".#{ current_file_name }.swp" and file!="#{ current_file_name }"
  9. #puts "File:"+file
  10. 1.upto(9) do |i|
  11. File.rename("#{ i.to_s }#{ i.to_s }.rb", "#{ i.to_s }.rb")
  12. puts "執行第#{ i }個文件"
  13. end
  14. break
  15. end
  16. end
  17. rescue => e
  18. puts "錯誤:"+e
  19. #ensure
  20. # puts "請確保文件名是否匹配!"
  21. end
  22. puts "------------"
  23. end
  24. change_name
Copyright © Linux教程網 All Rights Reserved