I'm attempting to write a script that will take in a directory path to a folder, and rename all the files in that folder to a name plus a number. For instance, rename img_0936.JPG, img_0937.JPG to hockeyPic_001.JPG and hockeyPic_002.JPG.
So here's my script:
folder = '/Users/ShaneWilliamson/Desktop/cobalt pics'
newName = 'cobaltPic'
puts 'Renaming files...'
files = Dir.entries(folder).reject{|f| f[0] == '.'}
puts files
i = 1
files.each do |file|
File.rename(file, newName + i.to_s + '.JPG')
i += 1
end
It's currently returning the correct file names (before the rename) but throwing:
Renaming files...
.
..
.DS_Store
image1.JPG
image2.JPG
image3.JPG
image4.JPG
image5.JPG
IMG_1586.JPG
IMG_1587.JPG
fileNamer.rb:10:in `rename': Invalid argument - . or cobaltPic_001.JPG
(Errno::EINVAL)
from fileNamer.rb:10
from fileNamer.rb:9:in `each'
from fileNamer.rb:9
Shanes-MacBook-Air:Desktop ShaneWilliamson$
So it seems the reject method isn't doing quite what I expect, which I think could be causing the error (system won't let the script change the 'hidden' files?).
In case it's relevant, I'm running OSX 10.7.5 on a MacBook Air. Ruby version 1.8.7 per 'ruby -v' command, which I think is just what came on the system out of the box.