#!/usr/bin/ruby require 'ftools'; requiredExtension = '' if $*.length > 0 if $*[0] == '--help' puts '' puts 'Renames all files in the current directory to the form date.extension. If more than one file has the same time stamp, index strings "_(2)", "_(3)", and so forth, are appended before the extension.' puts '' puts 'Synopsis:' puts ' timestamp2filename [extension]' puts '' puts 'Arguments:' puts ' extension - only rename files with the given extension' puts '' else requiredExtension = '.' + $*[0] end end def twoDigitString(n) s = n.to_s if s.length == 1 s = '0' + s end return s end files = Dir::entries('.') files::each { |name| if name != '.' && name != '..' if !File::directory? name extension = '' if name =~ /\.[^.]*$/ extension = $~[0] else extension == '' end if requiredExtension == '' || extension == requiredExtension time = File::mtime name newName = time.year.to_s() + '_' + twoDigitString(time.month) + '_' + twoDigitString(time.day) + '_' newName += twoDigitString(time.hour) + ':' + twoDigitString(time.min) indexString = '' index = 1 while FileTest::exist?( newName + indexString + extension ) index += 1 indexString = '_(' + index.to_s() + ')' end newName = newName + indexString + extension indexString = '' index = 1 File::move name, newName, true end end end }