Thread

Posted on Mon Feb 5 21:06:30 2007 by pdi
Rename using path's last dirname as prefix

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

Direct Responses: 4244 | Write a response
Posted on Mon Feb 5 21:23:29 2007 by pdi in response to 4243
Re: Rename using path's last dirname as prefix

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.

Direct Responses: 4245 | Write a response
Posted on Mon Feb 5 21:57:21 2007 by exiftool in response to 4244
Re: Rename using path's last dirname as prefix
This is a bit tricky, but possible. First I'll show you the command line, then I'll explain how it works:

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
Direct Responses: 4253 | Write a response
Posted on Tue Feb 6 07:24:36 2007 by pdi in response to 4245
Re: Rename using path's last dirname as prefix

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:

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

Write a response