Wednesday, December 2, 2009

[Ruby] file rename


filetype = ".jpg"

path = "."
prefix="slide_"

dir=Dir.open(path)
a=Array.new(dir.collect.length)
num=0
dir.each do |file|
if(file.length>filetype.length)
if (file[file.length-4..file.length]==filetype)
a[num]=file
num=num+1
end
end
end
puts num

count=0
for i in 0..num-1
fnew=prefix+"%05d"%count+a[i][a[i].length-4..a[i].length]
count=count+1
old_file = path + "/" + a[i]
new_file = path + "/" + fnew
puts "old "+old_file
puts "new "+new_file
puts "--------------"
File.rename(old_file, new_file)
end

No comments: