My first (useful) Ruby program
June 25th, 2011
I have a load of files with date/time-based file names in the format dd_mm_yyyy hh_mm.wav. I’d prefer these files to have a format that allowed them to be sorted in a directory in a meaningful way by the file name (i.e. yyyy-dd-mm). I also have a pet hate of file names containing spaces. It would also be useful to show the day of the week in the file name. So what I’d really like is a file name in the format yyyy-dd-mm_hhmm_<dayname>.wav.
In the past I’d have written a Perl script to rename a batch of files, but since I’ve been looking at Ruby recently it made sense to have a bash at doing this in Ruby.
Here’s the program I came up with:
Anyone looking at this who knows how to code in Ruby will, doubtless, immediately see all sorts of issues with it. But I’m quite happy with it because it didn’t take long to write and it does the job.
I’ve saved this file as RenameWavFiles.rb in the iTunes directory where all these files get created and I have a shortcut to that directory so that all I need to do is click the shortcut icon to open the directory in Windows Explorer, then double-click the Ruby file to rename any new files. The files are copied to a subdirectory with their original names just in case there’s a problem. Job done!
I wrote the code in Vim 7.2 and one nice thing about the GVim incarnation of Vim is that, in the Syntax menu, there’s a Convert to HTML option that creates the output you can see above.
![]()
Potentially similar posts
- Scan for available IP addresses – August 2011
- Rails Chronicles – Part 2 – July 2011
- Rails Chronicles – Part 1 – June 2011
- Adding your choice of text editor to Flare’s Open With menu – December 2010
- Perl basics for beginners (on Windows) – August 2010