修改目录文件名-Ruby实现


修改目录文件名-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.   
  25.   
  26. change_name  

相关内容