Rename using path's last dirname as prefix

Started by Archive, May 12, 2010, 08:54:00 AM

Previous topic - Next topic

Archive

[Originally posted by pdi on 2007-02-05 19:06:30-08]

I've been trying to sort this one out without much success and its likely I'm missing something. I'd appreciate any help.

There are some photos in a directory C:\path\to\dirname. Is there any way to rename the photos using as prefix the name of the last directory in the path? The filenames will be of the form DIRNAME_rest_of_filename.ext. Using the command below was not successful:

exiftool "-filename=%d_%2c.%e" dirname

Many thanks in advance.

pdi

Archive

[Originally posted by pdi on 2007-02-05 19:23:29-08]

One more detail I missed. The effort is to do the file renaming recursively from a top directory. Deeper dirnames may be of variable length and so a substring with fixed field width won't be useful.

Archive

[Originally posted by exiftool on 2007-02-05 19:57:21-08]

This is a bit tricky, but possible.  First I'll show you the command line, then
I'll explain how it works:

Code:
exiftool "-filename=%-.1d_%f.%e" "-directory=%d" dirname

The "%-.1d" takes all of the directory name but the last character (which is
the slash "/").  Without the trailing slash, the last directory name becomes part
of the filename.  (See the -w option documentation for details on how these
format modifiers work.)

But a side-effect is that all files will be moved to their parent directories, since
a file with path "a/b/c.jpg" will get its name set to "a/b_c.jpg".  So the
"-directory=%d" argument is added to override the file directory with
the original directory of the file.

In your example, you used "%c" instead of "%f".  That may be intentional, and will
work, but will give the file a number instead of using the file name as you seem
to want from your description.

I hope this helps.

- Phil

Archive

[Originally posted by pdi on 2007-02-06 05:24:36-08]

Phil,

Tricky it was, but so glad it was also possible. Many thanks for your time and your fast reply.

I used "%c" as a part of a DateTimeOriginal file naming scheme. With your help, the final command line is as follows:

Code:
exiftool -r "-filename<DateTimeOriginal" -d "%%-.1d_%y%m%d_%%.3c.%%e" "-directory=%d" dirname
This works like a charm, and lightning fast too :-)

Cheers,

pdi