Renaming Your Photos

Let’s do something really useful now. A day ago I got an email from someone wanting to know how to rename a bunch of files. A year ago my wife wanted a program to download the pictures from her camera’s memory card and rename them. I’ll show you a modified version of her program.

But first, we ought to talk about a few new methods we’ll be using in this program. The first is the Dir[] method. We’ve seen [] used with arrays before…you did know that was a method, didn’t you? Oh, yeah, it sure is. You say “arr[2]” and I say “arr.[] 2”—it’s all the same.

Anyway, rather than using an array’s [] method, we’re using the object Dir’s [] method. (The Dir is for directory.) And instead of passing in a number, like with arrays, this time you pass in a string. This is not just any string; it’s a string describing which filenames you are looking for. It then searches for those files and returns an array of the filenames (strings) it found.

The format of the input string is pretty easy. It’s basically just a filename with a few extra goodies. In fact, if you just pass in a filename, you’ll get either an array containing the filename (if the file exists) or an empty array (if it doesn’t).

puts Dir[​'ParisHilton.jpg'​]

Naturally, it didn’t find anything…what kind of programmer do you think I am?

Anyway, I could search for all JPEGs with Dir['*.jpg']. Actually, since these are case-sensitive searches, I should probably include the all-caps version as well, Dir['*.{JPG,jpg}'], which roughly means “Find me all files starting with whatever and ending with a dot and either JPG or jpg.” Of course, that searches for JPEGs only in the current working directory, which (unless you change it) is the directory you ran the program from. To search in the parent directory, you’d want something like Dir['../*.{JPG,jpg}']. If you wanted to search in the current directory and all subdirectories (a recursive search), you’d want something like Dir['**/*.{JPG,jpg}'].

And remember I said you could change your current working directory? You do that with Dir.chdir; just pass in the path to your new working directory.

We’ll also be using File.rename. It should be fairly obvious how it works. I have one thing to say about renaming, though. According to your computer, moving a file and renaming a file are really the same task. Often, only one of these is presented as an option. And, if you think about, this kind of makes sense. If you rename a file from ThingsToWrite/book.txt to ThingsToRead/book.txt, you just moved that file. And if you move a file to the same location, but with a different name, you have renamed it.

The last new method we’ll be using is print, which is almost exactly like puts, except it doesn’t advance to the next line. I don’t use it that often, but it’s nice for making little progress bars and things.

Finally, let me tell you a bit about my wife’s computer. It’s a Windows machine, so the absolute paths are going to be C:/is/for/cook.ie and such. Also, her F:/ drive is really her card reader for her camera’s memory card. We’re going to move the files to a folder on her hard disk and rename them as we do so. (And since, as we all know, move and rename are the same thing, we’ll do this in one fell swoop. Fell stroke? How does that go?)

# For Katy, with love.
# (I always write little notes in the programs
# I write for her. I deleted all of the dirty
# ones, though, so that one is all that's left.)
# This is where she stores her pictures before
# she gets her YAML on and moves them to the server.
# Just for my own convenience, I'll go there now.
Dir.chdir ​'C:/Documents and Settings/Katy/PictureInbox'
# First we find all of the pictures to be moved.
pic_names = Dir[​'F:/**/*.jpg'​]
puts ​'What would you like to call this batch?'
batch_name = gets.chomp
puts
print ​"Downloading ​#{pic_names.length}​ files: "
# This will be our counter. We'll start at 1 today,
# though normally I like to count from 0.
pic_number = 1
pic_names.each ​do​ |name|
print ​'.'​ ​# This is our "progress bar".
new_name = ​if​ pic_number < 10
"​#{batch_name}​0​#{pic_number}​.jpg"
else
"​#{batch_name}#{pic_number}​.jpg"
end
# This renames the picture, but since "name"
# has a big long path on it, and "new_name"
# doesn't, it also moves the file to the
# current working directory, which is now
# Katy's PictureInbox folder.
# Since it's a *move*, this effectively
# downloads and deletes the originals.
# And since this is a memory card, not a
# hard drive, each of these takes a second
# or so; hence, the little dots let her
# know that my program didn't hose her machine.
# (Some marriage advice from your favorite
# author/programmer: it's all about the
# little things.)

# Now where were we? Oh, yeah...
File.rename name, new_name
# Finally, we increment the counter.
pic_number = pic_number + 1
end
puts ​# This is so we aren't on progress bar line.
puts ​'Done, cutie!'

Nice! Of course, the full program I wrote for her also downloads the movies, deletes the thumbnails from the camera (since only the camera can use them), extracts the time and date from the actual jpg or avi file, and renames the file using that. It also makes sure never to copy over an existing file. Yep, it’s a pretty fancy program, but that’s for another day.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.229.246