Monday, January 14, 2008

ruby snippet of the evening




I have a really cute little zen stone MP3 player, and everyday I upload podcasts to it to listen to. It plays the directories in sorted order, and I create directories like "0 1 2 3 4 5" for the songs to go into.

I was having to manually rename those directories each night, and I got tired of this, so I came up with the following little Ruby script to do this for me.


#!/usr/bin/ruby

require 'find'

a = Array.new
Dir.entries(".").each do |n|
if (n =~ /([0-9]+)/)
a.push($1)
end
end
a.sort!.reverse!
a.each do |n|
File.rename(n,(n.to_i + 1).to_s)
end


Ruby sure is pretty.